Skills: sed tutorial - linux commands

Posted by drummer101 on Mon, 31 Jan 2022 23:22:11 +0100

preface

sed, whose full name is stream editor, is a stream editor for text processing and supports regular expressions. sed processes text one line at a time

Pay attention to the official account and exchange with each other. Search by WeChat: Sneak ahead.

sed syntax

  • Example: sed - I's / original string / new string / '/ home / test txt
  • The sed command deals with the contents of the schema space rather than directly dealing with the contents of the file. If the parameter i is added, the file content is modified directly

sed [-nefr parameter] [action] [file]

Options and parametersdescribe
-nUse silent mode. In the general sed usage, the input data will be output to the screen. However, if the - n parameter is added, it will not be displayed. If the p flag is followed, the row specially processed by sed will be listed
-eEdit the sed action directly on the command line interface and execute multiple subcommands
-fWrite the sed action in a file, - f filename executes the sed action of the script file
-rsed's actions support the syntax of extended regular expressions
-iDirectly modify the read file content
  • Option - n is set to quiet mode after adding the - N option, that is, the default printing information will not be output. Unless the print p option is specially specified in the subcommand, only the lines matching the modification will be printed
---- Print both lines ----
server11:~/test # echo -e 'hello \n world' | sed 's/hello/csc/'
csc 
 world
---- Not a single line was printed -----
server11:~/test # echo -e 'hello \n world' | sed -n 's/hello/csc/'
---- Matched lines printed -----
server11:~/test # echo -e 'hello \n world' | sed -n 's/hello/csc/p'
csc 
  • Option - e, multiple subcommands operate continuously
echo -e 'hello world' | sed -e 's/hello/csc/' -e 's/world/lwl/'
result: csc lwl
  • Option - i, directly modify 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
---- Add parameters i You can modify the file content directly----
server11:~/test # sed -i 's/hello/lwl/' file.txt 
lwl world
server11:~/test # cat file.txt              
hello world
  • Option - f, execute file script
sed.script Script content:
s/hello/A/
s/world/B/
------
echo "hello world" | sed -f sed.script
 result: A B
  • Option - r, support regular expressions
echo "hello world" | sed -r 's/(hello)|(world)/csc/g'
csc csc

Action: [n1[,n2]] function

n1, n2: optional, generally stands for "select the number of rows to perform the action". For example, if the action needs to be performed between 10 and 20 rows, it is expressed as 10,20 [function]

test.txt content 
111
222
333
444
----- Delete all lines not between line 2 and line 3 ----------
server11:~ # sed -i '2,3!d' test.txt 
server11:~ # cat test.txt
222
333

function has the following options

functiondescribe
aNew: a can be followed by strings, and these strings will appear on a new line (the current next line)
iInsert: i can be followed by strings, and these strings will appear on a new line (the current previous line)
cSubstitution: c can be followed by strings, which can replace the lines between N1 and N2
dDelete: because it is deleted, there is usually nothing after d
pPrint: also print a selected data. Usually p runs with the parameter sed -n
sSubstitution: you can directly carry out the work of substitution! Usually this s action can be matched with regular expressions! For example: 1,20 s/old/new/g
  • function: - A, insert a new line after the line
sed -i '/Specific string/a New line string' fileName
  • function: - i, insert a new line before the line
sed -i '/Specific string/i New line string' fileName
  • function: - c, modify the specified content line
sed -i '/Specific string/c csc lwl' fileName 
  • function: - d, delete a specific string
sed -i '/Specific string/d' fileName 

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

  • If {pattern} contains regular expressions, you need to add - r

  • If there is a group in {pattern}, "\ n" in {replacement} represents the nth group and "&" represents the whole matching string. See the ex amp le for details

  • The parameters of flags are as follows

flagsdescribe
nIt can be 1-512, indicating the replacement of the n-th occurrence
gGlobal change
pPrint the contents of the mode space
w fileWrite to a file
  • Examples
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

Reference articles

Topics: Java Linux Programmer sed