编程知识 cdmana.com

One Linux command per day (3): sed

recommend : One a day Linux command (2):od

Function introduction

sed Is a flow editor , It's also a great tool for text processing , When using more powerful processing with regularization , Store the currently processed rows in a temporary buffer , be called “ Mode space ”, Then use sed The command handles the contents of the buffer , When finished, output to the screen , Next line . The contents of the document have not changed , Unless used -i Options .sed Mainly used to edit one or more files , Simplify the repeated operation of the file or used to write conversion program, etc .

sed Same function awk similar , The difference lies in ,sed Simple , The function of column processing is worse ,awk Complex functions , The function of column processing is more powerful .

Command format

 sed [options] 'command' file(s)
 sed [options] -f scriptfile file(s)

Common parameters

-e # Processing input text files with specified instructions 
-n # Cancel default output ( If and p Commands used at the same time will only print lines that have changed )
-h # help 
-V # Display version information 

Common actions

a # Insert text below the current line 
i # Insert text above the current line 
c # Change the selected line to a new text 
d # Delete , Delete the selected line 
D # Delete the first line of the template block  
s # Replace specified characters 
h # Copy the contents of the template block to a buffer in memory 
H # Append the contents of the template block to the buffer in memory 
g # Get the contents of the memory buffer , And replace the text in the current template block  
G # Get the contents of the memory buffer , And appended to the current template block text  
l # List cannot print a list of characters 
n # Read the next input line , Use the next command to process the new line instead of the first command 
N # Append the next input line to the template block and embed a new line between the two , Change the current line number 
p # Print matching lines 
P #( Capitalization ) Print the first line of the template 
q # sign out Sed
b #lable  Branch to where the script is marked , If the branch does not exist, branch to the end of the script 
r #file  from file Chinese reading line 
t #label if Branch , Start with the last line , Once conditions are met or T,t command , Will result in branching to a labeled command , Or to the end of the script 
T #label  Wrong Branch , Start with the last line , Once something goes wrong or T,t command , Will result in branching to a labeled command , Or to the end of the script 
w #file  Write and append template blocks to file At the end of **
W #file  Write and append the first line of the template block to file At the end of **
! # Indicates that the following command will work on all unselected lines ** 
= # Print current line number **
# # Extend the comment before the next line break **

Sed Replace the command

g # Denotes an in line total substitution ( Global replacement fit s Command to use )
p # Means print line  
w  # To write a line to a file  
x # Represents the text in the swap template block and the text in the buffer  
y # To translate one character into another ( But not for regular expressions ) 
1  # Substring match mark  
& # Matched string token 

Sed Regular

^ # Match line begins  
$ # End of match line 
. # Any character that matches a non newline character 
* # matching 0 Characters or more 
[] # Match a specified range of characters 
[^]  # Match a character that is not in the specified range  
(..) # Match the strings 
&  # Save search characters to replace other characters 
< # Match the beginning of the word 
>  # Match the end of the word 
x{m} # Repeat characters x,m Time 
x{m,} # Repeat characters x, At least m Time  
x{m,n} # Repeat characters x, At least m Time , Not more than n Time 

Sed Common examples

1、 Replace operation

echo "hello world" |sed 's/ /-/1g'
hello-world 
# Start with the first space and replace it globally with -, It's just that there's only one space in the text 

2、 Delete operation

sed '/^$/d' filename # Delete blank lines 

sed '2d' filename # Delete the second line 

sed '2,$d' filename # Delete the second line until the end of all lines 

sed '$d' filename # Delete last line 

sed '/^test/'d filename # Delete with test Start line 

3、 Match and replace

echo "hello world" |sed 's/w+/[&]/g'
[hello] [world]
echo "hello world" |sed 's/w+/"&"/g'
"hello" "world"
#w+ Match every word ,& Represents the matched string 

echo AAA bbb |sed 's/([A-Z]+) ([a-z]+)/[2] [1]/'
[bbb] [AAA]
# Substring matching replacement 

4、 Selected range

sed -n '/= 0/,/max/p' svnserve.conf
#min-encryption = 0
#max-encryption = 256

# All in =0 To max The lines in the range will be printed out 

5、sed Multi point editing function (-e)

[root@centos001 ~]#cat -n test 
1  this is a test file
2  welcome
3  to
4  here
5  hello WORLD
6
7  linux centos6.8
8  redhat

sed -e '2,6d' -e 's/linux centos6.8/Linux Centos6.8/' test
this is a test file
Linux Centos6.8
redhat
# If the two commands function the same , Then you need to use the following parameters 

sed --expression='s/linux centos6.8/Linux Centos6.8/' --expression='s/to/TO/' test**
this is a test file
welcome
TO
here
hello WORLD
Linux CenTOs6.8
redhat

6、 Read in and write in

[root@centos001 ~]#cat test1
welcom 
to 
here

[root@centos001 ~]#sed '/here/r test1' test
this is a test file
welcome
to
here
#welcom
to
here#
hello WORLD
linux centos6.8
redhat

# take test1 The contents of the file read show all matches here The back of the line 
sed -n '/centos6.8/w test2' test

[root@centos001 ~]#cat test2
linux centos6.8

# take test The file matches to centos6.8 All the lines of are written to test2 In file , The file can not exist .
# If the file exists , You'll be redirected, not appended 

7、 Add and insert

[root@centos001 ~]#sed '/^l/a2017-08-08' test2
linux centos6.8
2017-08-08
# Match with l Add to the beginning line 2017-08-08

[root@centos001 ~]#sed '1a2017-08-08' test2
linux centos6.8
2017-08-08
# Add... After the first line 2017-08-08

[root@centos001 ~]#sed '/^l/i2017-08-08' test2
2017-08-08
linux centos6.8
# Match with l Insert... Before the beginning line 2017-08-08
####### The above operation will not change the file content ################

[root@centos001 ~]#sed -i '/^l/i2017-08-08' test2
[root@centos001 ~]#cat test2
2017-08-08
linux centos6.8

8、 Other command examples

[root@centos001 ~]#cat -n test2
 1 2017-08-08
 2 linux centos6.8
 3 08
 4
 5 test

[root@centos001 ~]#**sed '/08/{ n; s/l/L/; }' test2
2017-08-08
Linux centos6.8
08
test
# If 08 Jump to the next line if you match , Lower case l Replace with uppercase , Notice that the third line is also matched to 
# But the latter condition is not satisfied , All not replaced 

[root@centos001 ~]#sed '1,4y/8/9/' test2
2017-09-09
linux centos6.9
09
test
# take 1 to 4 Line up all the numbers 8 Replace with 9

[root@centos001 ~]#**sed '1q' test2**
2017-08-08
# Print the first line and exit 

9、 Print odd or common even lines

[root@centos001 ~]#sed -n 'p;n' test2
20170808
08

[root@centos001 ~]#sed -n 'n;p' test2
linux centos6.8
test

[root@centos001 ~]#sed -n '1~2p' test2
20170808
08

[root@centos001 ~]#sed -n '2~2p' test2
linux centos6.8
test

10、 Print the next line of the matching string line

[root@centos001 ~]#sed -n '/linux/{n;p}' test2
08
[root@centos001 ~]#awk '/linux/{getline; print}' test2
08

This article is from WeChat official account. - The road of technology for migrant workers (jishuroad)

The source and reprint of the original text are detailed in the text , If there is any infringement , Please contact the yunjia_community@tencent.com Delete .

Original publication time : 2020-12-23

Participation of this paper Tencent cloud media sharing plan , You are welcome to join us , share .

版权声明
本文为[Migrant worker brother]所创,转载请带上原文链接,感谢
https://cdmana.com/2020/12/20201224104023949j.html

Scroll to Top