sed高级命令

1. sed命令简介

sed命令是文本流编辑器,sed是一个 “非交互式的” 面向字符流的编辑器。能同时处理多个文件的多行内容,可以不对原文件进行修改,把整个文件输入到屏幕,也可以把只匹配到模式的内容输入到屏幕上。还可以对原文件进行修改,但是不会向屏幕输出修改的结果。

  • sed 命令的格式:‘sed command’ 文件名
  • sed 脚本的格式:"sed -f ’ sed命令的文件’ 要修改的文件名

2. sed命令的选项

参数作用
- n只输出匹配到的内容
- e可以一次修改多个内容每个修改的内容间需要用分号隔开
- f可以将写入到文件中的sed命令,通过此参数来1进行对某个文件的修改
- r支持使用扩展正则表达式
- i此参数可直接对文件内容进行修改

3. sed高级命令

  • N 命令是读取到文件里面指定的行,并将其添加到现有模式空间的下一行
  • n 读取下一行,此时模式空间有两行内容,但是先被读取到的内容不会被删除;当n命令之后的命令,还有其他命令时,如p命令,此时打印出的结果时n命令读取的那一行的内容
  • D 命令是就是将匹配到的模式空间中的第一行到\n(换行)之间的内容
  • d 删除当前模式空间中的内容
  • p 打印当前模式空间所有的内容,追加到默认输出之后
  • P 打印当前模式空间开端至\n的内容,并追加到默认输出之前
  • h 将当前模式空间中的内容覆盖到保持空间
  • H 将当前模式空间中的内容追加到保持空间
  • g 将当前保持空间中的内容覆盖到模式空间
  • G 将当前保持空间中的内容追加到模式空间
  • x 将当前保持空间的内容与模式空间的内容进行互换

4. 命令的用法

单行模式空间

[root@node4 file]#  cat test 
hell
o worl
 d admin
[root@node4 file]# sed -n '/hell/p' test 
hell
多行模式空间
[root@node4 file]# cat test 
hell
o worl
 d admin
[root@node4 file]# sed -n '/hell/N;p' test 
hell
o worl
 d admin

将\no替换为o

[root@node4 file]# sed '/hell/N;s/\no/o/' test 
hello worl
 d admin

4.1 D和N用法

N就是追加下一行到模式空间,将两行看看成一行,但两行之间有\n,执行后续的命令,然后再读取新的行。
D就是删除当前模式空间的开端至\n的内容。并且对剩余的模式空间继续执行。
[root@node4 file]# cat test2 
one
two
therr
four
five
[root@node4 file]# sed 'N;D' test2 
five

将one和two这两行加入到模式空间,但是这两行之间任然有换行符,就是将two这行换行

[root@node4 file]# cat test2 
one
two
therr
four
five

[root@node4 file]# sed '/one/{N;s/\n/ /}' test2 
one two
therr
four
five

4.2 d是删除当前模式空间的内容

将空行加入到模式空间里面,删除以什么开头换行以什么结尾就是空行,然后将其删除

[root@node4 file]# sed '/^$/{N;/^ \n$/d}' test2 
one
two


therr


four



five

[root@node4 file]# sed '/^$/{N;/^\n$/d}' test2 
one
two
therr
four

five

4.3 使用sed -f 使用文件对一个文件进行修改

[root@node4 file]# cat test2 
one
two


therr


four



five


[root@node4 file]# sed -f test.sh test2 
one
two
therr
four

five

[root@node4 file]# cat test.sh 
#!/bin/bash
/^$/{
N
/^\n$/d
}

D将hell和下一行加入模式空间,然后换行处理,这两行处理好之后(将两行看成一行)再将后面一行添加到模式空间进行处理,因为d前面有空格所以换行符后加一个空格

[root@node4 file]# cat test
hell
o worl
 d admin
[root@node4 file]# sed -f flie.sh test
hello world admin
[root@node4 file]# cat flie.sh 
#!/bin/bash
/hell/{
N
s/\n//
N
s/\n //
}

4.4 p就是打印当前模式空间的内容到默认输出之后

n+p的用法

n是读取下一行,覆盖模式空间的上一行.所以第一,第三行被覆盖了.因为到第五行的时候就只剩一行了.命令执行不成功了.所以就没有显示
[root@node4 file]# cat all 
This is the first line
This is the second line
This is the third line
This is the fourth line
This is the fifth line

[root@node4 file]# sed -n 'n;p' all 
This is the second line
This is the fourth line

若要使第五行显示可以使用下面的操作.
这里的$!的意是表示末尾行不执行n
[root@node4 file]# sed -n '$!n;p' all 
This is the second line
This is the fourth line
This is the fifth line

4.5 N+p

N是追加下一行到模式空间,并将两行看成一行,然后执行再命令,然后再读取新的行.

[root@node4 file]# cat all 
This is the 1 line
This is the 2 line
This is the 3 line
This is the 4 line
This is the 5 line
这里因为到第5行时发现没有下一行,所以就退出了
[root@node4 file]# sed -n 'N;p' all 
This is the 1 line
This is the 2 line
This is the 3 line
This is the 4 line

4.6 P的用法

打印从开始到第一个\n的内容,sed并不是对每一行末尾的\n进行处理,是对N命令追加的行间\n进行处理.
n就是提前读取下一行,覆盖模式空间的前一行。所以第一行被覆盖留下了空行,以此类推

[root@node4 file]# cat all 
This is the 1 line

This is the 2 line

This is the 3 line
This is the 4 line
This is the 5 line
[root@node4 file]# sed -n 'n;P' all 


This is the 4 line
因为sed是处理对N命令追加的行间的\n进行处理,而N是把第一行和空行当成一行。P时处理从开始到第一个\n的内容而最后只剩第五行单独一行就停止执行命令了。
[root@node4 file]# cat all 
This is the 1 line

This is the 2 line

This is the 3 line
This is the 4 line
This is the 5 line
[root@node4 file]# sed -n 'N;P' all 
This is the 1 line
This is the 2 line
This is the 3 line

4.7 d的用法

执行原理是n命令把第1行覆盖,输出第2行,然后d命令删除2行。依此类推
[root@node4 file]# cat all 
This is the 1 line
This is the 2 line
This is the 3 line
This is the 4 line
This is the 5 line
[root@node4 file]# sed  'n;d' all 
This is the 1 line
This is the 3 line
This is the 5 line
[root@node4 file]# sed -n 'n;d' all  //输出结果为空时因为d命令将输出的结果删除

[root@node4 file]# cat all 
This is the 1 line
This is the 2 line
This is the 3 line
This is the 4 line
This is the 5 line
[root@node4 file]# sed 'N;d' all 
This is the 5 line
因为N是追加下一行到当前行,把两行看成一行。因为执行到最后一行时发现只剩一行所以N就没有匹配到便留了下来

4.8 D的用法

因为D是删除当前模式空间至\n的内容
[root@node4 file]# cat all 
This is the 1 line
This is the 2 line
This is the 3 line
This is the 4 line
This is the 5 line
[root@node4 file]# sed 'n;D' all 
This is the 1 line
This is the 3 line
This is the 5 line
因为N是将两行看成一行所以就有1\n2,然后删除D又是删除当前模式空间的开始到\n的内容,最后只剩下5这一行。
[root@node4 file]# cat all 
This is the 1 line
This is the 2 line
This is the 3 line
This is the 4 line
This is the 5 line
[root@node4 file]# sed 'N;D' all 
This is the 5 line

4.9 h+x的用法

将1覆盖到保持空间,再将当前保持空间和模式空间的内容互换
[root@node4 file]# cat all 
This is the 1 line
This is the 2 line
This is the 3 line
This is the 4 line
This is the 5 line
[root@node4 file]# sed -e '/1/h' -e '/2/x' all 
This is the 1 line
This is the 1 line
This is the 3 line
This is the 4 line
This is the 5 line

4.10 h+g的用法

[root@node4 file]# cat all 
This is the 1 line
This is the 2 line
This is the 3 line
This is the 4 line
This is the 5 line
[root@node4 file]# sed -e '/1/h' -e '$g' all 
This is the 1 line
This is the 2 line
This is the 3 line
This is the 4 line
This is the 1 line
将1这一行覆盖到保持空间,$g是将保持空间的行取出来放到模式空间

4.11 H+g的用法

[root@node4 file]# cat all 
This is the 1 line
This is the 2 line
This is the 3 line
This is the 4 line
This is the 5 line
[root@node4 file]# sed -e '/1/H' -e '$g' all 
This is the 1 line
This is the 2 line
This is the 3 line
This is the 4 line

This is the 1 line
H是将当前模式空间里面的内容追加到保持空间$g是将保持空间中的行放到模式空间

4.12 G+h用法

将第一行加入到保持空间,然后G再将其追加到模式空间的第一行之后
[root@node4 file]# cat all 
This is the 1 line
This is the 2 line
This is the 3 line
This is the 4 line
This is the 5 line
[root@node4 file]# sed -e '/1/h' -e '/1/G' all 
This is the 1 line
This is the 1 line
This is the 2 line
This is the 3 line
This is the 4 line
This is the 5 line

4.13 H+G

H将第一行加入追加到保持空间,G将保持空间的内容追加到模式空间,因为追加的保持空间行,所以追加到空行后面,G再将保持空间的内容追加至模式空间的第一行后面,
[root@node4 file]# cat all 
This is the 1 line
This is the 2 line
This is the 3 line
This is the 4 line
This is the 5 line
[root@node4 file]# sed -e '/1/H' -e '/1/G' all   //G前面的数字代表模式空间的行数这里的1就是追加到模式空间的第一行的后面
This is the 1 line

This is the 1 line
This is the 2 line
This is the 3 line
This is the 4 line
This is the 5 line
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值