全部博文(73)
分类:
2009-04-23 17:26:17
接<给Shell编程初学者 7>
# print section of file based
on line numbers (ines 8-12, inclusive)
#根据行号来打印文件的一部分(-12行,包括在内)
sed -n '8,12p' # method 1
sed '8,12!d' # method 2
# print line number 52
#打印第52行
sed -n '52p' # method 1
sed '52!d' # method 2
sed '52q;d' # method 3, efficient
on large files
###仅注意第三种方法效率比较高就行了
# beginning at line 3, print
every 7th line
#从第三行开始,每7行打印一行
gsed -n '3~7p' # GNU sed only
sed -n '3,${p;n;n;n;n;n;n;}' # other
seds
###好像很容易理解了吧
# print section of file between
two regular expressions (nclusive)
#打印文件中指定字符之间的部分(含字符在内)
sed -n '/Iowa/,/Montana/p' # case
sensitive
###现在简单了吧.:)
SELECTIVE DELETION OF CERTAIN LINES:
# print all of file EXCEPT
section between 2 regular expressions
#打印除指定字符之间部分之外的全文
sed '/Iowa/,/Montana/d'
###与上边相似的简单
# delete duplicate, consecutive lines
from a file (emulates "uniq")
# First line in a set of
duplicate lines is kept, rest are
deleted.
#删除文件中重复的连续的行(似于"uniq"命令)
#重复行中第一行保留,其他删除
sed '$!N; /^\(.*\)\n\1$/!P; D'
###如果不是最后一行,就把下一行附加在模式空间,然后进行查找操作
###"^"和"$"中间的内容如果有重复就匹配成功.如果匹配不成功就用P打印
###第一行. 然后删除第一行.
# delete duplicate, nonconsecutive lines
from a file. Beware not to
# overflow the buffer size of
the hold space, or else use GNU
sed.
#删除文件中重复的,但不连续的行。注意不要溢出保留空间的缓冲器的大小,
#否则使用GNU
sed.
sed -n 'G; s/\n/&&/; /^\([
-~]*\n\).*\n\1/d; s/\n//; h; P'
###在我的linux环境执行不了,出错是sed: -e
expression #1, char 34:
###Invalid range end.是不是所谓的溢出保留空间的大小了呢?我也不得而知.
###大家补充吧.!!?????????????????
# delete the first 10 lines of
a file
#删除一个文件中前10行
sed '1,10d'
# delete the last line of a
file
#删除一个文件中最后1行
sed '$d'
###与上边一个都是查找删除
# delete the last 2 lines of
a file
#删除一个文件中最后2行
sed 'N;$!P;$!D;$d'
###如果理解了sed
'$!N;$!D'是如何工作的,这句话也不在话下吧!
# delete the last 10 lines of
a file
#删除一个文件中后10行
sed -e :a -e '$d;N;2,10ba' -e
'P;D' # method 1
sed -n -e :a -e '1,10!{P;N;D;};N;ba'
# method 2
###和打印后10行相似.什么?打印后10那个没看懂? /shakehand
:)
###?????????????????
# delete every 8th line
# 每8行删除1行
gsed '0~8d' # GNU sed only
sed 'n;n;n;n;n;n;n;d;' # other seds
###没说的!
# delete ALL blank lines from
a file (ame as "grep '.' ")
#删除文件所有空白行(似于"grep '.'
")
sed '/^$/d' # method 1
sed '/./!d' # method 2
###这两句就是告诉我们1.无内容的删除,2.有内容的保留 : )