SED command basic syntax (i)
Learning Goals
Sed workflow
SED command basic syntax
1. Sed workflow
The SED workflow is divided into the following steps
Flowchart Explanation: First read a line stored in the pattern space (pattern spaces);
Secondly, the content is obtained from the pattern space, and the command is parsed.
Then the contents of the pattern space are emptied and output;
Finally, repeated reading;
2. Basic syntax of sed command
(1) format of the SED command
Sed[options]{sed-commands}{input-file}
(2) sed p command with-n option (print,--quiet)
Sed ' P ' test.txt this will output two times per line, (read-in mode space output once, empty mode space and then output once)
Sed–n ' P ' test.txt plus-n is the same as the cat command, only the contents of the mode space are output;
Sed–n ' 1 P ' test.txt only prints the first line;
Sed–n ' 1,3 p ' test.txt printing line;
Sed–n '/hello/p ' test.txt print Hello line;
Sed–n '/hello/,/hello1/p ' test.txt print hello to Hello1 line;
Sed–n '/^hello/p ' test.txt prints the line at the beginning of the Hello; (regular expression)
Sed–n '/hello$/p ' test.txt prints the line at the end of the Hello; (regular expression)
Sed–n '/^[0-9] \{3\}/p ' test.txt print the number repeat 3 times the content;
Sed–n ' Test.txt P ' is printed once every other line (2-1=1);
(3) The D command (delete) of the SED, similar to the P command;
Sed '/103/d ' test.txt delete the line where mode space 103 is located, and the contents of the text file (test.txt) do not change;
(4) More SED options
-F Read command from File
Sed–f cmd.sed test.txt New cmd.sed command file, the file contents are as follows:/hello/p
Multiple commands can be written in the command file;
-e option to execute multiple commands
Sed–n-e '/hello/p ' –e '/hello1/p ' test.txt prints the line where Hello and Hello1 are located;
SED command basic syntax (i)