Sed is a good file processing tool. It is a pipeline command. It is mainly processed in behavioral units. It can perform specific tasks such as replacing, deleting, adding, and selecting data lines. Let's first understand the usage of sed.
The sed command line format is:
sed [-nefri] 'command' input text
1. Common options
-n: Use silent mode. In normal sed usage, all data from STDIN is normally listed on the screen. But if you add the -n parameter, only the line (or action) that has been specially processed by sed will be listed.
-e: Edit the action of sed directly on the command line mode;
-f: Write the sed action directly in a file, -f filename can execute the sed action in filename;
The -r:sed action supports extended regular notation syntax. (The default is the base regular notation syntax)
-i: Modify the contents of the read file directly instead of outputting it on the screen.
2. Common commands
a: new, a can be followed by strings, and these strings will appear on a new line (the current next line)~
c: replace, c can be followed by a string, and these strings can replace the lines between n1 and n2!
d: delete, because it is deleted, so there is usually no dong dong after d;
i: Insert, i can be followed by strings, and these strings will appear on a new line (the current previous line);
p: print, that is, to print out the selected data. Usually p will work with the parameter sed -n~
s: Replacement, you can directly carry out the replacement work! Usually the action of this s can be paired with regular notation! For example 1,20s/old/new/g and that's it!
3. Example: (assuming we have a file named ab)
1. delete a row
[root@localhost ruby] # sed '1d' ab #delete the first line [root@localhost ruby] # sed '$d' ab # delete the last line [root@localhost ruby] # sed '1,2d' ab #delete the first line to the second line [root@localhost ruby] # sed '2,$d' ab #delete the second line to the last line
2. Display 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,2p' ab #Display the first line to the second line [root@localhost ruby] # sed -n '2,$p' ab #Display the second line to the last line
3. Query using schema
[root@localhost ruby] # sed -n '/ruby/p' ab # The query includes all lines where the keyword ruby is located [root@localhost ruby] # sed -n '/\$/p' ab # The query includes all lines where the keyword $ is located, use backslash \ to block special meaning
4. Add one or more lines of string
[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" after the first line to the third line 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, use a newline character\n Hello! drink tea or coffee ruby is me,welcome to my blog. end
5. In place of one or more lines
[root@localhost ruby] # sed '1c Hi' ab #The first line is replaced by Hi Hi ruby is me,welcome to my blog. end [root@localhost ruby] # sed '1,2c Hi' ab #The first line to the second line is replaced by Hi Hi end
6. Replace part of a line
Format: sed 's/string to be replaced/new string/g' (the string to be replaced can use regular expressions)
[root@localhost ruby] # sed -n '/ruby/p' ab | sed 's/ruby/bird/g' #替换ruby为bird [root@localhost ruby] # sed -n '/ruby/p' ab | sed 's/ruby//g' #删除ruby
7. Insert
[root@localhost ruby] # sed -i '$a bye' ab #Enter "bye" directly in the last line of file ab [root@localhost ruby]# cat ab Hello! ruby is me,welcome to my blog. end bye
8. Delete matching lines
sed -i '/match string/d' filename (Note: If the match string is a variable, you need "" instead of ''. Remember it seems to be)
9. Replace a certain string in the matched line
sed -i '/match string/s/replace source string/replace target string/g' filename
Article source: http://www.cnblogs.com/dong008259/archive/2011/12/07/2279897.html