linux下的sed应用,linux下使用最广泛的命令之一:sed(应用篇二)

如果不特别说明,sed例子中使用下述文本文本quote.txt的内容:

The honeysuckle band played all night long for only $90.

It was an evening of splendid music and company.

Too bad the disco floor fell through at 23:10.

The local nurse Miss P.Neave was in attendance.

四、实例(续)

1.使用&实现附加或修改字符串

[root@RHEL5 shell]# sed -n 's/nurse/"Hello" &/p' quote.txt

The local "Hello" nurse Miss P.Neave was in attendance.

[root@RHEL5 shell]# sed -n 's/played/"from Hockering" &/p' quote.txt

The honeysuckle band "from Hockering" played all night long for only $90.

2.将sed输出结果写入文件

[root@RHEL5 shell]# sed '1,2 w sed.out' quote.txt

The honeysuckle band played all night long for only $90.

It was an evening of splendid music and company.

Too bad the disco floor fell through at 23:10.

The local nurse Miss P.Neave was in attendance.

[root@RHEL5 shell]# cat sed.out

The honeysuckle band played all night long for only $90.

It was an evening of splendid music and company.

可以看出,显示在屏幕上的是所有行,而写入文件的只有第1 2行。

3.从文件中读取

创建一个文本test.txt,内容为:Boom boom  went the music

[root@RHEL5 shell]# sed '/company/ r test.txt' quote.txt

The honeysuckle band played all night long for only $90.

It was an evening of splendid music and company.

Boom boom  went the music

Too bad the disco floor fell through at 23:10.

The local nurse Miss P.Neave was in attendance.

所读的文件名需要用单引号括起来

4.匹配后退出

[root@RHEL5 shell]# sed '/.a.*/q' quote.txt

The honeysuckle band played all night long for only $90.

首次匹配后就退出

5.显示文件中的控制符

sed -n '1,$l' anony.txt

第一个参数是数字1,$后是小写的L。

五、使用系统sed

1.处理控制字符

现有一个dos.txt的文件,内容如下:

12332##DISO##45.12^M

00332##LPSO##23.11^M

01299##USPD##34.46^M

可采取以下动作:

(1) 用一个空格替换所有的##符号

[root@RHEL5 shell]# sed 's/##*/ /g' dos.txt

12332 DISO 45.12^M

00332 LPSO 23.11^M

01299 USPD 34.46^M

(2) 删除起始域中最前面的0(00)

[root@RHEL5 shell]# sed 's/^0*//g' dos.txt

12332##DISO##45.12^M

332##LPSO##23.11^M

1299##USPD##34.46^M

(3) 删除行尾控制字符(^M)

[root@RHEL5 shell]# sed 's/\^M//g' dos.txt

12332##DISO##45.12

00332##LPSO##23.11

01299##USPD##34.46

写成一条语句:

[root@RHEL5 shell]# cat dos.txt |sed 's/##*/ /g' |sed 's/0*//g' |sed 's/\^M//g'

12332 DISO 45.12

332 LPSO 23.11

1299 USPD 34.46

2.处理报文输出

该实例以sql.txt内容为数据源:

[root@RHEL5 shell]# cat sql.txt

Database        Size(MB)        Date Created

--------------------------------------------

GOSOUTH         2244            12/11/97

TRISUD          5632            8/9/99

(2 rows affected)

---end---

对上述内容作一些处理:

(1) 使用s /-*//g删除横线- - - - - -

[root@RHEL5 shell]# sed 's/-*//g' sql.txt

Database        Size(MB)        Date Created

GOSOUTH         2244            12/11/97

TRISUD          5632            8/9/99

(2 rows affected)

(2) 使用/^$/d删除空行

[root@RHEL5 shell]# sed '/^$/d' sql.txt

Database        Size(MB)        Date Created

--------------------------------------------

GOSOUTH         2244            12/11/97

TRISUD          5632            8/9/99

(2 rows affected)

(3) 使用$d删除最后一行

[root@RHEL5 shell]# sed '$d' sql.txt

Database        Size(MB)        Date Created

--------------------------------------------

GOSOUTH         2244            12/11/97

TRISUD          5632            8/9/99

[root@RHEL5 shell]#

(4) 使用1d删除第一行

[root@RHEL5 shell]# sed '1d' sql.txt

--------------------------------------------

GOSOUTH         2244            12/11/97

TRISUD          5632            8/9/99

(2 rows affected)

(5) 使用awk {print $1}打印第一列

[root@RHEL5 shell]# awk '{print $1}' sql.txt

Database

--------------------------------------------

GOSOUTH

TRISUD

(2

经过分步测试之后,将几个命令写成一条:

[root@RHEL5 shell]# cat sql.txt | sed 's/-*//g' | sed '/^$/d' | sed '$d' | sed '1d' | awk '{print $1}'

GOSOUTH

TRISUD

[root@RHEL5 shell]# cat sql.txt | sed 's/-*//g' | sed '/^$/d' | sed '$d' | sed '1d' | cut -f 1

GOSOUTH

TRISUD

3.附加文本

现有文件pass.txt,内容如下:

AC456

AC1

AC492169

AC9967

AC88345

[root@RHEL5 shell]# sed 's/[0-9][0-9][0-9]*/& PASS/g' pass.txt

AC456 PASS

AC1

AC492169 PASS

AC9967 PASS

AC88345 PASS

4.快速实用命令

‘s/\.$// g’ 删除以句点结尾行

‘-e/abcd/d’ 删除包含abcd的行

‘s/[][][]*/[]/g’ 删除一个以上空格,用一个空格代替

‘s/^[][]*//g’ 删除行首空格

‘s/\.[][]*/[]/g’ 删除句点后跟两个或更多空格,代之以一个空格

‘/^$/d’ 删除空行

‘s/^.//g’ 删除第一个字符

‘s/COL\(...\)//g’ 删除紧跟COL的后三个字母

‘s/^\///g’ 从路径中删除第一个\

‘s/[]/[ ]//g’ 删除所有空格并用tab键替代

‘S/^[ ]//g’ 删除行首所有tab键

‘s/[ ]*//g’ 删除所有tab键

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值