Three ways to use SED
1.shell command Line Input command
Format: sed [options] ' sed command ' input file
2. After entering the SED command into the script file, the SED command calls
Format: sed [options]-F SED script file input file
3. After inserting the SED command into a script file, three script files can be executed to execute the script file directly
Format:./sed script File Input file
SED options
-N does not display all rows to the screen (standard output)
-e means that the next string is resolved to the SED edit command, and if only one edit command is passed to the SED,-E option can be omitted
-F indicates that the SED script file is being invoked
Two ways of positioning text in SED
1. Use line numbers, specify a row, or specify a line number range
2. Using regular expressions
Positioning text
x x is the specified line number
X, y specifies the range of line numbers from X. to Y
/pattern/the row containing the pattern appears
/pattern/pattern/querying rows that contain two patterns
/pattern/,x line from X to match line of pattern
x,y! Query for rows that do not include X and Y line numbers
SED edit command
P Print matching lines
= Print File line number
A\ Append text information after locating line numbers
I\ inserting text information before locating line numbers
D Delete Anchor row
C\ Replace the pattern with new text
R read files from another file
W Save Text to a file
Y Transform character
Q Exit when the first pattern match is completed
L Display control characters equivalent to octal ASCII code
{} The command group executed on the anchor row
n Region next input row, with the next command to process the new row
H Copy the contents of the pattern buffer to the hold buffer
H appends the text of the pattern buffer to the hold buffer
x swaps the mode buffers and preserves the contents of the buffer
G Copies the contents of the hold buffer to the pattern buffer
G appends the contents of the hold buffer to the pattern buffer
One, text options
The-n option of the 1.sed command--does not display all rows to the screen (standard output)
Example
Sed-n ' 1p ' input displays only the first line of text
Sed ' 1p ' input not only prints the first line, but also prints the entire contents of the text
SED command Print range line
Example
Sed-n ' 3,6p ' input prints 3 to 6 lines of text
Print Match mode-n '/ca/p ' input print the line containing the CA in the text
-E option for 2.sed command--use multiple Edit commands
Example
Sed-n '/ca/= ' input prints the line number containing the CA
Example
Sed-n-E '/ca/p '-e '/ca/= ' input print line with CA line and line number
Note: Usage with multiple edit commands is not supported
such as Sed-n '/ca/p= ' input
The-f option for the 3.sed command
Sed ' Specify address a\text ' input file
Example:
Sed '/file:/a\i love you ' input
Add I love you (as a line) after the line that contains file: in text input
Script execution Append-Inserts a row after the matching row
Touch zhuijia.sed
#!/bin/sed-f #调用sed脚本
/file:/a\ # a\ means the text is added here.
#所添加的文本内容
L like you.\ # "\" symbol means line break
L Love you.
Execute script
To execute permissions
./zhuijia.sed Input
+++++++++++++++++++++++++++++++++++++++++++++++
Second, text positioning
1. Match meta-characters
Example
Sed-n '/\./p ' input matches text containing "." The line
2. Using metacharacters to match
Example
Sed-n ' $p ' input matches the last line of text
Sed-n '/.*bus/p ' input matches any string containing the end of bus
3.! Symbol
Example
Sed-n ' 2,10!p ' input matches all rows that are no longer 2 to 10 rows
4. Qualifying line ranges using line numbers and keyword matches
Example
Sed-n '/ca/, $p ' input prints a match to the CA line to the last line
Sed-n ' 3,/ca/p ' input prints the third line to the matching row of the CA
+++++++++++++++++++++++++++++++++++++++++++
Iii. sed Edit command
1. Insert text--Insert a line before the matching line
Format: sed ' Specify address i\text ' input file
Example
SED script
#!/bin/sed-f
/file:/i\ #i \ means wrap text here
We insert a new line. #插入的文本内容
Function: Insert the We insert a new line on the previous row of the file where you are located. This line of content
2. Modify text--matching lines of text replaced with new text
Format: ' Specify address c\text ' input file
Example
#!/bin/sed-f
/file:/c\ #c \ means the text is changed here.
We Modify this line #修改的文本内容
Function: Replace the line of text where file: is the We modify this
3. Delete text--delete the specified line or the specified line range
Format: Delete Address D
Example
Sed ' 1d ' input deletes the first line of text
Sed ' $d ' input to delete the last line of text
Sed ' 1,10d ' input deletes 1 to 10 lines of text
Sed '/[cc][ee]/d ' removes all rows containing case-insensitive keyword CE
4. Replace text
Format: s/replaced by string/new string/[replace option]
Replacement options
G represents all occurrences of the substituted string in the replacement text
P is combined with the-N option to print the replacement line
The W file name indicates that the output is directed to a file
1>p Options
Command format: Sed-n ' s/replaced string/new string/NP ' input file
No-n Displays the text after replacing the string
No p does not show any content
There are only lines that display a string that has been replaced
Np substitution nth match for each line
Example
Sed ' s/ca/ce/p ' input replaces the first CA in the line containing the CA in the text with the CE, and displays the replaced text
Sed-n ' s/ca/ce/' input replaces the first CA in the line containing the CA in the text with the CE, without displaying the text
Sed-n ' s/ca/ce/p ' input replaces the first CA in the line containing the CA in the text with the CE, showing only the rows that are replaced
Sed-n ' s/ca/ce/2p input replaces the second CA in the line containing the CA in the text China with CE, showing only the rows that are replaced
2>g Options
Command format: Sed-n ' s/replaced string/new string/g ' input file
No G replaces only the first replaceable string per line
Have g replace all the strings that can be substituted for each line
Example
Sed-n ' s/ca/ce/g ' replaces all CAs containing the CA's lines in the text with CE, showing only the rows that are replaced
3>w Options
Command format: Se-n ' replaced string/new string/w output ' input
Example
Sed-n ' s/ca/ce/w output ' input replaces the CA in the text with the CE and writes the result to the output file
4> text substitution combined with &--Save the replaced string to &
Example
Sed-n '/ca/(&)/P ' input ==sed-n '/ca/(CA)/P '
5. Write a new file
command format: Specify address W file name
example
sed-n ' 1,5 w Output ' input writes 1 to 5 lines in text input to output
sed-n '/ca/w output ' input writes the row of a matching CA in text input to output
6. Reading text from a file ---reading text from another file and appending it to the specified address
format: Specify the address R file name
example
sed '/ca/r otherfile ' input inserts all the text from the Otherfile file into the The input text matches the CA's text line after the
7. Exit command
Format: Specify address Q
Example
Sed ' 5 q ' input prints the first 5 lines of text and then exits
Sed-n '/.r.*/p ' input find "all" any character followed by R character and followed by 0 or more arbitrary characters
Sed-n '/.r.*/q ' finds "first" any character followed by the R character and followed by 0 or more arbitrary characters, then exits the lookup
8. Transform commands
Format: sed ' y/transformed character sequence/transformed character sequence/' input file
Example:
Sed ' y/12345/abcde/' input replaces 1,234,511 in text with ABCDE
Note: The transformed character sequence and the transform character sequence must be equal in length
9. Display control characters
Example
Sed-n ' 1,$1 ' input displays all control characters in the text (such as: SHIFT, BACKSPACE, etc.)
10. Executing the command group in the equal-bit line
Example
Equivalent command sed-n-e '/ca/p '-e '/ca/= ' input
Sed-n '/ca/{p;=} ' input
Sed '/ca/{s/i/i/g;s/le/99/;} ' input matches all I in the text line of the CA with I, and the first le of the matching line is replaced by 99
+++++++++++++++++++++++++++++++++++++++++++++++++++
Iv. Advanced Editing
1. Processing the next line of matching rows
Example
Sed '/ca/{n;s/11/99;} ' input replaces ll in the next row of the CA's matching line in the text with 99
Processing of 2.sed buffers??????????????????????????
Two types of buffers for SED
Mode buffers
Keep buffer
Example
Sed-e '/ca/h '-e ' ce/x '-e ' $G ' input
3. Separating multiple editing commands with semicolons
Format: sed ' edit command 1; Edit command 2;. ' Input file
Example
Sed ' s/ca/ce; S/csa/ce ' input replaces the CA in the text with the CE,CSA replaced by CE
Bourne the shell command line
Sed '
>s/ca/ce
>s/csa/ce
....
> $d ' input
This article is from the "All Night" blog, please make sure to keep this source http://endmoon.blog.51cto.com/8921900/1616625
Use and description of SED commands