当前位置:网站首页>Use the SED command to process text efficiently
Use the SED command to process text efficiently
2022-04-23 06:59:00 【Sebastien23】
Use sed Command to process text efficiently
sed The format of the command is
sed [options] 'command' file(s) # With command Instructions to process file(s)
sed [options] -f script file(s) # With script Script to handle file(s)
among ,options Mainly used in the following :
-i: Modify the original document directly , Instead of output to the screen
sed -i 's/test/TEST' file01
-n: namely --quiet
, Only the processed results are displayed , Not the whole content of the file
And p Instructions can be used together to print only the lines where the replacement occurs .
sed -n 's/test/TEST/p' file01
-e : namely --expression
, Allow multiple commands to be executed on the same line
sed -e '1,5d' -e 's/test/check/' file01
delimiter
sed In general use /
As a delimiter , You can also use other symbols . When the delimiter appears in the string to match , You need to use a backslash to escape .
# take test Replace all with TEST
sed 's/test/TEST/g' file01
sed 's#test#TEST#g' file01
sed 's:test:TEST:g' file01
sed 's|test|TEST|g' file01
# take /bin Replace all with /usr/bin
sed 's/\/bin/\/usr\/bin/g' file01
sed 's#/bin#/usr/bin#g' file01
Print :p
And -n
Use it together , Print only rows that match
# Print match to test The line of
sed -n '/test/p' file01
# Print page 99 Everything after the line
sed -n '100,$p' file02
Read :r
Read file02 The content in , Add to file01 Match to test After each line of the string
sed '/test/r file02' file01
write in :w
take file01 Match to test All lines of the string , Write to file02 in ( Will overwrite the original contents of the file )
sed '/test/w file02' file01
Delete :d
# Delete blank lines
sed '/^$/d' file01
# Delete the first 2 That's ok
sed '2d' file01
# Delete the first 2 Line to all lines at the end of the file
sed '2,$d' file01
# Delete last line
sed '$d' file01
# Delete test All the lines at the beginning
sed '/^test/d' file01
Replace :s
Replace Every line Match to first character string :
sed 's/test/TEST/' file01
Replace Every line Match to all character string :
sed 's/test/TEST/g' file01
matching
Common matching rules include the following :
^
: Match line begins , Such as :/^sed/
Match all to sed Beginning line .
$
: End of match line , Such as :/sed$/
Match all to sed The line at the end .
.
: matching One Any character that is not a newline character , Such as :/s.d/
matching s Followed by an arbitrary character , And finally d.
*
: matching 0 One or more character , Such as :/*sed/
Matching all templates is one or more spaces followed by sed The line of .
\+
: matching 1 One or more character , Such as :/se\+d/
matching s and d At least one letter in the middle e String .
[]
: Match a specified range of characters , Such as /[sS]ed/
matching sed and Sed.
[^]
: Match a character that is not in the specified range , Such as :/[^A-RT-Z]ed/
Match does not contain A-R and T-Z The beginning of a letter , Following the ed The line of .
\(..\)
: Match the strings , Save matching characters , Such as s/\(love\)able/\1rs
,loveable Replaced with lovers.
\1
: Substring match mark , And \(..\)
In combination with .\1
Represents the first substring matched , And so on ,\2
Represents the second substring that matches ,….
&
: Save search characters to replace other characters , Such as s/love/ **&** /
,love That's it **love**
.
\<
: Match the beginning of the word , Such as :/\<love/
The match consists of love The line at the beginning of the word .
\>
: Match the end of the word , Such as /love\>/
The match consists of love The lines of the ending words .
x\{m\}
: Repeat characters x,m Time , Such as :/0\{5\}/
Matching inclusion 5 individual 0 The line of .
x\{m,\}
: Repeat characters x, At least m Time , Such as :/0\{5,\}/
Match at least 5 individual 0 The line of .
x\{m,n\}
: Repeat characters x, At least m Time , Not more than n Time , Such as :/0\{5,10\}/
matching 5~10 individual 0 The line of .
[:space:]
: Match single whitespace , Including Spaces 、 Box drawings, etc .
Example :
A simple example
# \w\+ Match every word ,"&" Put double quotation marks on the matched string
echo this is a test line | sed 's/\w\+/"&"/g'
"this" "is" "a" "test" "line"
# Match two English letter strings , Then change the position.
echo aaa BBB | sed 's/\([a-z]\+\) \([A-Z]\+\)/\2 \1/'
BBB aaa
# \([a-z]\+\) Match to aaa, Marked as \1
# \([A-Z]\+\) Match to BBB, Marked as \2
A complex example
cat file03
# logfile /var/log/nscd.log
# threads 4
enable-cache group yes
enable-cache hosts yes
# Remove the comment on the first line ( The delimiter is /)
sed -i 's/^#\([[:space:]]*logfile[[:space:]]*\/var\/log\/nscd.log\)/\1/g' file03
# Remove the comment on the first line ( The delimiter is |)
sed -i 's|^#\([[:space:]]*logfile[[:space:]]*/var/log/nscd.log\)|\1|g' file03
# among ^# Said to # Beginning line
# [[:space:]]* Represents a match 0 One or more blanks
# \(...\) Indicates that the matching substring is reserved ,\1 Represents the first string matched
cat file03
logfile /var/log/nscd.log
# threads 4
enable-cache group yes
enable-cache hosts yes
# Put the third line of yes Replace with no
sed -i 's/\([[:space:]]*enable-cache[[:space:]]*group[[:space:]]*\)yes/\1no/g' file03
cat file03
logfile /var/log/nscd.log
# threads 4
enable-cache group no
enable-cache hosts yes
References:
【1】https://g-ghy.github.io/linux-command/c/sed.html
【2】https://www.twle.cn/c/yufei/sed/sed-basic-regular-expressions.html
版权声明
本文为[Sebastien23]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230557415948.html
边栏推荐
- The time format is incorrect, and an error is reported when running the SQL file
- JS implementation of web page rotation map
- [MySQL basics] startup options and configuration files
- JQ序列化后PHP后台解析
- 阅读笔记:FedGNN: Federated Graph Neural Network for Privacy-Preserving Recommendation
- 使用百度智能云人脸检测接口实现照片质量检测
- rdam 原理解析
- Web登录小案例(含验证码登录)
- 【代码解析(4)】Communication-Efficient Learning of Deep Networks from Decentralized Data
- SQL学习|集合运算
猜你喜欢
随机推荐
多线程
【代码解析(6)】Communication-Efficient Learning of Deep Networks from Decentralized Data
虚拟环境中使用jupyter notebook
[fish in the net] ansible awx calls playbook to transfer parameters
Each traversal usage of tp6
[MySQL basics] startup options and configuration files
TypeScript(下)
New formdata() when importing files
ES6 specification details
柯里化实现函数连续调用计算累加和
fdfs启动
Curry realization of function continuous call calculation and accumulation
redis 实践笔记和源码分析
关于注解1
Number of stair climbing methods of leetcode
ebfp编程常用API介绍
volatile 关键字的三大特点【数据可见性、指令禁止重排性、不保证操作原子性】
Leak detection and vacancy filling (II)
Basic concepts of database: OLTP / OLAP / HTAP, RPO / RTO, MPP
mysql中sum (if)_mysql 中sum (if())