高级sed编程1--多行命令

如果在shell命令行当中有多个命令的话,最好使用{}将命令包含在其中;

1 多行命令

1.1next命令

  1.1.1 单行next命令

       小写的n命令告诉sed编辑器移动到数据流文本的下一行,而不是回到命令的最开始。请记住,通常sed编辑器在但个行上处理完所有的命令后才移动到文本的下一行。

eg:

root@jiangjian-K42JZ:/home/jiangjian# cat data
This is the header line

This is a data line

This is the last line
root@jiangjian-K42JZ:/home/jiangjian# sed '/header/{
> n
> d
> }' data
This is the header line
This is a data line

This is the last line
root@jiangjian-K42JZ:/home/jiangjian# 
这个data文本当中有两个空行,这里选择的删除了第一个,而如果是在单行的情况下,都会删除的。

eg:

root@jiangjian-K42JZ:/home/jiangjian# cat data
This is the header line

This is a data line

This is the last line
root@jiangjian-K42JZ:/home/jiangjian# sed '/^$/d' data
This is the header line
This is a data line
This is the last line
root@jiangjian-K42JZ:/home/jiangjian# 

1.1.2 组合多行文本

next的命令的多行版本(使用大写的N)将文本的下一行添加到已经存在与模式空间的文本当中,模式空间是指sed将要处理的文本


root@jiangjian-K42JZ:/home/jiangjian# cat data
This is the header line
This is a data line
This is the last line
root@jiangjian-K42JZ:/home/jiangjian# sed '/header/{
> N
> s/\n/ /
> }' data
This is the header line This is a data line
This is the last line
root@jiangjian-K42JZ:/home/jiangjian#


组合多行文本还可以处理跨行的文本

root@jiangjian-K42JZ:/home/jiangjian/sh# cat data3
The frist meeting of the Linux System
Administrator's group will be held on Tuesday.
All System Administrators should attend this meeting.
Thank you for your attendance
root@jiangjian-K42JZ:/home/jiangjian/sh# sed '
N
s/System.Administrator/Desktop User/
' data3
The frist meeting of the Linux Desktop User's group will be held on Tuesday.
All Desktop Users should attend this meeting.
Thank you for your attendance
root@jiangjian-K42JZ:/home/jiangjian/sh#
可以发现替换命令使用了通配符以匹配空格和换行符,然后,在匹配到换行符时,它被从字符串中删除了,导致两行合并为一行,这可嫩那个不是我们想要的结果。

    要解决这个问题,可以在sed编辑器脚本中使用两个替换命令,一个匹配跨行出现的内容,一个匹配单行出现的内容。

root@jiangjian-K42JZ:/home/jiangjian/sh# cat data3
The frist meeting of the Linux System
Administrator's group will be held on Tuesday.
All System Administrators should attend this meeting.
Thank you for your attendance
root@jiangjian-K42JZ:/home/jiangjian/sh# sed '
> N
> s/System\nAdministrator/Desktop\n User/
> s/System Administrator/Desktop User/
> '  data3
The frist meeting of the Linux Desktop
 User's group will be held on Tuesday.
All Desktop Users should attend this meeting.
Thank you for your attendance
root@jiangjian-K42JZ:/home/jiangjian/sh# 
这里存在一个问题就是当N下面不存在行时,sed编辑器就会停止工作,

root@jiangjian-K42JZ:/home/jiangjian/sh# cat data3
The frist meeting of the Linux System
Administrator's group will be held on Tuesday.
All System Administrators should attend this meeting.
root@jiangjian-K42JZ:/home/jiangjian/sh# sed '
> N
> s/System\n Administrator/Desktop\n User/
> s/System  Administrator/Desktop User/
> ' data3
The frist meeting of the Linux System
Administrator's group will be held on Tuesday.
All System Administrators should attend this meeting.#未被处理
root@jiangjian-K42JZ:/home/jiangjian/sh# 
解决方式,将当行命令移动到N命令之前,
root@jiangjian-K42JZ:/home/jiangjian/sh# cat data3
The frist meeting of the Linux System
Administrator's group will be held on Tuesday.
All System Administrators should attend this meeting.
root@jiangjian-K42JZ:/home/jiangjian/sh# sed '
> s/System Administrator/Desktop User/
> N
> s/System\n Administrator/Desktop\nUser/
> ' data3
The frist meeting of the Linux System
Administrator's group will be held on Tuesday.
All Desktop Users should attend this meeting.
root@jiangjian-K42JZ:/home/jiangjian/sh# 

1.2多行删除命令


使用单行删除命令必须要小心,

root@jiangjian-K42JZ:/home/jiangjian/sh# cat data3
The frist meeting of the Linux System
Administrator's group will be held on Tuesday.
All System Administrators should attend this meeting.
root@jiangjian-K42JZ:/home/jiangjian/sh# sed '
> N
> /System\nAdministrator/d
> ' data3
All System Administrators should attend this meeting.
root@jiangjian-K42JZ:/home/jiangjian/sh# 
这里删除了一行有System,下一行有Administrator的行的两行。

sed提供了多行删除命令D,它只撒谎年初模式空间中的第一行。它将删除直至换行符的所有字符。

root@jiangjian-K42JZ:/home/jiangjian/sh# cat data3
The frist meeting of the Linux System
Administrator's group will be held on Tuesday.
All System Administrators should attend this meeting.
root@jiangjian-K42JZ:/home/jiangjian/sh# sed '
> N
> /System\nAdministrator/D
> ' data3
Administrator's group will be held on Tuesday.
All System Administrators should attend this meeting.
root@jiangjian-K42JZ:/home/jiangjian/sh# 
这个命令的用途可以 删除在找到数据字符串的行前面出现的文本行

例:删除第一个空行

root@jiangjian-K42JZ:/home/jiangjian/sh# cat data.txt
This is head line

This is a data line

This is the last line
root@jiangjian-K42JZ:/home/jiangjian/sh# sed '/^$/{
N
/data/D      #指明了空行下面行存在data字符串,就执行删除前面一个模式空间
}' data.txt
This is head line
This is a data line

This is the last line
root@jiangjian-K42JZ:/home/jiangjian/sh# 

1.3多行打印命令

使用P,可以打印多行模式空间中的第一行。

root@jiangjian-K42JZ:/home/jiangjian/sh# cat data3
The frist meeting of the Linux System
Administrator's group will be held on Tuesday.
All System Administrators should attend this meeting.
root@jiangjian-K42JZ:/home/jiangjian/sh# sed -n '
> N
> /System\nAdministrator/P
> ' data3
The frist meeting of the Linux System



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值