SED command
Row-based operations to replace, delete, add, and stdin data
The parameters are as follows
-N Quiet mode. By default, sed outputs all content, including modified and unmodified content. Use-n will only output the modified line of content.
-R supports extended regular expressions
-I directly modifies the file and does not output content
Parameter-I operates directly on the file and does not output anything.
Sed-i ' $aEND line ' regular_express.txt
$ is the last line meaning, used to specify the range. A is the insert action. END Line is a row of content
[email protected] ~]# cat Regular_express.txt
"Open Source" is a good mechanism to develop programs.
Apple is my favorite food.
......
Gnu
END Line
Line Range
N1,n2 optional, specifies that SED moves only from N1 lines to N2
Action
A new, add a line under each line in the range, example
[Email protected] ~]# cat-n regular_express.txt |sed '1,3aNewline '
1 "Open Source" is a good mechanism to develop programs.
New Line
2 Apple is my favorite food.
New Line
3 Football game isn't use feet only.
New Line
4 This dress doesn ' t fit me.
5 However, this dress was about $3183 dollars.
6 GNU is free air isn't free beer.
7 She hair is very beauty.
8 I can ' t finish the test.
The range is set on line 1th to 3rd, with one row below each line new lines
C substitution, if there is a specified range, replaces the entire range with the specified content, or, if no range is specified, replaces each row of stdin with the specified content.
[Email protected] ~]# cat-n regular_express.txt |sed ' 1,3creplace line '
Replace Line
4 This dress doesn ' t fit me.
5 However, this dress was about $3183 dollars.
6 GNU is free air isn't free beer.
7 She hair is very beauty
......
You can see that the 第1-3 line is replaced by a line
[Email protected] ~]# cat-n regular_express.txt |sed ' creplace line '
Replace Line
Replace Line
Replace Line
Replace Line
......
Each row is replaced
d Delete, delete whole line according to condition
First look at the contents of the file that is not processed
[Email protected] ~]# cat-n regular_express.txt
1 "Open Source" is a good mechanism to develop programs.
2 Apple is my favorite food.
3 Football game isn't use feet only.
4 This dress doesn ' t fit me.
5 However, this dress was about $3183 dollars.
6 GNU is free air isn't free beer.
[Email protected] ~]# cat-n regular_express.txt |sed '/3/d '
1 "Open Source" is a good mechanism to develop programs.
2 Apple is my favorite food.
4 This dress doesn ' t fit me.
6 GNU is free air isn't free beer.
There is a regular condition between/and/and only the contents of that line contain 3 of this character, then the whole row is deleted. In the original content, the third line contains the line number 3, and the 5th line contains $3183, so the two lines are deleted.
Also, if you specify only a range, the contents of the range are deleted
[Email protected] ~]# cat-n regular_express.txt |sed ' 1,4d '
5 However, this dress was about $3183 dollars.
6 GNU is free air isn't free beer.
But the seemingly range and conditions cannot be removed,
I insert, similar to a, but a is inserted one line below the original line, and I is inserted before the original line.
P printing, with the-n Quiet mode parameter, and range, can easily achieve the print file of the specified line range content
[Email protected] ~]# cat-n regular_express.txt |sed-n ' 2,5p '
2 Apple is my favorite food.
3 Football game isn't use feet only.
4 This dress doesn ' t fit me.
5 However, this dress was about $3183 dollars.
S///g replacements, such as the mating range, are replaced within the range.
[Email protected] ~]# cat-n regular_express.txt |sed ' s/good/------------/g '
1 "Open Source" is a------------mechanism to develop programs.
2 Apple is my favorite food.
3 Football game isn't use feet only.
4 This dress doesn ' t fit me.
5 However, this dress was about $3183 dollars.
6 GNU is free air isn't free beer.
7 She hair is very beauty.
8 I can ' t finish the test.
9 oh! The Soup taste------------.
Ten motorcycle is cheap than car.
Linux Learning notes: SED