Sed in detail

The sed command 
calls the sed command in two forms: 

sed [options] 'command' file(s) 

sed [options] -f scriptfile file(s) 
a\ 
Add a line of text after the current line. 
b lable 
branches to the marked place in the script, or to the end of the script if the branch does not exist. 
c\ 
Change the text of this line with the new text. 

Delete the line from the template block (Pattern space) position. 

delete the first line of the template block. 
i\ 
Insert text above the current line. 

copies the contents of the template block to a buffer in memory. 

appends the contents of the template block to the buffer  in memory

gets the contents of the memory buffer and replaces the text in the current template block. 

gets the contents of the memory buffer and appends it to the text of the current template block. 
lList 
cannot print a list of characters. 

reads the next line of input and processes the new line with the next command instead of the first command. 

Appends the next input line after the template block and inserts a new line between them, changing the current line number. 

prints the lines of the template block. 
P (uppercase) 
prints the first line of the template block. 
qQuit 
Sed. 
r file 
reads lines from file. 
t label 
if branch, starting from the last line, once the condition is satisfied or T, t command, it will lead to branch to the command with the label, or to the end of the script. 
T label 
error branch, starting from the last line, once an error or T, t command occurs, it will cause a branch to the command with the label, or to the end of the script. 
w file 
Writes and appends the template block to the end of file. 
W file 
writes and appends the first line of the template block to the end of file. 

Indicates that the following command will act on all unselected lines. 
s/re/string 
replaces the regular expression re with string. 

prints the current line number. 

Extend the comment up to the next newline. 
The following is the replacement marker 

g means inline full replacement. 

p means print line. 

w means write the line to a file. 

x means to swap the text in the template block and the text in the buffer. 

y means to translate a character into another character (but not for regular expressions) 
 
4. Option 
-e command, --expression=command 
allows multiple editing. 
-h, --help 
Print help and show the address of the bug list. 
-n, --quiet, --silent suppress 
 
default output. 
-f, --filer=script-file 
bootstrap sed script filename. 
-V, --version 
Print version and copyright information. 
 
5. The beginning of the meta character set ^ 
anchors the line such as: /^sed/ matches all lines starting with sed.  
The end of the $ 
anchor line is: /sed$/ matches all lines ending with sed.  

matches a non-newline character such as: /sd/ matches s followed by any character, then d.  

matches zero or more characters eg: /*sed/ matches all lines where the template is one or more spaces followed by sed. 
[]
matches a specified range of characters, such as /[Ss]ed/ matches sed and Sed. 
[^]
Matches a character that is not in the specified range, such as: /[^A-RT-Z]ed/ matches the beginning of a letter that does not contain AR and TZ, followed by the line of ed. 
\(..\)
saves matching characters, such as s/\(love\)able/\1rs, loveable is replaced by lovers. 
&
Save the search characters to replace other characters, such as s/love/**&**/, love becomes **love**.  
\< 
anchors the start of a word, eg:/\<love/ matches a line containing a word starting with love.  
\> 
Anchors the end of a word, eg /love\>/ matches a line containing a word ending in love.  
x\{m\} 
repeats the character x, m times, such as: /0\{5\}/ matches a line containing 5 o.  
x\{m,\} 
repeats the character x at least m times, such as: /o\{5,\}/ matches lines with at least 5 o's.  
x\{m,n\} 
repeats the character x, at least m times, not more than n times, such as: /o\{5,10\}/ matches 5--10 lines of o. 
6. Instance 
deletion: d command 

$ sed '2d' example----- delete the second line of the example file. 

$ sed '2,$d' example----- delete all lines from the second line to the end of the example file. 

$ sed '$d' example----- delete the last line of the example file. 

$ sed '/test/'d example----- delete all lines containing test in the example file. 
Replace: s command 

$ sed 's/test/mytest/g' example----- replaces test with mytest in the whole line. Without the g flag, only the first matching test on each line is replaced with mytest. 

$ sed -n 's/^test/mytest/p' example-----(-n) option used with the p flag to print only those lines where the substitution occurs. That is, if test at the beginning of a line is replaced with mytest, print it. 

$ sed 's/^192.168.0.1/&localhost/' example----- The & symbol represents the found part of the replacement string. All lines starting with 192.168.0.1 will be replaced with itself plus localhost, which becomes 192.168.0.1localhost. 

$ sed -n 's/\(love\)able/\1rs/p' example-----love is marked as 1, all loveables will be replaced with lovers, and the replaced lines will be printed. 

$ sed 's#10#100#g'  example-----Any character following the s command is considered a new delimiter, so "#" is the delimiter here, instead of the default "/" delimiter. means to replace all 10s with 100s.
Range of selected lines: comma 

$ sed -n '/test/,/check/p' example-----All lines within the range determined by the template test and check are printed. 

$ sed -n '5,/^test/p' example----- prints all lines starting from the fifth line to the first containing the line starting with test. 

$ sed '/test/,/check/s/$/sed test/' example-----For lines between template test and west, replace the end of each line with the string sed test. 
Multi-point editing: e command 

$ sed -e '1,5d' -e 's/test/check/' example-----(-e) option allows to execute multiple commands on the same line. As shown in the example, the first command deletes lines 1 to 5, and the second command replaces test with check. The order in which the commands are executed has an effect on the result. If both commands are substitution commands, the first substitution command will affect the result of the second substitution command. 

$ sed --expression='s/test/check/' --expression='/love/d' example----- A better command than -e is --expression. It can assign values ​​to sed expressions. 
Read in from file: r command 

$ sed '/test/r file'  The content in example-----file is read in and displayed after the line matching test. If multiple lines are matched, the content of the file will be displayed below all matching lines.
Write to file: w command 

$ sed -n '/test/w file' example-----All lines containing test in example are written to file. 
Append command: a command 

$ sed '/^test/a\\--->this is a example' example<-----'this is a example' is appended to the line starting with test, sed requires The command a is followed by a backslash. 
Insert: i command 
$ sed '/test/i\\ 
new line 
-------------------------' example 
If test is matched, put the reverse The text after the slash is inserted before the matching line. 
Next: n command 

$ sed '/test/{ n; s/aa/bb/; }' example----- if test is matched, move to the next line of the matching line, replace aa of this line, becomes bb, prints the line, and continues. 
Variation: y command 

$ sed '1,10y/abcde/ABCDE/' example----- Convert all abcde in lines 1--10 to uppercase. Note that regular expression metacharacters cannot be used with this command. 
exit: q command 

$ sed '10q'  example----- After printing line 10, exit sed.
Hold and Get: h command and G command 

$ sed -e '/test/h' -e '$G example-----When sed processes a file, each line is stored in a temporary buffer called the pattern space, unless the line is deleted or output is canceled, otherwise all processed lines will be printed to the screen. Then the pattern space is emptied, and a new line is stored for processing. In this example, after a line matching test is found, it is stored in the pattern space, and the h command copies it and stores it in a special buffer called the hold buffer. The second statement means that when the last line is reached, the G command fetches the line that holds the buffer, puts it back into the pattern space, and appends to the end of the line that now exists in the pattern space. In this example it is appending to the last line. Simply put, any line containing test is copied and appended to the end of the file. 
Hold and swap: h command and x command 

$ sed -e '/test/h' -e '/check/x' example ----- swap the pattern space and the contents of the hold buffer. That is, the lines containing test and check are interchanged.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326260709&siteId=291194637
sed