First look at the text for our exercise, example.txt
$ cat example.txt
This is a test file.
It is the last day of 2018.
Hope all you success!
HAPPY NEW YEAR!
-e: execute multiple commands
Normally, sed can only be followed by one command. Of course, you can use the pipe symbol "|' to execute multiple commands. For example, I want to replace "last" with "first" and "2018" with "2019",
using the pipe symbol:
sed "s/last/first/g" example.txt | sed "s/2018/2019/g"
The standard output of the first sed serves as the standard input of the second sed.
Of course, a more concise way is to use the -e parameter, a sed can execute multiple commands.
sed -e "s/last/first/g" -e "s/2018/2019/g" example.txt
# This is a test file.
# It is the first day of 2019.
# Hope all you success!
# HAPPY NEW YEAR!
-f: declare sed script file
Create a new text file example.sed and enter in the text:
s/last/first/g
s/2018/2019/g
Note that there is no need for quotation marks in the file, and then execute the file on the command line:
sed -f example.sed example.txt
# This is a test file.
# It is the first day of 2019.
# Hope all you success!
# HAPPY NEW YEAR!
Writing commands out of script files is suitable for situations where there are many commands or frequently used.
Statement line
Use line numbers to declare lines
Add the line number directly in front of s to tell sed the line to be executed.
For example, we want to replace the is in line 2 with was:
sed '2s/is/was/g' example.txt
# This is a test file.
# It was the last day of 2018.
# Hope all you success!
# HAPPY NEW YEAR!
如果要声明范围,可以使用逗号“,”将行号分割开。比如将example.txt的第2-4的大写字母全部改成小写。
sed '2,4s/[A-Z]/\L&/g' example.txt
# This is a test file.
# it is the last day of 2018.
# hope all you success!
# happy new year!
By declaring lines through regular expressions,
we can not only tell sed the lines to be operated on by the line number, but also filter out the lines to be operated on by regular expressions. Note that the regular expression must be placed within the slash.
For example, we want to filter out the lines that end with an exclamation mark (!) and turn all uppercase letters in these lines into lowercase letters.
sed '/!$/s/[A-Z]/\L&/g' example.txt
# This is a test file.
# It is the last day of 2018.
# hope all you success!
# happy new year!
Where "!$" means the line ending with an exclamation mark.
Just like declaring line ranges by numbers, you can also declare line ranges through regular expressions. For example, we want to change all uppercase letters in the line between the penultimate character is a number [0-9] to the penultimate character is an exclamation mark.
sed '/[0-9].$/,/!$/s/[A-Z]/\L&/g' example.txt
# This is a test file.
# it is the last day of 2018.
# hope all you success!
# HAPPY NEW YEAR!
It can be seen that combined with regular expressions, sed can complete very complex operations.
d: delete row
The s parameter mainly completes the search and replacement function, and the d parameter can delete a row.
For example, we want to delete rows 1-3:
sed '1,3d' example.txt
# HAPPY NEW YEAR!
If we want to delete the first and third lines, we can split the two commands with a semicolon ";":
sed '1d;3d' example.txt
# It is the last day of 2018.
# HAPPY NEW YEAR!
Of course, you can also specify the rows to be deleted through regular expressions, so we will not give examples.
p: output line
It has been introduced before, and it is often used with "-n" to output a specific line. For example, if I want to view the
9999th line of a large file, I can sed -n '9999p' filename
by! For reverse selection
, the "!" in the regular expression can mean "no", so if I want to select the line that does not contain numbers in example.txt:
sed -n '/[0-9]/!p' example.txt
# This is a test file.
# Hope all you success!
# HAPPY NEW YEAR!
Summarize d, p and!
The parameter d is used to delete, the parameter p is used to output, which is equivalent to keep, and the parameter! Means reverse selection. So the same purpose can be achieved through their different combinations. The following are examples of different combinations of these 3 parameters
A hundred reading is worse than one practice
===== THE END ====
Reference materials: http://www.grymoire.com/Unix/Sed.html#uh-30