Sed of three swordsmen in shell text processing


Sed is the abbreviation of stream editor, which is mainly used to process standard output or files.


  • 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'

HELLO bash
echo "hello world"
echo "hello world"
➜  sed -n '/hello/p'
echo "hello world"

#- e appends a set of edit commands
➜  sed -n -e '/hello/p' -e '/HELLO/p'
HELLO bash
echo "hello world"

#- f saves all editing commands in a file and is suitable for complex editing operations
➜  cat edit.sed
➜  sed -n -f edit.sed

#- E (or - R) supports extended regular expressions
➜  sed -n -E '/hello|HELLO/p'
HELLO bash
echo "hello world"

#- I modify the contents of the source file directly
#S is the replace command
#This is it  All hello in the file is changed to hello123
sed -n -i 's/hello/hello123/g'

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

    • pPrint matching content
  • increase

    • a stringAdd after line
    • i stringAdd before line
    • r fileRead from an external file and append after the matching line
    • w newfileWrite the matching line to the external file
  • delete

    • ddelete
  • modify

    • s/old/newReplace the first old in the line with new
    • s/old/new/gReplace all old in the line with new
    • s/old/new/2gReplace all old from line 2 to the end of the file with new
    • s/old/new/igReplace all old in the row with new, ignoring case


#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

Recommended Today

PHP 12th week function learning record

sha1() effect sha1()Function to evaluate the value of a stringSHA-1Hash. usage sha1(string,raw) case <?php $str = “Hello”; echo sha1($str); ?> result f7ff9e8b7bb2e09b70935a5d785e0cc5d9d0abf0 sha1_file() effect sha1_file()Function calculation fileSHA-1Hash. usage sha1_file(file,raw) case <?php $filename = “test.txt”; $sha1file = sha1_file($filename); echo $sha1file; ?> result aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d similar_text() effect similar_text()Function to calculate the similarity between two strings. usage similar_text(string1,string2,percent) case […]