Chinaunix首页 | 论坛 | 博客
  • 博客访问: 25871
  • 博文数量: 9
  • 博客积分: 198
  • 博客等级: 入伍新兵
  • 技术积分: 100
  • 用 户 组: 普通用户
  • 注册时间: 2010-12-10 12:34
文章分类
文章存档

2011年(5)

2010年(4)

最近访客

分类:

2010-12-10 14:31:36

 在linux宝库中找到了一个关于sed 用法小结我在这先贴出来:
 在sed中一直以为*号是配置任何字符.没想到这种理解是错误的, *号在sed中的用法有点像python 正确表达式一样. *是表示匹配一个或多个. 所以别在以为 sed '/^123*/lingfen' file 找到123开头后面跟任何字符的行换成lingfen, 而是以123开头并且123后面有多个3的行换成lingfen. (注:这段所述可能不正确,我从ABS高级部分正确表达式中所说的不一样,有点乱了)
   2 还有就是匹配空格用[]来表示.sed  不仅能用在file 中, 也能用在字符串中操作如, sed 's///g' "dsafsdafdsf"
   3 在网上还看到一篇文章, 原关于sed怎么工作的。 原来sed中有两个空间(buffer),一个叫patern space 和hold space。 patern space 是sed 操作的场所, 也就是说sed的所有操作都在patern space 中。而hold space是一个临时场所,可以把patern space 中的内空拷贝到hold space中。sed 从标准输入中读取数据(或对文件中读取),读取一行就在放在patern space中,然后看有没有操作,若有则进行相应的操作如 h, H, G, p, n, N等。 如果没有操作sed就会把放入在patern space 的东西输出,然后清除patern space里的内容。接着进行对下一行的操作。如此反复直到file中所有行都操作完。下面是网上看到的粘子。
  • l list pattern space的東西是甚麼
  • h 把hold space裡的東西清掉,把pattern space的東西copy給hold space
  • H 把pattern space的東西加在hold space東西後面
  • g 把pattern space裡的東西清掉,把hold space東西拿回給pattern space
  • G 把hold space的東西加在pattern space東西後面
  • p 印出pattern space的東西
  • x 交換(exchange)pattern space與hold space
l跟p的差別在對非ASCII的處理 cyril@gyoza:~$ cat last
第一行 first line
第二行 second line
最後一行 last line

cyril@gyoza:~$ sed '1,2p' last
第一行 first line
第一行 first line
第二行 second line
第二行 second line
最後一行 last line

cyril@gyoza:~$ sed '1,2l' last
\262\304\244@\246\346 first line$
第一行 first line
\262\304\244G\246\346 second line$
第二行 second line
最後一行 last line
這邊可以對pattern space有更深的了解,第一行進到pattern space 看到p把第一行印出,接著pattern space變成output,所以sed又把 他印一遍,接著第二行進來了,在pattern space裡,看到p把第二行印出, 同理又有兩個第二行被印出,第三行由於p沒作用,直接是pattern space 送到output。再看一個例子(抄出來的) cyril@gyoza:~$ cat command.txt
This describes the Linux ls command.
This describes the Linux cp command.

cyril@gyoza:~$ cat command.sed
/Linux/{
h
s/.* Linux \(.*\) .*/\1:/
p
x
}
cyril@gyoza:~$ sed -f command.sed command.txt
ls:
This describes the Linux ls command.
cp:
This describes the Linux cp command.

或者寫成一行
cyril@gyoza:~$ sed '/Linux/{h;s/.* Linux \(.*\) .*/\1:/;p;x;}' command.txt
這邊看第一個h把第一行推進hold space,不過記住pattern space還保有 原本第一行的句子。然後代換掉變成ls:,現在pattern space只有ls:, 然後p印出ls:,最後把hold space跟ls:交換,所以最後pattern space內 就是原本的東西,被sed當成output送出,cp是同樣的道理。 例子:

quote.txt

The honeysuckle hand 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.

显示行

$sed -n '2p' quote.txt

范围

$sed -n '1,3p' quote.txt

匹配单词

$sed -n '/The/'p quote.txt

匹配某行单词

$sed -n '4,/The/'p quote.txt

匹配元字符

$sed -n '/\$/'p quote.txt

显示整个文件

$sed -n '1,$p' quote.txt    sed -n '1,$'p quote.txt

任意字符

$sed -n '/.*ing/'p quote.txt  任意字符出现一次或N次 以ing结尾

首末行

$sed -n '1p' quote.txt

$sed -n '$p' quote.txt

打印行号

$sed -n '/music/=' quote.txt

2

$sed -n -e '/music/p' -e '/music/=' quote.txt

It was an evening of splendid music and company.

2

附加文件

!/bin/sed -f

/文件中匹配的字符/ a\

插入的字符 +x; *.sed quote.txt

删除行

$sed '1d' quote.txt 删除第一行

$sed '1,3d' quote.txt 删除1到3行

$sed '$d' quote.txt 删除最后一行

$sed '/Neave/d' quote.txt 删除带Neave的行

替换文本

$sed 's/night/NIGHT/' quote.txt  s替换/night/NIGHT/

$sed 's/\$//' quote.txt  $换成空格

$sed 's/The/Wow!/g' quote.txt    g替换所有

$sed 's/splendid/SPLENDID/w sed.out' quote.txt  w sed.out此行写入文件

$sed -n 's/nurse/"Hello" &/p' quote.txt  

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

$sed -n 's/played/from Hockering &/p' quote.txt

将sed结果写入文件命令

$sed '1,2 w filedt' quote.txt 第一二行写入文件

$sed '/Neave/ w dht' quote.txt 匹配//的写入文件

从文件中读文本

$sed '/company./r sedex.txt' quote.txt

在quote.txt的company后换行,写入sedex.txt的内容

匹配后退出

$sed '/.a.*/q' quote.txt

任意字符后跟a,再跟任意字符0次或任意次匹配:

Line 1.band  Line 2.bad  Liner3.was  Line 4.was

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

vi dos.txt

12332##DISO##45.12^M

00332##LPSO##23.11^M

01299##USPD##34.46^M

用一个空格替换所有的##,删除最前边的0,删除尾部^M

$sed 's/##*/ /g' dos.txt

$sed 's/^0*/ /g' dos.txt

$sed 's/\^M//g' dos.txt

$cat dos.txt |sed 's/##*/ /g'|sed 's/^0*/ /'|sed 's/\^M//g'

处理报文输出

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}'

去除行首数字

$vi UNH.txt

12345UND SPLLFC 234344

9999999UND SKKLT 3423

1UND SPLLY 434

$sed 's/^[0-9]*//g' UND.txt

附加文本每一行末尾加上字符串'passwd'

vi ok.txt

AC456

AC492169

AC9967

AC88345

$sed 's/[0-9]*/& Passwd/g' ok.txt

从sed输出中设置shell变量

$NAME="It's a go situation"

$REPLACE="GO"

$NEW_NAME='echo $NAME | sed "s/go/$REPLACE/g"'

$echo $NEW_NAME

快速一行命令

's/\.$//g'       删除以.结尾的行

'-e /abcd/d'     删除包含abcd的行

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

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

's/\.[][]*/[]//g 删除.后跟2或多个空格,以一个空格代替

's/COL\(...\)//g'删除COL和它后边的3个字母的行

's/^\//g'        删除第一个\

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

's/^[]//g'       删除行首tab键

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

插入文本

$echo "Mr Willis"|sed 's/Mr/& Bruce/g'

删除首字符

$echo "attkla.dc"|sed 's/^./g'

删除文件扩展名

$echo "attkla.dc"|sed 's/.dc//g'

增加扩展名

$echo "attkla"|sed 's/$/.dc/g'

替换字符系列

$x="Department+payroll%Building G"

+ 换成of %换成located

$echo $x | sed 's/\+/ of /g' | sed 's/\%/ Located at /g '

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

上一篇:没有了

下一篇:通配符(globbing)与正则表达式的区别

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