Chinaunix首页 | 论坛 | 博客
  • 博客访问: 527275
  • 博文数量: 128
  • 博客积分: 4000
  • 博客等级: 上校
  • 技术积分: 1345
  • 用 户 组: 普通用户
  • 注册时间: 2008-01-22 21:43
文章分类

全部博文(128)

文章存档

2009年(30)

2008年(98)

我的朋友
sed

分类: LINUX

2008-04-23 13:13:55

sed选项
n 不打印
c 下一命令是编辑命令
f 调用sed脚本文件
sed不接触初始化文件

*1.使用sed在文件中查询文本
(1)使用行号
(2)使用正则表达式
   使用sed在文件中定位文本的方式
------------------------------------------------------------------
 x    x为一行号,如1
 x,y    表示行号范围从x到y,如2,5表示从第2行到第5行
 /patern/   查询包含模式的行。
 /pattern/pattern/ 查询包含两个模式的行。
 pattern/,x   在给定行号上查询包含模式的行。如/ribbon/,3
 x,/pattern/  通过行号和模式查询匹配行。3./vdu/
 x,y!    查询不包含指定行号x和y的行。1,2!
-------------------------------------------------------------------

*2.基本sed编辑命令
   sed编辑命令
-----------------------------------------------------------------
 p   打印匹配行
 =   显示文件行号
 a\   在定位行号后附加新文本信息
 i\   在定位行号后插入新文本信息
 d   删除定位行
 c\   用新文本替换定位文本
 s   使用替换模式替换相应模式
 r   从另一个文件中读文本
 w   写文本到一个文件
 q   第一个模式匹配完成后推出或立即推出
 l   显示与八进制ASCII代码等价的控制字符
 {}   在定位行执行的命令组
 n   从另一个文件中读文本下一行,并附加在下一行
 g   将模式2粘贴到/pattern n/
 y   传送字符
 n   延续到下一输入行;允许跨行的模式匹配语句
-------------------------------------------------------------------

*2.1 使用p显示行
打印第2行
$ sed -n '2p' quote.txt
It was an evening of splendid music and company.

*2.2 打印范围
打印1-3行
$ sed -n '1,3p' 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.

*2.3 打印模式
匹配单词Neave,并打印此行
$ sed -n '/Neave/'p quote.txt
The local nurse Miss P.Neave was in attendance.

*2.4 使用模式和行号进行查询
在第4行中查询"The"
$ sed -n '4,/The/'p quote.txt

*2.5 匹配元字符
匹配元字符$
$ sed -n '/\$/'p quote.txt
The honeysuckle band played all night long for only $90

*2.6 显示整个文件
要打印整个文件,只需将行范围设为第一行到最后一行1,$。$意为最后一行
$ sed -n '1,$p' 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.

*2.7 任意字符
$ sed -n '/.*ing/'p quote.txt

*2.8 打印行号
要打印地号,使用等号=。打印模式匹配的行号,使用格式/pattern/=
$ sed -e '/music/=' quote.txt
The honeysuckle band played all night long for only $90
2
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.
只关心实际行号
$ sed -n '/music/=' quote.txt
如果只打印行号及匹配行,必须使用两个sed命令,并使用e选项
$ sed -n -e '/music/p' -e '/music/=' quote.txt
It was an evening of splendid music and company.
2

*2.9 附加文本
a\通知sed这是一个附加操作,首先插入一个新行。把第三行拷贝到新行
#!/bin/sed -f
/honeysuckle/ a\
Then suddenly it happended.

*2.10 插入文件
在插定行前面插入,地址是匹配模式或行号
#!/bin/sed -f
/attendance/ i\
utter confusion followed.
或:
#!/bin/sed -f
4 i\
utter confusion followed.

*2.11 修改文本
#!/bin/sed -f
# change.sed
# 把匹配honeysuckle单词和其后的内容用下面的内容替换
/honeysuckle/ c\
The Office Dibble band played well.

混合:插入、修改、追加
#!/bin/sed -f
# this is a comment line, all comment starts with a #
# name: mix.sed

# this is the change on line1
1 c\
The Dibble band were grooving.
#let's now insert a line
/evening/ i\
The played some great tunes.
# change the last line, a $ mean last line
$ c\
Nurse Neave was too tipsy to help.

# stick in a new line before the last line
3 a\
Where was the nurse to help?

*2.12 删除文本
删除第一行文本
$ sed '1d' quote.txt
删除第一到三行
$ sed '1,3d' quote.txt
删除最后一行
$ sed '$d' quote.txt
删除匹配行
$ sed '/Neave/d' quote.txt

*2.13 替换文本
s选项通知sed这是一个替换操作
替换选项如下:
g 缺省情况下只替换第一次出现模式,使用g选项替换全局所有出现模式。
p 缺省sed将所有被替换行写入标准输出,加p选项将使-n选项无效。
w 文件名 使用此选项将输出定向到一个文件。
eg:
替换night为NIGHT,首先查询模式night,然后用文本NIGHT替换
$ sed 's/night/NIGHT/' quote.txt
The honeysuckle band played all NIGHT long for only $90
要从$90中删除$符号,不写入任何东西,仍需要用斜线括起来。
$ sed 's/\$//' quote.txt
The honeysuckle band played all night long for only 90
要进行全局替换,即替换所有出现模式,在命令后加g选项。
下面将所有The替换在Wow!
$ sed 's/The/Wow!/g' quote.txt

将替换结果写入一个文件用w选项,
将splendid替换为SPLENDID的替换结果写入文件sed.out,只写入替换行
$ sed 's/splendid/SPLENDID/w sed.out' quote.txt
$ cat sed.out
It was an evening of SPLENDID music and company.

*3.使用替换修改字符串
要附加或修改一个字符串,可以使用(&)命令。

用于插入新的内容
$ sed -n 's/nurse/"Hello" &/p' quote.txt
The local "Hello" nurse Miss P.Neave was in attendance.
原句:The local nurse Miss P.Neave was in attendance.
$ sed -n 's/played/from Hockering &/p' quote.txt
The honeysuckle band from Hockering played all night long for only $90
原句:The honeysuckle band played all night long for only $90

*4.将sed结果写入文件命令
w选项通知sed将结果写入文件。
将1,2行输出到文件filedt。
$ sed '1,2 w filedt' quote.txt
$ cat filedt
The honeysuckle band played all night long for only $90
It was an evening of splendid music and company.

查询模式Neave,匹配结果写入文件filedht。
$ sed '/Neave/ w dht' quote.txt
$ cat dht
The local nurse Miss P.Neave was in attendance.

*5.从文件中读文本
参数为/r
address r filename
读入一个文件,追加到匹配模式的后面
$ sed '/company./r sedex.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.

*6.匹配后退出
在模式匹配首次出现后退出sed,以便执行其他处理脚本。
address q
$ sed '/.a.*/q' quote.txt

*7.显示文件中的控制字符
$ sed -n '1,$l' ctr.txt

*8.系统sed
(1)处理控制字符
$ cat dos.txt
12332##DISO##45.12^M
00332##LPSO##23.11^M
01299##USPD##34.46^M
1)用空格替换所有的(##)符号
$ sed 's/##*/ /g' dos.txt
12332 DISO 45.12^M
00332 LPSO 23.11^M
01299 USPD 34.46^M
2)删除起始域中最前面的0(00)
$ sed 's/^0*//g' dos.txt
12332##DISO##45.12^M
332##LPSO##23.11^M
1299##USPD##34.46^M
3)最后除去尾^M符号,为此需做全局替换。设置替换部分为空。

(2)处理报文输出
Database        Size(MB)        Date Created
--------------------------------------------
GOSOUTH         2244            12/11/97
TRISUD          5632            8/9/99

(2 rows affected)

1)使用s/--*//g删除横线
2)使用/^$/d删除空行
3)使用$d删除最后一行
4)使用1d删除第一行
5)使用awk { print $1 } 打印第一列
$ cat sql.txt | sed 's/--*//g' | sed '/^$/d' | sed '$d' | sed '1d' | awk '{ print $1 }'
GOSOUTH
TRISUD

(3) 去除行首数字
$ sed 's/^[0-9]//g' UNH.txt

(4) 附加文本
在每一行末尾加一个Passed字符串
$ sed 's/[0-9][0-9]*/& Passed/g' ok.txt
AC456 Passed
AC492169 Passed
AC9967 Passed
AC88345 Passed

(5) 从shell向sed传值
$ Name="It's a go situation"
$ REPLACE="GO"
$ NEW_NAME=`echo $Name | sed "s/go/$REPLACE/g"`
$ echo $NEW_NAME
It's a GO situation

  sed 命令集
----------------------------------------------------------------------------
 's/\.$//g'    删除以句点结尾的行
 '-e/abcd/d'   删除包含abcd的行
 's/   */ /g'   删除一个以上空格,用一个空格代替
 's/^  *//g'   删除行首空格
 's/\.  */ /g'   删除句点后跟两个或更多空格,代之以一个空格
 '/^$/d'    删除空行
 's/^.//g'    删除第一个字符
 's/COL\(...\)//g'  删除紧跟COL的后三个字母
 's/^\///g'    从路径中删除第一个\
 's/ /\t//g'   删除所有空格并用tab键替代
 'S/^ //g'    删除行首所有tab键
 's/ *//g'    删除所有tab键
--------------------------------------------------------------------------

*删除路径名第一个/符号
$ echo $PWD | sed 's/^\///g'
usr/local

*追加/插入文本
$ echo "Mr Willis" | sed 's/Mr /& Bruce/g'
Mr Bruce Willis

*删除首字符
$ echo "accounts.doc" | sed 's/^.//g'
ccounts.doc

*删除文件扩展名
$ echo "accounts.doc" | sed 's/.doc//g'
accounts

*增加文件扩展名
$ echo "accounts" | sed 's/$/.doc/g'

*替换字符系列
$ x="Department+payrool%Building G"
$ echo $x
$ Department+payroll%Building G
实现转换:
+ to 'of'
% to 'located'
$ echo $x | sed 's/\+/ of /g' | sed 's/\%/ Located /g'

阅读(833) | 评论(0) | 转发(0) |
0

上一篇:shell 登录环境

下一篇:awk

给主人留下些什么吧!~~