preface

Sed, full name for Stream Editor, is a stream editor for text processing that supports regular expressions. Sed processes text one line at a time

Pay attention to the public account, communicate together, wechat search: sneak forward

Sed grammar

  • The sed command processes content in the schema space rather than directly processing file content. If I is added, the content of the file can be modified directly
  • Example:Sed -i 's/ original string/new string /' /home/test.txt

Sed [-nefr parameter] [function] [filePath]

Options and Parameters describe
-n Use Silent mode. In normal sed usage, the input data is output to the screen. If there is a p flag, the sed line will be listed
-e Directly on the command line interface for sed action editing, execute multiple sliver commands
-f -f filename Executes the sed action in the script file
-r Sed actions support the syntax of extended regular expressions
-i Modify the contents of the read file directly
  • The -n option is set to quiet mode with the -n option, that is, no default print is printed, unless the print p option is specified in the subcommand, only lines that match changes are printed
---- print both lines ---- server11:~/test# echo -e 'hello \n world' | sed 's/hello/csc/'CSC world ---- did not print a line ----- server11:~/test# echo -e 'hello \n world' | sed -n 's/hello/csc/'---- a matching line ----- server11:~/test is printed# echo -e 'hello \n world' | sed -n 's/hello/csc/p'
csc 
Copy the code
  • Option -e, multiple sliver commands operate consecutively
echo -e 'hello world' | sed -e 's/hello/csc/' -e 's/world/lwl/'Results: CSC LWLCopy the code
  • The -i option directly modifies the contents of the read file
server11:~/test # cat file.txt 
hello world
server11:~/test # sed 's/hello/lwl/' file.txt 
lwl world
server11:~/test # cat file.txt              Hello world ---- You can modify the file contents by adding the I parameter ---- server11:~/test# sed -i 's/hello/lwl/' file.txt 
lwl world
server11:~/test # cat file.txt              
lwl world
Copy the code
  • The -f option executes the file script
Sed. Script Script content: s/hello/ CSC/s/world/ LWL / ------ echo"hello world"| sed -f sed. Script results: CSC LWLCopy the code
  • -r. By default, only basic regular expressions are supported. To support extended regular expressions, -r is required
echo "hello world" | sed -r 's/(hello)|(world)/csc/g'
csc csc
Copy the code

[n1[,n2]] function or /{pattern}/function

  • N1, n2: Optional, generally means “select the number of lines to perform the action”, for example, iffunctionIs required between 10 and 20 lines, is10, 20 (function)
  • If you need a regular expression to match a string, use it/{pattern}/matching
Test. TXT content111
222
333
444----- delete non - first2The first3All lines between rows ---------- server11:~# sed -i '2, 3! d' test.txt 
server11:~ # cat test.txt
222
333------ regular expression matches ------------ server11:~# echo 'clswcl.txt' | sed -nr '/.*/p'
clswcl.txt  // /{pattern}/ = /.*/
Copy the code

Function has the following options

function describe
a New: a can be followed by a string, which will appear on a new line (currently the next line)
i Insert: I can be followed by a string, which will appear on a new line (currently the previous line)
c Substitution: c can be followed by strings that replace lines between n1 and n2
d Delete: Because it is delete, so the D is usually not followed by anything
p Print: Also to print the selected data. Usually p is run with the sed-n argument
s Replace: can directly replace the work! Often the s action can be paired with a regular expression! For example, 1,20 s/old/new/g
  • Function: -a, insert a new line after the line
sed -i '/ specific string /a newline string ' fileName
Copy the code
  • Function: -i, insert a new line before the line
sed -i '/ specific string/I newline string ' fileName
Copy the code
  • Function: -c, modify the specified content line
sed -i '/ specific string /c CSC LWL ' fileName 
Copy the code
  • Function: -d, delete the specified string
sed -i '/ specific string /d' fileName 
Copy the code

Sed s subcommand: s/{pattern}/{replacement}/{flags}

  • If {pattern} contains regular expressions, add -r
  • If {pattern} has groupings, the “\n” in {replacement} represents the NTH group and the “&” represents the entire matching string. See examples for details
  • The flags parameters are as follows
flags describe
n It can be 1-512, representing the NTH occurrence of the substitution
g Global change
p Prints the contents of the schema space
w file Write to a file called file
  • The sample
server11:~ # echo -e 'hello 1112 world' | sed -r 's/([a-z]+)( [0-9]+ )([a-z]+)/&/'  
hello 1112 world
server11:~ # echo -e 'hello 1112 world' | sed -r 's/([a-z]+)( [0-9]+ )([a-z]+)/\3\2\1/' 
world 1112 hello
Copy the code

Refer to the article

  • Sed -i command description and introduction