Note: The reference book for the Sed command is "Sed&awk", so the notes are also reference to the book.
Basic syntax and commands for SED commands:
I. SED command syntax
sed [options] {sed-commands} {Input-file}
SED is the data write mode space that reads Input-file per line, then executes Sed-command and executes.
Options is an optional parameter used to pass the SED command.
You can use the-e parameter when you need to use more than one command, with the following syntax:
sed [options]-e {sed-command1}-e {sed-command2}-e {command3} {Input-file}
Multiple lines of command can also be displayed with the following syntax:
sed [option] ' {
>sed-command1
>sed-command2
} ' {Input-file}
When there are too many commands, you can write the command to a document and then use the-f parameter to implement it with the following syntax:
sed [options]-f {commands-file} {Input-file}
The SED command does not change the contents of the file.
Ii. process of implementation of the SED command
1, read: read one line to the mode space
2. Execute (EXECUTE): Executes the command in the pattern space (multiple commands are executed sequentially)
3. Print (print): Prints the executed content in the pattern space and empties the mode space
4, repeat (repeat): Repeat the above procedure by row
Three, print mode space (command p)
1. The SED command print mode space is not added by default [options]
Syntax: sed ' p ' {input-file}
[[email protected] ~]# sed ' p ' employee.txt
101,john Doe,ceo
101,john Doe,ceo
102,jason Smith,it Manager
102,jason Smith,it Manager
103,raj Reddy,sysadmin
103,raj Reddy,sysadmin
104,anand Ram,developer
104,anand Ram,developer
105,jane Miller,sales Manager
105,jane Miller,sales Manager
2. output file content only
Syntax: Sed-n ' P ' {input-file}
[email protected] ~]# sed-n ' P ' employee.txt
101,john Doe,ceo
102,jason Smith,it Manager
103,raj Reddy,sysadmin
104,anand Ram,developer
105,jane Miller,sales Manager
3. Specify the address range to print
① only one line is printed
Syntax: Sed-n ' x P ' {Input-file} (x indicates a row)
[[email protected] ~]# sed-n ' 2 p ' employee.txt
102,jason Smith,it Manager
② Print line x to page y
Syntax: Sed-n ' x, y p ' {input-file} (x, Y represents a row, and when Y is $, that means printing from line x to the last line)
[[email protected] ~]# sed-n ' 1,3 P ' employee.txt
101,john Doe,ceo
102,jason Smith,it Manager
103,raj Reddy,sysadmin
[[email protected] ~]# sed-n ' 3,$ p ' employee.txt
103,raj Reddy,sysadmin
104,anand Ram,developer
105,jane Miller,sales Manager
You can modify the address range by using commas, plus signs, and wavy numbers.
Plus + with commas, you can specify several lines of the phase, rather than absolute lines. If N,+m represents the M line after the start of the nth row
Tilde ~ You can also specify an address range. It specifies the number of rows to skip each time. If n~m indicates starting at line N, skipping m rows at a time:
1,3,5,7-Matched,...... (only odd lines are printed)
2~2 matching 2,4,6,8,......
Match 1,4,7,10,.....
Match 2,5,8,11,.....
Matching mode
SED can match according to the line number, and SED can also match the print according to the contents of the file.
① single condition Matching
Syntax: Sed-n '/word/p ' {input-file}
[email protected] ~]# sed-n '/101/p ' employee.txt
101,john Doe,ceo
② Print first content matches to a line of content
Syntax: Sed-n '/word/,x p ' {input-file} (when x=$, then directly matches to the last line)
[[email protected] ~]# sed-n '/102/,3 p ' employee.txt
102,jason Smith,it Manager
103,raj Reddy,sysadmin
If there is no match to Word in line x, sed will match after X line and print.
③ prints a line that matches two lines between word
Syntax: Sed-n '/word1/,/word2/p ' {input-file}
[email protected] ~]# sed-n '/102/,/104/p ' employee.txt
102,jason Smith,it Manager
103,raj Reddy,sysadmin
104,anand Ram,developer
④ prints the lines that match word and the x lines that follow them
Syntax: Sed-n '/word/,+x p ' {input-file}
[[email protected] ~]# sed-n '/102/,+2 p ' employee.txt
102,jason Smith,it Manager
103,raj Reddy,sysadmin
104,anand Ram,developer
Iv. deleting rows (command D)
In front of the print mode, we use the command is P, then if you want to delete a row, we should use the command is D, next, we will look at the use of the delete row command d.
Use command D to delete the contents of the schema space only, do not modify the source files.
① default use of delete command D
Because SED defaults to match all rows, if you do not specify an address range when using command D, the default is to delete all the contents of the schema space, that is, the output is empty.
Syntax: sed ' d ' {input-file}
② Delete Only a row
Syntax: sed ' x d ' {input-file}
[[email protected] ~]# sed ' 2 d ' employee.txt
101,john Doe,ceo
103,raj Reddy,sysadmin
104,anand Ram,developer
105,jane Miller,sales Manager
③ Delete a specified range of rows
Syntax: sed ' x, y d ' {input-file} (when y=$, that is, from the X line to the last line)
[[email protected] ~]# sed ' 1,4 d ' employee.txt
105,jane Miller,sales Manager
④ the same as the P command, the D command can also match word
Syntax: sed '/word/d ' {input-file}
[[email protected] ~]# sed '/101/d ' employee.txt
102,jason Smith,it Manager
103,raj Reddy,sysadmin
104,anand Ram,developer
105,jane Miller,sales Manager
It is easy to see that the D command is basically the same as the use of the P command. Here are a few more special uses.
⑤ Delete all empty lines
Syntax: sed '/^$/d ' {input-file}
[email protected] ~]# sed-n ' P ' employee.txt
101,john Doe,ceo
102,jason Smith,it Manager
103,raj Reddy,sysadmin
104,anand Ram,developer
105,jane Miller,sales Manager
[[email protected] ~]# sed '/^$/d ' employee.txt
101,john Doe,ceo
102,jason Smith,it Manager
103,raj Reddy,sysadmin
104,anand Ram,developer
105,jane Miller,sales Manager
⑥ Delete all comment lines (assuming that the comment lines start with #)
Syntax: sed '/^#/d ' {input-file}
V. Write the contents of the schema space to a file (command W)
The W command can write the contents of the current pattern space to a file, with the same usage as the P command and the D command, all within its specified address range.
Example: Sed-n ' 2 w output.sed ' Employee.txt
The above statement means that the second line of content in the Employee.txt file is written to the output.sed file, and the essence of the write is to overwrite the contents of the output.sed file each time.
Note: The reference book for the Sed command is "Sed&awk", so the notes are also reference to the book.
+1
This article is from the "Party Add" blog, make sure to keep this source http://xxpf09.blog.51cto.com/7240879/1795062
sed command (1)-Basic syntax and commands