sed It's a stream editor (stream editor), Mainly used to perform text replacement . but sed The main purpose of editing files in a batch way is not to edit them in a batch way .
Basic command format
sed [ Common options ] Command text Input
-n (--quiet, --silent)： Quiet mode . stay sed In the basic usage of , All the information from the standard output is listed on the terminal . add -n After the parameter , Then only by sed Only those lines processed will be output .
-e： Specifies the command text to be executed on the command line mode . By default, you do not need to specify , Only when you want to execute multiple command texts at the same time, you need to explicitly specify -e Options .
-f： To execute multiple command texts at the same time , You can write the command text to a file , And then through -f filename The way to use .
-r：sed Basic regular expression syntax is used by default (BRE), Appoint -r Option, use extended regular expression syntax (ERE).
-i： Modify the read document directly , Instead of output to the terminal .
a： New row , a Followed by a string , These strings are added below the matching line .
c： Replacement row , c Followed by a string , These strings replace the matching lines .
d： Delete row , Delete the matching row .
i： Insert row , i Followed by a string , These strings are inserted on top of the matching line .
p： Print , Output some lines . Usually p And parameters -n Use it together , In this way, only the matching lines are output .
s： String substitution , Mainly used with regular expressions .
Explain that in this article " command " And " Command text " The difference between ：
Commands are abstract operations , such as a Indicates the new line ,d Indicates to delete the line .
Command text is a string composed of command and other information , Used to perform specific operations .
For example, add a line below the first line , The content is 'Hello world', The command text is ：'1a Hello world'
Another example is to delete the containing string 'Hello world' The line of , The command text is ：'/Hello world/d'
Common options and commands
explain ： In this example demo file test.txt Contains three lines of text , The content is ：
aa bb cc
demo file hello.txt Contains three lines of text , The content is ：
Hello world! Hello Jack! Hello China! Hello Nick!
Deleting lines requires the command d：
$ sed '1d' test.txt # Delete first line $ sed '$d' test.txt # Delete last line $ sed '1,2d' test.txt # Delete the first line to the second line $ sed '2,$d' test.txt # Delete the second line to the last line
Be careful , Finish the above command , We can only see the correct result on the command line terminal , and test.txt The document has not changed at all ：
If you want to modify the original file directly ( It's actually modifying the contents of the file first , And then save it to the original file ), Options required -i：
$ sed -i '1d' test.txt
Be careful , application -i There is no output on the command line after the option , But the source file has been updated .
a Command can add a new line below the matching line ：
$ sed '1a Hello world!' test.txt # Add a new line under the first line , The content is "Hello world!" $ sed '$a Hello world!' test.txt # Add a new line under the last line , The content is "Hello world!" $ sed '1,3a Hello world!' test.txt # In the first line , Add a line below the second line and a line below the third line , Content # by "Hello world!" 1,3 From the first line to the third line $ sed '1a Hello world!\nHello China!' test.txt # Adding more than one line at a time requires a newline character \n
-e Option is used to specify the command text , If there is only one command text -e Options can be omitted . How to specify multiple command texts requires the use of -e Options .
$ sed -e '1a xxx' -e '2a yyy' test.txt
i The command inserts a line above the matching line , The syntax is the same as the new line , It's just that the new line is on top of the specified line ( And a Distinction of command )：
Let's go through the options -e Added multiple command texts , But if you need to add more command text , Use options -e Not really . Because writing all the command text on the command line will cause maintenance difficulties . This option -f That comes in handy . We can write multiple command texts to a text file , And then through -f Option to reference .
Let's create a new one called commands The file of , Add three command text to it as follows ：
1i Hello world! 2i Hello world! 3i Hello world!
And then execute the command ：
$ sed -f commands test.txt
adopt -f Options ,commands All three command texts in the file are executed ！
Use c Command can easily replace the whole line ：
$ sed '1c Hello world!' test.txt # Replace the first line with "Hello world!" $ sed '1,3c Hello world!' test.txt # Replace the first to third lines with "Hello world!"
Be careful , The command in the figure above replaces three lines of text with one line of text ！
Unlike line substitution ,s The command replaces only the matched content ( It's usually a string ):
$ sed 's/Hello/Hi/' hello.txt # hold Hello Replace with Hi
One of the puzzles of the picture above is ： Why is there only the first one in the first line Hello Has been replaced by ？ The answer is sed By default, only the first match is replaced ！ So our second puzzle is ： If only the first match is replaced , So why the second and third lines Hello They've all been replaced ？ This problem involves sed How it works ,sed It's a text processing tool based on behavioral units ！ So the three lines in the picture are divided into three times , One line at a time . So in the second and third lines Hello For the bank, it's the first thing to match , It's right to be replaced .
To make a global replacement , You need to specify... In the command text g, Try the following command ：
$ sed 's/Hello/Hi/g' hello.txt # Put all the matches to Hello Replace with Hi
Two in the next line Hello It's all replaced .
We can also limit the number of rows that perform the replace operation ：
$ sed '2,3s/Hello/Hi/g' hello.txt # Replace only on the second and third lines
Of course, you can also delete unnecessary strings by substitution ：
$ sed 's/Hello//g' hello.txt # Delete string Hello
although / Is the most commonly used delimiter , But you can also use other characters . A simple example , When you want to be in linux When Path replacement is performed under , Use / It's not nice to be a delimiter ( You need a lot of escape characters ), At this point, a new delimiter is the best solution ：
In the figure above, we use semicolon as a delimiter to easily implement Path replacement .
Careful students may have noticed ,sed All operations are based on line positioning . That is to say, no matter what you are going to do , You have to find it first ( matching ) Target line . Even the simplest delete line '1d', You have to go to the first line , Then delete . So the only way to limit our play sed The ability factor is ： How to match to the desired line ？
The answer is to master the basic rules , Then practice more ！ -n Options and p Command is a good helper for us to practice .-n Options tell sed Output only those processed lines . such as sed '1a Hello world!' test.txt The command outputs four lines by default , application -n After that, only one line is output ：
p The order tells sed Output only those lines that match , For example, command ：
$ sed -n '1p' test.txt And orders sed -n '2,3s/Hello/Hi/gp' hello.txt
There are probably two types of rules for line matching ： Match by line number and match by regular expression .
Here are some examples of matching by line number ：
$ sed -n '1p' test.txt # Match the first line $ sed -n '$p' test.txt # Match the last line $ sed -n '2,3p' test.txt # Match the second and third lines $ sed -n '3,$p' test.txt # Match the third line and every line after the third line
Here's an example of matching through regular expressions ：
$ sed -n '/Hello/p' hello.txt
The default match is case sensitive , To ignore case, use I( Capital i)
$ sed -n '/hello/Ip' hello.txt
The following are some examples of operations performed after regular expression matching ：
$ sed '/Hello/d' hello.txt # Find the matching line , And delete $ sed '/Hello/a world!' hello.txt # Find the matching line , And add new lines below them $ sed 's/world/China/g' hello.txt # Put... In the line world Replace with China $ sed '/Hello/s/world/China/g' hello.txt # Find the matching line , Perform substitution in these lines
The following example uses regular expressions to replace the entire line ：
$ sed -i 's/^commonName.*/commonName = xxx/g' my.cnf $ sed -i 's/^subjectAltName.*/subjectAltName = DNS:xxx/g' my.cnf
sed It's a powerful text replacement tool , Especially when it needs to be used in automated scenarios . In this paper, combined with examples, a more detailed introduction of sed How to use , I hope to know about 、 Use sed Help .