给Shell编程初学者 7

AWolfBoy 2004-10-23 03:39:12
接<给Shell编程初学者 6>

# print only lines which do NOT match regexp (emulates "grep -v")
#只打印于一定字符不匹配的行(效"grep -v")
sed -n '/regexp/!p' # method 1, corresponds to above
sed '/regexp/d' # method 2, simpler syntax
###和上边相反,正如注释所说.

# print the line immediately before a regexp, but not the line
# containing the regexp
#打印包含"regexp"那一行的上一行,但是不打印包含"regexp"的行.
sed -n '/regexp/{g;1!p;};h'
###在命令执行到包含"regexp"那一行的上一行时,模式空间中这行的内容被
###拷贝到保留空间中.执行到包含"regexp"那一行时就打印它了.

# print the line immediately after a regexp, but not the line
# containing the regexp
#打印在"regexp"之后紧跟那一行,但是除去包含"regexp"的行.
sed -n '/regexp/{n;p;}'
###与上边类似,比上边简单.

# print 1 line of context before and after regexp, with line number
# indicating where the regexp occurred (imilar to "grep -A1 -B1")
#在"regexp"前后打印一行上下文,使其行号指示"regexp"在哪里出现(
#grep -A1 -B1相似)
sed -n -e '/regexp/{=;x;1!p;g;$!N;p;D;}' -e h
###看上去好像挺复杂,其实倒是不难解释.
###假设文档是这样
###$ cat test.txt
### 1 abc
### 2 cde
### 3 regexp
### 4 fgh
### 5 xyz
###命令执行到regexp前一行,引号里边的命令不执行,只执行h命令得到结果
### command parttern space holdspace output
### 执行到前一行 2cde 2cde
### 执行到regexp行 "=" 3regexp 3
### "x" 2cde 3regexp
### "1!p" 2cde 3regexp 2cde
### "g" 3regexp 3regexp
### "$N" 3regexp ; 4fgh 3regexp
### "p" 3regexp ; 4fgh 3regexp 3regexp
### 4fgh
### "D" 4fgh 3regexp
### "h" 4fgh 4fgh
###
### 看一下最右边的输出结果,还不错吧!

# grep for AAA and BBB and CCC (n any order)
#查找"AAA"和"BBB"和"CCC".(任意顺序)
sed '/AAA/!d; /BBB/!d; /CCC/!d'

# grep for AAA and BBB and CCC (n that order)
# 查找"AAA"和"BBB"和"CCC".(一定顺序)
sed '/AAA.*BBB.*CCC/!d'

# grep for AAA or BBB or CCC (emulates "egrep")
#查找"AAA"或"BBB"或"CCC".(任意顺序)
sed -e '/AAA/b' -e '/BBB/b' -e '/CCC/b' -e d # most seds
gsed '/AAA\|BBB\|CCC/!d' # GNU sed only
###上边三个没什么说的,就是查找功能呗.


# print paragraph if it contains AAA (blank lines separate paragraphs)
# HHsed v1.5 must insert a 'G;' after 'x;' in the next 3 scripts below
#如果某段包含"AAA",则打印这一段。(空行用来分隔段落)
#HHsed v1.5必须在'x;'之后插入一个'G;'
sed -e '/./{H;$!d;}' -e 'x;/AAA/!d;'
###前边一部分命令用保留空间来存储整个段落内容,后边一个命令用来查找

# print paragraph if it contains AAA and BBB and CCC (n any order)
#如果某段包含"AAA"和"BBB"和"CCC",则打印这一段
sed -e '/./{H;$!d;}' -e 'x;/AAA/!d;/BBB/!d;/CCC/!d'
###同上

# print paragraph if it contains AAA or BBB or CCC
# 如果某段包含"AAA"或"BBB"或"CCC",则打印这一段
sed -e '/./{H;$!d;}' -e 'x;/AAA/b' -e '/BBB/b' -e '/CCC/b' -e d
gsed '/./{H;$!d;};x;/AAA\|BBB\|CCC/b;d' # GNU sed only
###同上

# print only lines of 65 characters or longer
#仅打印长于65个字符的行
sed -n '/^.\{65\}/p'
###这也没什么好说的,正则表达式的运用.

# print only lines of less than 65 characters
#仅打印少于65个字符的行
sed -n '/^.\{65\}/!p' # method 1, corresponds to above
sed '/^.\{65\}/d' # method 2, simpler syntax
###又没啥吧

# print section of file from regular expression to end of file
#打印从字符"regexp"开始到文件结束的部分
sed -n '/regexp/,$p'
###还没啥,注意","的作用是选择行的范围,从包含regexp的行到最后一行

(待续)
...全文
49 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

23,120

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧