Shell Three Musketeers–sed

Non-interactive editor that processes content one line at a time. (Streaming text editor)

parameter

① -f specifies a rule file. When the requirements are complex, there are many contents that need to be matched.

② -n prevents input line output

③ -r expands the regular rule, and the things that cannot be matched can now be matched.

④ -i The modified data will replace the original file

model

①s replacement

② g whole line (can also be a number, replace the number)

③ d delete

④ p print

⑤ a append

⑥ i insert

1. Replacement

cp /etc/passwd /opt

Replace root with ROOT: #The first two slashes are the content to be matched. You can use regular expressions. The middle of the next two slashes is the content to be replaced, which is plain text.

sed 's/root/ROOT/' /opt/passwd

sed 's/root/ROOT/ ; s/mail/MAIL/' /opt/passwd

Use script file:

vim state

script:

s/root/Root/
s/lp/LP/
s/mail/Mail/

Use script file:

sed -f state /opt/passwd

Save the output:

sed -f state /opt/passwd > newfile

Prevent input lines from being displayed automatically:

sed -n 's/root/ROOT/p' /opt/passwd

2. Delete

rm -rf /opt/*
cp /etc/passwd /opt

Delete the first line of the file

sed '1d' /opt/passwd

Delete lines 1 to 2 of the file

sed '1,2d' /opt/passwd

Delete row 2 to last row

sed '2,$d' /opt/passwd

Matches root, delete this line

sed '/root/d' /opt/passwd

Match the root line to a certain line

sed '/root/,2d' /opt/passwd

Delete odd rows

sed '1~2d' /opt/passwd

Delete even rows

sed '0~2d' /opt/passwd

sed can use instructions to delete empty lines in files

sed '/^$/d' /opt/passwd

Remove comments and empty lines from the sshd configuration file

sed '/^#.*/d;/^$/d' /opt/passwd

3. Replacement

rm -rf /opt/*
cp /etc/passwd /opt

The option -i will cause sed to replace the original file with the modified data

sed -i 's/bin/BIN/' /opt/passwd

The g flag enables sed to perform global substitutions

sed 's/root/admin/g' /opt/passwd

Ignore case substitutions

sed 's/Root/admin/gi' /opt/passwd

The g mark allows sed to match characters after the Nth time to be replaced.

echo "thisthisthisthis" | sed 's/this/THIS/2g'

The delimiter in sed can be replaced with other characters, because the s flag will consider the following character as a delimiter

sed 's:adm:text:' /opt/passwd

sed 's|adm|text|' /opt/passwd

Since it is more dangerous when using the -i parameter, when we use the i parameter and add .bak at the end, a backup file will be generated.

sed -i.bak 's/root/admin/' /opt/passwd

If sed is used in a script, it is inevitable to call variables, so the following method can be used to call variables, that is, ‘ ‘ is replaced by ” “

text=hello
echo "hello world" | sed "s/$text/HELLO/"

Replace the contents on the left and right sides of the equal sign

sed "s/\(.*\) = \(.*\)/\2 = \1/" /opt/passwd

4. Append and insert

rm -rf /opt/*
cp /etc/passwd /opt

Insert content in the next line matching the line starting with bin

sed '/^bin/a\hello nihao/' /opt/passwd

Insert content one line above the line that matches the line starting with bin

sed '/^bin/i\hello nihao/' /opt/passwd