Sed is the abbreviation of stream editor, which is mainly used to process standard output or files.
Grammar:
stdout | sed [option] "pattern command"
sed [option] "pattern command" file1
<!– more –>
Common options
#- n only print silent pattern matching lines, not the original lines
#P is the print command
➜ sed '/hello/p' helloWorld.sh
#!/bin/bash
HELLO bash
echo "hello world"
echo "hello world"
➜ sed -n '/hello/p' helloWorld.sh
echo "hello world"
#- e appends a set of edit commands
➜ sed -n -e '/hello/p' -e '/HELLO/p' helloWorld.sh
HELLO bash
echo "hello world"
#- f saves all editing commands in a file and is suitable for complex editing operations
➜ cat edit.sed
/hello/p
➜ sed -n -f edit.sed hello.md
#- E (or - R) supports extended regular expressions
➜ sed -n -E '/hello|HELLO/p' helloWorld.sh
HELLO bash
echo "hello world"
#- I modify the contents of the source file directly
#S is the replace command
#This is it helloWorld.sh All hello in the file is changed to hello123
sed -n -i 's/hello/hello123/g' helloWorld.sh
Pattern matching
Matching pattern | explain |
---|---|
10command |
Line 10 |
10,20command |
Lines 10 to 20 |
10,+5command |
Lines 10 to 16 |
/pattern1/command |
Match the row corresponding to pattern1 |
/pattern1/,/pattern2/command |
Start from the row corresponding to pattern1 and go to the row of pattern2 |
10,/pattern1/command |
Start at line 10 and go to pattern 1 |
/pattern1/,10command |
Start with the line corresponding to pattern1 and go to line 10 |
Common editing commands
-
query
p
Print matching content
-
increase
a string
Add after linei string
Add before liner file
Read from an external file and append after the matching linew newfile
Write the matching line to the external file
-
delete
d
delete
-
modify
s/old/new
Replace the first old in the line with news/old/new/g
Replace all old in the line with news/old/new/2g
Replace all old from line 2 to the end of the file with news/old/new/ig
Replace all old in the row with new, ignoring case
Example:
#Delete lines that start with sys and end with / SBIN / nologin
➜ sed -i '/^sys.*\/sbin\/nologin$/d' passwd_bak
#Delete comment line and empty line
sed -i '/[:blank:]*#/d;/^$/d' passwd_bak
#Find the line that begins with vagrant and append to the next line
➜ sed - I '/ ^ vagrant / a this is an additional line' passwd '_ Bak
#Replace all roots with root123
➜ sed -i 's/root/root123/ig' passwd_bak
#Append at the end of all lines that start with sys and end with nologin_ Six hundred and sixty-six
#Where & represents the content matched by the previous regularities
➜ sed -i 's/^sys.*nologin$/&_666/g' passwd_bak
#Start all with sys and nologin_ Change the line at the end of 666 to read
#With sys_ Beginning with_ End of 777, where "1" indicates the middle part of the match in the preceding brackets
➜ sed -i 's/^sys\(.*\)nologin_666$/SYS__777/g' passwd_bak
#Change all sys from lines 1 to 10 to sys
➜ sed -i '1,10s/sys/SYS/ig' passwd_bak
#Statistics my.cnf Number of sub configuration items of mysqld in the file
#Sed finds the row from [mysqld] to the next [. *]
#Grep - V filters comments, empty lines, and [. *] lines
#WC - L counts the last number of rows
sed -n "/^\[mysqld\]$/,/^\[.*\]$/p" /etc/my.cnf | grep -Ev '^$|[#;]|^\[.*' | wc -l
be careful: double quotation marks are recommended if there are variables in the match pattern, such assed -i "s/$OLD_STR/$NEW_STR/g" passwd_bak