当前位置:网站首页>使用sed命令来高效处理文本
使用sed命令来高效处理文本
2022-04-23 05:59:00 【Sebastien23】
sed命令的格式为
sed [options] 'command' file(s) # 以command指令来处理file(s)
sed [options] -f script file(s) # 以script脚本来处理file(s)
其中,options主要常用到下面几种:
-i:直接修改原文件,而不是输出到屏幕
sed -i 's/test/TEST' file01
-n:即--quiet
,仅显示处理后的结果,而不是文件的全部内容
与p指令一起使用可以只打印发生替换的行。
sed -n 's/test/TEST/p' file01
-e :即--expression
,允许在同一行里执行多条命令
sed -e '1,5d' -e 's/test/check/' file01
定界符
sed一般使用/
作为定界符,也可以使用其他符号。定界符出现在要匹配的字符串中时,需要用反斜杠进行转义。
# 将test全部替换为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
# 将/bin全部替换为/usr/bin
sed 's/\/bin/\/usr\/bin/g' file01
sed 's#/bin#/usr/bin#g' file01
打印:p
与-n
一起使用,只打印匹配到的行
# 打印匹配到test的行
sed -n '/test/p' file01
# 打印第99行以后所有内容
sed -n '100,$p' file02
读取:r
读取file02中的内容,添加到file01中匹配到test字符串的每一行之后
sed '/test/r file02' file01
写入:w
将file01中匹配到test字符串的所有行,写入到file02中(会覆盖文件中原有的内容)
sed '/test/w file02' file01
删除:d
# 删除空白行
sed '/^$/d' file01
# 删除第2行
sed '2d' file01
# 删除第2行到文件末尾的所有行
sed '2,$d' file01
# 删除最后一行
sed '$d' file01
# 删除test开头的所有行
sed '/^test/d' file01
替换:s
替换每一行中匹配到的第一个字符串:
sed 's/test/TEST/' file01
替换每一行中匹配到的所有字符串:
sed 's/test/TEST/g' file01
匹配
常用的匹配规则包括以下内容:
^
:匹配行开始,如:/^sed/
匹配所有以sed开头的行。
$
:匹配行结束,如:/sed$/
匹配所有以sed结尾的行。
.
:匹配一个非换行符的任意字符,如:/s.d/
匹配s后接一个任意字符,最后是d。
*
:匹配0个或多个字符,如:/*sed/
匹配所有模板是一个或多个空格后紧跟sed的行。
\+
:匹配1个或多个字符,如:/se\+d/
匹配s和d中间至少有一个字母e的字符串。
[]
:匹配一个指定范围内的字符,如/[sS]ed/
匹配sed和Sed。
[^]
:匹配一个不在指定范围内的字符,如:/[^A-RT-Z]ed/
匹配不包含A-R和T-Z的一个字母开头,紧跟ed的行。
\(..\)
:匹配子串,保存匹配的字符,如s/\(love\)able/\1rs
,loveable被替换成lovers。
\1
:子串匹配标记,与\(..\)
配合使用。\1
表示匹配到的第一个子串,以此类推,\2
表示匹配到的第二个子串,…。
&
:保存搜索字符用来替换其他字符,如s/love/ **&** /
,love这成**love**
。
\<
:匹配单词的开始,如:/\<love/
匹配包含以love开头的单词的行。
\>
:匹配单词的结束,如/love\>/
匹配包含以love结尾的单词的行。
x\{m\}
:重复字符x,m次,如:/0\{5\}/
匹配包含5个0的行。
x\{m,\}
:重复字符x,至少m次,如:/0\{5,\}/
匹配至少有5个0的行。
x\{m,n\}
:重复字符x,至少m次,不多于n次,如:/0\{5,10\}/
匹配5~10个0的行。
[:space:]
:匹配单个空白符,包括空格、制表符等。
示例:
一个简单的示例
# \w\+匹配每一个单词,"&"给匹配到的字符串加上双引号
echo this is a test line | sed 's/\w\+/"&"/g'
"this" "is" "a" "test" "line"
# 匹配两个英文字母串,然后调换位置
echo aaa BBB | sed 's/\([a-z]\+\) \([A-Z]\+\)/\2 \1/'
BBB aaa
# \([a-z]\+\)匹配到aaa,标记为\1
# \([A-Z]\+\)匹配到BBB,标记为\2
一个复杂的示例
cat file03
# logfile /var/log/nscd.log
# threads 4
enable-cache group yes
enable-cache hosts yes
# 去掉第一行的注释(定界符为/)
sed -i 's/^#\([[:space:]]*logfile[[:space:]]*\/var\/log\/nscd.log\)/\1/g' file03
# 去掉第一行的注释(定界符为|)
sed -i 's|^#\([[:space:]]*logfile[[:space:]]*/var/log/nscd.log\)|\1|g' file03
# 其中^#表示以#开头的行
# [[:space:]]*表示匹配0个或多个空白符
# \(...\)表示保留匹配子串,\1表示匹配到的第一个字串
cat file03
logfile /var/log/nscd.log
# threads 4
enable-cache group yes
enable-cache hosts yes
# 将第三行的yes替换为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://blog.csdn.net/Sebastien23/article/details/122843146
边栏推荐
- 【代码解析(3)】Communication-Efficient Learning of Deep Networks from Decentralized Data
- JS realizes modal box dragging
- ES6规范详解
- thinkphp5 ---- object(think\response\Json)转数组
- leetcode之爬楼梯方法数
- Solution to page cache problem (use with caution)
- ubuntu下搭建mysql环境 & 初识SQL
- 修改Jupyter Notebook样式
- tc ebpf 实践
- offset和client獲取dom元素比特置信息
猜你喜欢
ebfp编程常用API介绍
2021-09-18
LeetCode刷题|368最大整除子集(动态规划)
.NET类型转移
Kids and COVID: why young immune systems are still on top
Kids and COVID: why young immune systems are still on top
freeCodeCamp----arithmetic_arranger练习
端口占用1
Parse PSD files and map them into components
Decentralized Collaborative Learning Framework for Next POI Recommendation
随机推荐
Decentralized Collaborative Learning Framework for Next POI Recommendation
el-cascader和el-select点击别处让下拉框消失
DNA reveals surprise ancestry of mysterious Chinese mummies
openvswitch 编译安装
leetcode刷题之整数加一
Leetcode integer plus one
【代码解析(7)】Communication-Efficient Learning of Deep Networks from Decentralized Data
Promise(三)
不用登录直接下载PNG图标的一个网站
百度地图案例-修改地图样式
ES6 specification details
Solution to page cache problem (use with caution)
Curry realization of function continuous call calculation and accumulation
Centos8 builds php8 0.3 operating environment
JS实现网页轮播图
【Markdown笔记】
百度地图案例-缩放组件、地图比例组件
thinkphp5 ---- object(think\response\Json)转数组
el-date-picker限制选择范围,从当前时间到两个月前
JQ序列化后PHP后台解析