[Zz] usage of sed in Linux

Source: Internet
Author: User

Sed is a good file processing tool. It is a pipeline command mainly used for processing by behavior units. It can replace, delete, add, and select data rows, next, let's take a look at the usage of sed.
Sed command line format:
Sed [-nefri] 'command' input text

Common options:
-N: Use silent mode. In general sed usage, all information from stdin is usually listed on the screen. However, if the-n parameter is added, only the row (or action) that has been specially processed by SED will be listed.
-E: directly edit the SED action in the Command column mode;
-F: Write the SED action directly in a file.-f filename can execute the SED action in filename;
-R: sed supports the syntax of extended regular notation. (The default is the basic regular expression syntax)
-I: directly modify the content of the file to be read, rather than output by the screen.

Common commands:
A: new. A can be followed by strings. These strings will appear in a new row (the next row currently )~
C: replace. C can be followed by strings. These strings can replace rows between N1 and N2!
D: delete. Because it is deleted, D is usually not followed by any comment;
I: insert, I can be followed by strings, and these strings will appear in the new line (the previous line currently );
P: print the selected data. Usually P will work with the sed-N parameter ~
S: replace, you can directly replace the work! Generally, this s action can be combined with regular notation! For example, 1, 20 s/old/new/g!

Example: (assume we have a file named AB)
Delete a row
[Root @ localhost Ruby] # sed '1d 'AB # Delete the first line
[Root @ localhost Ruby] # sed '$ d' AB # Delete the last row
[Root @ localhost Ruby] # sed '1, 2D 'AB # Delete rows 1 to 2
[Root @ localhost Ruby] # sed '2, $ d' AB # Delete the second row to the last row

Show a row
. [Root @ localhost Ruby] # sed-N '1p' AB # display the first line
[Root @ localhost Ruby] # sed-n' $ P' AB # display the last line
[Root @ localhost Ruby] # sed-n'1, 2 p 'AB # display the first row to the second row
[Root @ localhost Ruby] # sed-n' 2, $ P' AB # display the second row to the last row

Use mode for query
[Root @ localhost Ruby] # sed-n'/Ruby/P' AB # query all rows including the keyword Ruby
[Root @ localhost Ruby] # sed-n'/\ $/P' AB # query all rows with the keyword $. Use backslash \ To block special meanings.

Add one or more strings
[Root @ localhost Ruby] # Cat AB
Hello!
Ruby is me, welcome to my blog.
End
[Root @ localhost Ruby] # sed '1a drink tea 'AB # Add the string "drink tea" after the first line"
Hello!
Drink tea
Ruby is me, welcome to my blog.
End
[Root @ localhost Ruby] # sed '1, 3A drink tea 'AB # Add the string "drink tea" between the first row and the third row"
Hello!
Drink tea
Ruby is me, welcome to my blog.
Drink tea
End
Drink tea
[Root @ localhost Ruby] # sed '1a drink tea \ nor coffee 'AB # add multiple lines after the first line and use the line break \ n
Hello!
Drink tea
Or coffee
Ruby is me, welcome to my blog.
End

Replace one or more rows
[Root @ localhost Ruby] # sed '1c Hi' AB # Replace the first line with hi
Hi
Ruby is me, welcome to my blog.
End
[Root @ localhost Ruby] # sed '1, 2c Hi' AB # Replace the first and second rows with hi
Hi
End

Replace a part of a row
Format: SED's/string to be replaced/New String/G' (the string to be replaced can use a regular expression)
[Root @ localhost Ruby] # sed-n'/Ruby/P' AB | SED's/Ruby/bird/G' # Replace Ruby with bird
[Root @ localhost Ruby] # sed-n'/Ruby/P' AB | SED's/Ruby // G' # Delete Ruby

Insert
[Root @ localhost Ruby] # sed-I '$ A Bye' AB # Enter "bye" in the last line of file AB"
[Root @ localhost Ruby] # Cat AB
Hello!
Ruby is me, welcome to my blog.
End
Bye

From: http://www.cnblogs.com/dong008259/archive/2011/12/07/2279897.html

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.