|

楼主 |
发表于 2004-11-17 17:00:17
|
显示全部楼层
翻阅了一下《sed&awk》那本书,结合freefall的帖子,这里给出自己的一点体会,希望各位指正。
h
=======>The hold command copies the current input line into the hold space.
Pattern space:/home/yourname/filename hello
Hold space:/home/yourname/filename hello
s:.*/::
===>相当于把/home/yourname/filename hello替换成filename hello
Pattern space:filename hello
Hold space:/home/yourname/filename hello
s: :_:g
===>把空格替换成_,filename hello变成filename_hello
Pattern space:filename_hello
Hold space:/home/yourname/filename hello
G
====>The Get command appends the line saved in the hold space to the pattern space.
Pattern space:
filename_hello\n/home/yourname/filename hello
Hold space:/home/yourname/filename hello
s:\(.*\)\n\(.*/\).*:\2\1:
==>对上面产生的Pattern space进行替换操作
第一个\(.*\),也就是see section,对应filename_hello
第二个\(.*/\),对应/home/yourname/
2,1组装,结果/home/yourname/filename_hello
总结一下,sed有两个缓存空间,一个是pattern space,一个是hold space.所有的s操作是针对pattern space来进行的。还有一个就是see section的概念,它是用\(see section)\来定义的。哈哈,研究了一天终于写出这点东西,看来要明白sed,最好的资料还是sed & awk, 2nd Edition,这里有下载ftp://210.73.95.87/books/unix/O'Reilly%20-%20Sed%20&%20awk,%202nd%20Edition.rar。 |
|