Linux(Centos 7.6)命令详解:sed

1.命令作用

流编辑器过滤和转换文本(stream editor for filtering and transforming text);sed是一种强大的流式文本编辑器,主要用于非交互式文本处理;

2.命令语法

Usage: sed [OPTION]... {script-only-if-no-other-script} [input-file]...

3.参数详解

OPTION:

  • -n, --quiet, --silent,抑制默认输出行为(静默输出) 
  • -e script, --expression=script,可指定多个-e参数,定义多个可执行的命令
  • -f script-file, --file=script-file,将script-file的内容添加到需要执行的命令中
  • --follow-symlinks,修改符号链接而非实际文件(默认参数),与-i参数一起使用,软链接修改后会变成实际的文件
  • -i[SUFFIX], --in-place[=SUFFIX],直接修改文件edit files in place (如指定SUFFIX后缀,则进行备份为后缀名称,然后修改源文件);如果修改的是软链接,修改后会变成实际的文件
  • -c, --copy,-i直接修改文件时,使用重命名(rename)而不是拷贝复制(copy),保留文件的inode和权限属性
  • -b, --binary,不进行任何操作;以兼容WIN32/CYGWIN/MSDOS/EMX (以二进制模式打开文件(CR+LFs are not treated specially))
  • -l N, --line-length=N,-l N后指定'l'命令来指定换行的长度N
  • --posix,默认为GNU扩展;--posix是禁用所有GNU扩展,使用POSIX扩展,主要是为了兼容macOS系统用
  • -r, --regexp-extended,在脚本中使用扩展正则表达式;默认使用基本正则(BRE),-r指定扩展正则(ERE)
  • -s, --separate,将文件视为独立的,而不是单个连续的长流;默认情况下sed将所有输入的文件视为连续数据流(行号连续递增),添加-s后每个文件独立维护处理上下文(行号、模式空间等重置);会影响多个文件处理的性能
  • -u, --unbuffered,从输入文件加载最少量的数据,并更频繁地刷新输出缓冲区;即sed默认会按块缓冲输入数据以提高性能,添加-u后强制逐行立即处理输入流,禁用缓冲优化;会影响大文件或多文件处理的性能
  • -z, --null-data,用NUL字符分隔行;默认分隔行是\n,-z可将\n作为普通字符进行被替换
  • --help,显示帮助信息并退出
  • --version,输出版本信息并退出

script-only-if-no-other-script:

  • s, '[[n,]m]s/regexp/replacement/[num/i/g/p/w newfile]'
    • s是将regexp正则匹配的内容替换为replacement替换内容
    • 其中n、m为数值,[[n],m]省略即全文操作,若指定[[n],m]则替换指定的m行或则n到m行;
    • 其中 为分隔符号,也可以指定为井号(#),如's#regexp#replacement#g',只要三个分隔符号指定一样即可;
    • 同一行有多个regexp正则匹配,默认仅替换第一个,num可以指定替换匹配的第几个
    • i 是忽略大小写
    • g是同一行中如果存在多个regexp正则匹配的内容都替换,如果没有g仅替换第一个regexp正则匹配的内容;
    • p 是直接打印 (匹配的行会单独输出1次,加上源文件处理后的1次,会连续输出2次)
    • w newfile 是将替换内容写到新文件
  • d, '/pattern/d', ''[[n,]m]d',删除匹配pattern的行,然后开始下一个循环,直到删除匹配pattern的所有行;删除m行,或者删除n到m行
  • a, '[[n,]m]a\text',附加文本;其中n、m为数值,如果忽略,是每行后添加text,若指定[[n],m]则是在指定的m行或者n到m行后添加text
  • i, '[[n,]m]i\text',插入文本;其中n、m为数值,如果忽略,是每行前添加text,若指定[[n],m]则是在指定的m行或者n到m行前添加text
  • p, -n '[[n,]m]p',p常与-n参数使用,其中n、m为数值,打印第m行,或者打印n到m行
  • q,立即终止sed脚本,不再处理任何后续输入;如果不关闭自动打印功能,则会打印当前模式空间的内容
  • Q,立即退出sed脚本,不再处理任何后续输入;
  • w, -n '/pattern/w newfile', -n '[[n,]m]w newfile',pattern、[[n],m]参考以上解释,w newfile是将sed处理后的内容不打印,而是输出到一个新的文件newfile里
  • 注意:以上[[n],m]可替换为 ,表示对最后一行进行操作

hold and pattern spaces(保持和模式空间):

  • hold spaces保持空间,sed用于临时存储文本;sed处理文本时可将文本行从模式空间交换到保持空间,或者从保持空间交换到模式空间,保持空间默认有一个换行符\n
  • pattern spaces模式空间,sed用于存放当前正在处理的文本行;sed命令执行时,它会从输入中读取一行文本,然后将这行文本存储在模式空间中。然后根据sed的script的各种编辑命令,如替换、删除、插入等对文本进行编辑,完成改行编辑后sed会输出修改后的行,然后继续处理下一行,依次循环处理
  • h, H,将模式空间内容覆盖/追加到保持空间,覆盖就没有\n,追加会有\n
  • g, G,将保持空间内容‌覆盖/追加‌到模式空间
  • x,模式空间与保持空间的内容交换
  • n,清空模式空间读取下一行
  • z,清空模式空间留下\n

4.参数OPTION用例

4.1.静默输出

[root@node1 ~]# sed 's/Never/AAAAA/g' test.txt 
AAAAA give up,
AAAAA lose hope.
Always have faith,
It allows you to cope.
Trying times will pass,
As they always do.
Just have patience,
Your dreams will come true.
So put on a smile,
You'll live through your pain.
Know it will pass,
And strength you will gain.
[root@node1 ~]# sed -n 's/Never/AAAAA/g' test.txt 
[root@node1 ~]# 

 4.2.指定多个可执行命令执行

[root@node1 ~]# sed -e 's/Never/AAAAA/g' -e 's/You/BBBBB/g' test.txt
AAAAA give up,
AAAAA lose hope.
Always have faith,
It allows you to cope.
Trying times will pass,
As they always do.
Just have patience,
BBBBBr dreams will come true.
So put on a smile,
BBBBB'll live through your pain.
Know it will pass,
And strength you will gain.
[root@node1 ~]# 

4.3.指定sed可执行的命令文件执行

[root@node1 ~]# cat script 
s/Never/AAAAA/g
s/You/BBBBB/g
[root@node1 ~]# sed -f script test.txt 
AAAAA give up,
AAAAA lose hope.
Always have faith,
It allows you to cope.
Trying times will pass,
As they always do.
Just have patience,
BBBBBr dreams will come true.
So put on a smile,
BBBBB'll live through your pain.
Know it will pass,
And strength you will gain.
[root@node1 ~]# 

4.4.进行实际文件的修改,不进行标准输出,且指定备份源文件

[root@node1 ~]# sed -i.bak 's/Never/AAAAA/g' test.txt  # -i直接修改文件,.bak作为源文件的备份文件的后缀名称
[root@node1 ~]# cat test.txt
AAAAA give up,
AAAAA lose hope.
Always have faith,
It allows you to cope.
Trying times will pass,
As they always do.
Just have patience,
Your dreams will come true.
So put on a smile,
You'll live through your pain.
Know it will pass,
And strength you will gain.
[root@node1 ~]# cat test.txt.bak 
Never give up,
Never lose hope.
Always have faith,
It allows you to cope.
Trying times will pass,
As they always do.
Just have patience,
Your dreams will come true.
So put on a smile,
You'll live through your pain.
Know it will pass,
And strength you will gain.
[root@node1 ~]# 

 4.5.修改链接文件不修改实际文件

[root@node1 ~]# ln -s test.txt ln_test.txt
[root@node1 ~]# ll *test.txt
lrwxrwxrwx. 1 root root   8 May 27 05:31 ln_test.txt -> test.txt
-rw-r--r--. 1 root root 262 May 27 05:10 test.txt
[root@node1 ~]# sed -i 's/Never/AAAAA/g' ln_test.txt     # 修改后,链接文件直接变成实际的文件
[root@node1 ~]# ll *test.txt
-rw-r--r--. 1 root root 262 May 27 05:32 ln_test.txt
-rw-r--r--. 1 root root 262 May 27 05:10 test.txt
[root@node1 ~]# cat ln_test.txt 
AAAAA give up,
AAAAA lose hope.
Always have faith,
It allows you to cope.
Trying times will pass,
As they always do.
Just have patience,
Your dreams will come true.
So put on a smile,
You'll live through your pain.
Know it will pass,
And strength you will gain.
[root@node1 ~]# cat test.txt 
Never give up,
Never lose hope.
Always have faith,
It allows you to cope.
Trying times will pass,
As they always do.
Just have patience,
Your dreams will come true.
So put on a smile,
You'll live through your pain.
Know it will pass,
And strength you will gain.
[root@node1 ~]# 

4.6.修改链接文件链接的实际文件

[root@node1 ~]# ln -s test.txt ln_test.txt
[root@node1 ~]# ll *test.txt
lrwxrwxrwx. 1 root root   8 May 27 06:01 ln_test.txt -> test.txt
-rw-r--r--. 1 root root 262 May 27 05:10 test.txt
[root@node1 ~]# sed --follow-symlinks -i 's/Never/AAAAA/g' ln_test.txt
[root@node1 ~]# ll *test.txt
lrwxrwxrwx. 1 root root   8 May 27 06:01 ln_test.txt -> test.txt
-rw-r--r--. 1 root root 262 May 27 06:01 test.txt
[root@node1 ~]# cat ln_test.txt 
AAAAA give up,
AAAAA lose hope.
Always have faith,
It allows you to cope.
Trying times will pass,
As they always do.
Just have patience,
Your dreams will come true.
So put on a smile,
You'll live through your pain.
Know it will pass,
And strength you will gain.
[root@node1 ~]# cat test.txt 
AAAAA give up,
AAAAA lose hope.
Always have faith,
It allows you to cope.
Trying times will pass,
As they always do.
Just have patience,
Your dreams will come true.
So put on a smile,
You'll live through your pain.
Know it will pass,
And strength you will gain.
[root@node1 ~]# 

 4.7.-c与-i一起使用,-c用于指定-i修改方式为复制,而非重命名

[root@node1 ~]# ls -il test.txt
67415572 -rw-r--r--. 1 root root 262 May 27 06:12 test.txt
[root@node1 ~]# sed -i 's/TTTTT/KKKKK/g' test.txt
[root@node1 ~]# ls -il test.txt
67166132 -rw-r--r--. 1 root root 262 May 27 06:15 test.txt
[root@node1 ~]# sed -c -i 's/KKKKK/MMMMM/g' test.txt
[root@node1 ~]# ls -il test.txt
67166132 -rw-r--r--. 1 root root 262 May 27 06:15 test.txt
[root@node1 ~]# 

4.8.指定每行输出的长度,不够的使用\分隔,换行输出

[root@node1 ~]# sed -n -l 25 'l' test.txt
Never give up,$
Never lose hope.$
Always have faith,$
It allows you to cope.$
Trying times will pass,$
As they always do.$
Just have patience,$
Your dreams will come tr\
ue.$
So put on a smile,$
You'll live through your\
 pain.$
Know it will pass,$
And strength you will ga\
in.$
[root@node1 ~]# 

4.9.扩展正则 

[root@node1 ~]# cat test1.txt 
aa aa aa aa 
       bb bb bb bb
 cc cc cc
    dd dd
 ee
ff ff ff
[root@node1 ~]# sed -r 's/( *)(.*)/\2/' test1.txt   
aa aa aa aa 
bb bb bb bb
cc cc cc
dd dd
ee
ff ff ff
[root@node1 ~]# 
## ( *): 匹配0个或多个空格(贪婪匹配)
## (.*): 匹配剩余所有字符(含空格)
## \1: 从行首开始捕获连续空格
## \2: 剩余内容(包括后续空格)全部存入\2
[root@node1 ~]# sed 's/( *)(.*)/\2/' test1.txt                               # 没有-r扩展正则使用()符号会报错 
sed: -e expression #1, char 14: invalid reference \2 on `s' command's RHS    # 没有-r,()需要对其使用\符号进行转义
[root@node1 ~]# sed 's/\( *\)\(.*\)/\2/' test1.txt 
aa aa aa aa 
bb bb bb bb
cc cc cc
dd dd
ee
ff ff ff
[root@node1 ~]#

 4.10.分割符号替换(默认\n为分割符号,是隐藏的字符不能替换,-z参数可将\n视为普通字符进行被替换)

[root@node1 ~]# sed 's/\n/ /g' test.txt 
Never give up,
Never lose hope.
Always have faith,
It allows you to cope.
Trying times will pass,
As they always do.
Just have patience,
Your dreams will come true.
So put on a smile,
You'll live through your pain.
Know it will pass,
And strength you will gain.
[root@node1 ~]# sed -z 's/\n/ /g' test.txt     # -z是NUL作为分割符,将隐藏的\n视为普通字符进行被替换,则全部合并为一行
Never give up, Never lose hope. Always have faith, It allows you to cope. Trying times will pass, As they always do. Just have patience, Your dreams will come true. So put on a smile, You'll live through your pain. Know it will pass, And strength you will gain. [root@node1 ~]# 
[root@node1 ~]# sed -z 's/up,\nNever/###/g' test.txt   # 可进行跨行匹配
Never give ### lose hope.
Always have faith,
It allows you to cope.
Trying times will pass,
As they always do.
Just have patience,
Your dreams will come true.
So put on a smile,
You'll live through your pain.
Know it will pass,
And strength you will gain.
[root@node1 ~]# echo 'aaa;bbb' | sed 's/;/\n/g'      # 将echo输出内容通过管道给sed处理; 内容直接替换为换行符\n,则不需要指定-z参数
aaa
bbb
[root@node1 ~]# echo "abc123" | sed 's/[0-9]\+/(\0)/g'  # \0是完整匹配的内容,[0-9]\+匹配的内容是123,然后将123替换成(123)
abc(123)
[root@node1 ~]# echo "abc123" | sed 's/\([a-z]\+\)\([0-9]\+\)/\2\1/'   # 使用括号(括号需要\进行转义)进行分组,然后通过123...标记替换
123abc
[root@node1 ~]# 

5.脚本script-only-if-no-other-script用例

5.1.替换字符串

# 将echo输出内容通过管道给sed处理;
[root@node1 ~]# echo 'abc123' | sed 's/[1-9]\+/***/g'
abc***
[root@node1 ~]# 

5.2.删除指定范围行

[root@node1 ~]# sed '3,10d' test.txt   # 删除3到10行,包括第3行和第10行
Never give up,
Never lose hope.
Know it will pass,
And strength you will gain.
[root@node1 ~]# 

5.3.行后添加内容

[root@node1 ~]# sed '1a\##########' test.txt 
Never give up,
##########
Never lose hope.
Always have faith,
It allows you to cope.
Trying times will pass,
As they always do.
Just have patience,
Your dreams will come true.
So put on a smile,
You'll live through your pain.
Know it will pass,
And strength you will gain.
[root@node1 ~]# 

5.4.行前添加内容

[root@node1 ~]# sed '1,3i\##########' test.txt 
##########
Never give up,
##########
Never lose hope.
##########
Always have faith,
It allows you to cope.
Trying times will pass,
As they always do.
Just have patience,
Your dreams will come true.
So put on a smile,
You'll live through your pain.
Know it will pass,
And strength you will gain.
[root@node1 ~]# 

5.5.打印指定内容

[root@node1 ~]# sed -n '1,2p' test.txt  # 与-n一起使用打印指定1到2行
Never give up,
Never lose hope.
[root@node1 ~]# 

6.hold and pattern spaces

6.1.倒序输出文件内容

[root@node1 ~]# cat test1.txt 
1
2
3
4
5
[root@node1 ~]# sed -n 'h;n;G;h;n;G;h;n;G;h;n;G;p' test1.txt 
5
4
3
2
1
# ①sed读取第一行 1 到模式空间,h 将模式空间覆盖到保持空间,此时保持空间内容为 1
# ②sed n清空模式空间读取下一行 2,G将保持空间的 1 追加到模式空间 2 后面,即模式空间内容为 2\n1
# ③sed h将模式空间覆盖到保持空间,此时保持空间内容为 2\n1
# ④sed n清空模式空间读取下一行 3,G将保持空间的 2\n1 追加到模式空间 3 后面,即模式空间内容为 3\n2\n1
# ⑤sed h将模式空间覆盖到保持空间,此时保持空间内容为 3\n2\n1
# ⑥sed n清空模式空间读取下一行 4,G将保持空间的 3\n2\n1 追加到模式空间 4 后面,即模式空间内容为 4\n3\n2\n1
# ⑦sed h将模式空间覆盖到保持空间,此时保持空间内容为 4\n3\n2\n1
# ⑧sed n清空模式空间读取下一行 5,G将保持空间的 3\n2\n1 追加到模式空间 5 后面,即模式空间内容为 5\n4\n3\n2\n1
# ⑨sed p将模式空间内容打印到屏幕
[root@node1 ~]# sed -n ':a;h;n;G;$p;ba' test1.txt
5
4
3
2
1
# ①将以上内容进行简写,以上h;n;G;循环执行4次,可以使用:a作为一个标记,当执行到ba是跳转到:a,以此来进行循环执行
# ②$p指执行到最后一行时打印,$是最后一行,p是打印
[root@node1 ~]# sed '1!G;h;$!d' test1.txt    # 其他实现方法
5
4
3
2
1
①文本第一行1,1!G代表非第一行执行G,即第一行不执行G,而是执行h,则此时模式空间和保持空间都是1;$!d代表非最后一行执行,则删除模式空间内容,无内容可打印
②文本第二行2,执行G保持空间1追加到模式空间2后,执行h模式空间2\n1覆盖到保持空间;$!d代表非最后一行执行,则删除模式空间内容,无内容可打印
③文本第三行3,执行G保持空间2\n1追加到模式空间3后,执行h模式空间3\n2\n1覆盖到保持空间;$!d代表非最后一行执行,则删除模式空间内容,无内容可打印
④文本第四行4,执行G保持空间3\n2\n1追加到模式空间4后,执行h模式空间4\n3\n2\n1覆盖到保持空间;$!d代表非最后一行执行,则删除模式空间内容,无内容可打印
⑤文本第五行5,执行G保持空间4\n3\n2\n1追加到模式空间5后,执行h模式空间4\n3\n2\n1覆盖到保持空间;$!d代表非最后一行执行,次为最后一行没有-n参数,默认会打印模式空间内容
[root@node1 ~]# 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

豆是浪个

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值