Linux Basic Article -14,sed command detailed

Source: Internet
Author: User

Basic usage of SED

Sed:stream Editor

Line Editor

By default, the file content is not modified, only the data in the pattern space is processed, and after processing is finished, the pattern space is printed to the screen

sed [options] ' addresscommand ' file ...

-N: Silent mode, does not display the contents of the mode space

-I: Modify the original file directly

-E: Multiple scripts can be executed at the same time

-f/path/to/sed_script

Sed-f/path/to/scripts File

-R: Using extended regular expressions


D: Delete rows that match the criteria

P: Show rows that match the criteria

A:\string: Appends a new row after the specified row, with the contents of string

\ nthe line can be wrapped

I:\string: Adds a new row before the specified line, with the contents of string

R file: The contents of the specified file are added to the qualifying line

W File: The address specified in the range is saved to the specified file

s/pattern/string/: Find and Replace, default to only the first row in each row that is matched by the pattern

[Email protected] ~]# sed s/oot/oot//etc/fstab

s/pattern/string/g Global Substitution

I ignore character casing

@@@ ## # #通用


Delete the first two lines

Sed ' 1,2d '/tmp/text.txt


Delete First row and last row

Sed ' 1,$-1d '/tmp/text.txt


Delete one to last row

Sed ' 1, $d '/tmp/text.txt


Delete first three rows

Sed ' 1,+2d '/tmp/text.txt



Delete the specified line

[[Email protected] ~]# sed ' 2d '/tmp/text.txt


There's 8 in the delete row.

[[Email protected] ~]# sed '/8/d '/tmp/text.txt


----------


Print rows that match the criteria

Sed-n ' 1p '/tmp/test.txt



This article is from the "James Zhan Linux Advanced ops" blog, so be sure to keep this source http://jameszhan.blog.51cto.com/10980469/1875922

Linux Basic Article -14,sed command detailed

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.