当前位置:网站首页>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
边栏推荐
- JS regular matching first assertion and last assertion
- How to use DBA_ hist_ active_ sess_ History analysis database history performance problems
- fdfs启动
- 模仿扇贝短文阅读页面
- [ES6 quick start]
- [Lombok quick start]
- CentOS8搭建PHP8.0.3运行环境
- Leak detection and vacancy filling (III)
- 批量修改/批量更新数据库某一个字段的值
- 【代码解析(1)】Communication-Efficient Learning of Deep Networks from Decentralized Data
猜你喜欢
随机推荐
virtio 与vhost_net介绍
leetcode刷题之二进制求和
[OSS file upload quick start]
Decentralized Collaborative Learning Framework for Next POI Recommendation
【Shell脚本练习】将新加的磁盘批量添加到指定的VG中
Thinkphp5 -- object (think \ response \ JSON) to array
[MySQL basics] data export and import permissions and local_ Infile parameter
【代码解析(3)】Communication-Efficient Learning of Deep Networks from Decentralized Data
The arithmetic square root of X in leetcode
[MySQL basics] startup options and configuration files
CentOS8搭建PHP8.0.3运行环境
Leak detection and vacancy filling (IX) -- Procedure
Leak detection and vacancy filling (IV)
The time format is incorrect, and an error is reported when running the SQL file
[Lombok quick start]
Kids and COVID: why young immune systems are still on top
用Future与CountDownLatch实现多线程执行多个异步任务,任务全部完成后返回结果
你应该知道的 JVM 基础知识
file_ get_ Two solutions to content accessing SSL errors
openvswitch 编译安装

![[ES6 quick start]](/img/9e/4c4be5907c1f7b3485c2f4178b9150.png)



![[OSS file upload quick start]](/img/db/9043d1df0163a7154bebac8e79097f.png)



