Linux sed usage

Source: Internet
Author: User

sed is a good file processing tool, itself is a pipeline command, mainly in the behavior of the unit processing, you can replace the data row, delete, add, select and other specific work, the following first understand sed usage of
The SED command line format is:
sed [-nefri] ' command ' Enter text         

Common options:
- n : Use Quiet (silent)mode. In generalsedin the usage, all fromSTDINinformation is generally listed on the screen. But if you add- Nparameter, only thesedspecial handling of that line(or Action)will be listed.
-E: Directly in instruction-list modesedthe action editor;
-F: Direct thesedthe action is written in a file,- f filenameYou can performfilenamewithin thesedAction;
-R:sedthe action supports the syntax of the extended formal notation. (The presupposition is the basic formal representation of French law)
-I.: Directly modify the contents of the read file instead of the screen output.        

Common commands:
A: Added,acan be followed by strings that appear on a new line(the next line of the current)~
C: Replace,Ccan be followed by a string that can replacen1,n2between the lines!
D: Delete, because it is deleted Ah, soDThe back usually does not pick up any boom;
I: Insert,Ican be followed by strings that appear on a new line(Current Previous line);
P: Print, that is, print out a selected material. UsuallyPwill be associated with the parameterSed-nWorking together ~
S: Replace, can be directly replaced by work! Usually thissthe action can be paired with the formal notation! For example1,20s/old/new/gThat's it!

Example : (Suppose we have a file named AB)
Delete a row
[[email protected] ruby] # sed ' 1d ' ab # Delete First row
[[email protected] ruby] # sed ' $d ' ab # Delete last line
[[email protected] ruby] # sed ' 1,2d ' ab # Delete first row to second row
[[email protected] ruby] # sed ' 2, $d ' ab # Delete the second line to the last row

show a row
. [[email protected] ruby] # sed-n ' 1p ' ab # Show First Row
[[email protected] ruby] # sed-n ' $p ' ab # Show last line
[[email protected] ruby] # sed-n ' 1,2p ' ab # Show first row to second row
[[email protected] ruby] # sed-n ' 2, $p ' ab # show the second line to the last row

use mode for querying
     [[email protected] Ruby] # sed-n '/ruby/p ' Ab    # ruby where all rows
     [[email protected] ruby] # sed-n '/\$/p ' Ab

add one or more lines of string
[email protected] ruby]# Cat AB
Hello!
Ruby is me,welcome to my blog.
End
[[email protected] ruby] # sed ' 1a drink tea ' ab # add string after first line"Drink Tea"
Hello!
Drink tea
Ruby is me,welcome to my blog.
End
[[email protected] ruby] # sed ' 1,3a drink tea ' ab # Add a string after the first line to the third row"Drink Tea"
Hello!
Drink tea
Ruby is me,welcome to my blog.
Drink tea
End
Drink tea
[[email protected] ruby] # sed ' 1a drink tea\ nor coffee ' ab # add more lines after the first line, using line breaks\ n
Hello!
Drink tea
or coffee
Ruby is me,welcome to my blog.
End

instead of one or more rows
     [[email protected] Ruby] # sed ' 1c Hi ' ab                # first line instead of hi
     Hi
     Ruby is me,welcome to my blog.
     end
     [[email protected] ruby] # Sed ' 1,2c Hi ' ab             # The first row to the second row instead of the hi
     Hi
     End

Replace a section in a row
format:sed ' s/ the string to replace/the new string/g   
     [[email protected] Ruby] # sed-n '/ruby/p ' ab | sed ' s/ruby/bird/g ' replace ruby bird
  [[email protected] ruby] # sed-n '/ruby/p ' ab | sed ' s/ruby//g '

Insert
[[email protected] ruby] # sed-i ' $a bye ' ab # in the file AB enter directly in the last line "Bye"
[email protected] ruby]# Cat AB
Hello!
Ruby is me,welcome to my blog.
End
Bye

Linux sed usage

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.