Chinaunix

标题: 如何打印匹配行 [打印本页]

作者: shintoky    时间: 2014-09-18 13:17
标题: 如何打印匹配行
请教各位大神如何打印匹配行,如果匹配行的下一行是空白行
比如
==alpha==
sawefweaf

==belta==

==charlie==
tttaweftt

则需要打印出 ==belta==
作者: 这个冬天不冷    时间: 2014-09-18 13:25
  1. [root@localhost ~]# awk '/==/{t=$0;getline line ;if(!line)print t}' file1
  2. ==belta==
  3. [root@localhost ~]# cat file1
  4. ==alpha==
  5. sawefweaf

  6. ==belta==

  7. ==charlie==
  8. tttaweftt
复制代码

作者: Herowinter    时间: 2014-09-18 13:32
回复 1# shintoky
  1. sed -n '/^==/{N;/\n\s*$/P}' i
  2. ==belta==
复制代码

作者: yinyuemi    时间: 2014-09-18 13:36
本帖最后由 yinyuemi 于 2014-09-18 13:37 编辑

回复 1# shintoky
  1. awk -vRS='\n\n' '!$2'

  2. awk -vRS='\n\n' '!(NF-1)'
复制代码

作者: shintoky    时间: 2014-09-18 13:39
回复 4# yinyuemi


   谢谢 yinyuemi
作者: 这个冬天不冷    时间: 2014-09-18 13:42
回复 1# shintoky
  1. [root@localhost ~]# awk '/==/{t=$0;getline line ;if(!line)print t}' file1
  2. ==ses600e==
  3. [root@localhost ~]# cat file
  4. file1  file2
  5. [root@localhost ~]# cat file1
  6. ==ses600e==

  7. ==ses600e==
  8. root
  9. [root@localhost ~]#
复制代码

作者: jeffreyst    时间: 2014-09-18 13:44
本帖最后由 jeffreyst 于 2014-09-18 13:49 编辑

cat file | sed -n '/==/{h;N;/\n$/{x;p}}'
==belta==
作者: Herowinter    时间: 2014-09-18 13:46
回复 8# 这个冬天不冷

其实这么写有bug的,zooyoo版主指出过我好几次,
改成$0~/^\s*$/好一点.
  1. awk '/==/{t=$0;getline line ;if(!line)print t}' i
  2. ==belta==

  3. cat i
  4. ==alpha==
  5. sawefweaf

  6. ==belta==
  7. 0

  8. ==charlie==
  9. tttaweftt
复制代码

作者: yinyuemi    时间: 2014-09-18 13:50
回复 10# Herowinter


    NF is the equal to /^\s*$/, but the simpler
作者: 这个冬天不冷    时间: 2014-09-18 13:51
回复 10# Herowinter


    ,,,明白了,当$0=0的时候 会出现。。。。判断不够细致、、、

学习了。

作者: yestreenstars    时间: 2014-09-18 13:53
回复 10# Herowinter

低版本的awk不支持\s
   
作者: Herowinter    时间: 2014-09-18 14:00
回复 13# yestreenstars

我的也不支持呀,我一直用的[[:space:]]*
作者: Kasiotao    时间: 2014-09-18 14:00
  1. sed -n '/^==/{h;n;/^$/{g;p}}' testfile
复制代码

作者: yestreenstars    时间: 2014-09-18 14:05
回复 15# Kasiotao

把/^$/换成/^\s*$/会更好些
   
作者: Kasiotao    时间: 2014-09-18 14:10
回复 16# yestreenstars
顿悟!谢谢星辰大大

   
作者: zooyo    时间: 2014-09-18 14:42
提示: 作者被禁止或删除 内容自动屏蔽
作者: jcdiy0601    时间: 2014-09-18 15:01
回复 3# Herowinter
能解释一下么?

   
作者: amandaysu    时间: 2014-09-18 15:06
awk '/==/{t=$0;getline s;if(length(s)==0) print t}' test.txt
作者: Herowinter    时间: 2014-09-18 15:27
回复 18# zooyo

这个比较押韵,不好意思了...
   
作者: Herowinter    时间: 2014-09-18 15:29
回复 19# jcdiy0601

看一下sed命令的N P参数.

N     Append the next line of input into the pattern space

P     Print  up  to  the first embedded newline of the current pattern
       space.
作者: jcdiy0601    时间: 2014-09-18 15:53
回复 3# Herowinter


    这里/s是什么意思啊?
作者: jcdiy0601    时间: 2014-09-18 15:54
回复 3# Herowinter


    哦哦,我明白了
\s 匹配任意的空白符
\S 匹配任意不是空白符的字符
作者: Herowinter    时间: 2014-09-18 16:01
回复 24# jcdiy0601
楼主说的是空白行,可以是
^$ 空行,也可以是行首行尾间有0-n个空白字符的行.

   
作者: louis0o0    时间: 2014-09-18 16:06
也来一个~
awk 'last~/==.*==/&&/^[[:space:]]*$/{print last}{last=$0}' file
作者: jcdiy0601    时间: 2014-09-18 16:07
回复 25# Herowinter


    严谨
作者: chengchow    时间: 2014-09-18 16:07
  1. sed -r '1h;1!H;$!d;${g;s/.*\n(==[a-z]*==)\n\n.*/\1/g}' file
  2. ==belta==
复制代码

作者: klainogn    时间: 2014-09-18 16:54
本帖最后由 klainogn 于 2014-09-18 17:21 编辑

其实getline不给参数的话是会自动更新NF,NR及记录信息的,所以可以这样写:
  1. $ awk '/==/{t=$0;getline;if(NF==0)print t}' urfile
复制代码
4.9.10 Summary of getline Variants
Table 4.1 summarizes the eight variants of getline, listing which built-in variables are set
by each one, and whether the variant is standard or a gawk extension. Note: for each
variant, gawk sets the RT built-in variable.
Variant                           Effect                                      Standard / Extension
getline                            Sets $0, NF, FNR, NR, and RT   Standard
getline  var                     Sets var, FNR, NR, and RT        Standard
getline < file                    Sets $0, NF, and RT                Standard
getline var < file              Sets var and RT                       Standard
command | getline           Sets $0, NF, and RT                 Standard
command | getline var      Sets var and RT                      Standard
command |& getline         Sets $0, NF, and RT                 Extension
command |& getline var    Sets var and RT                      Extension


   
作者: jeffreyst    时间: 2014-09-18 17:00
楼层全乱了,有没有
作者: Herowinter    时间: 2014-09-18 17:06
回复 29# klainogn

多谢多谢,前面yinyuemi大神也提过了,好像有人删了一个帖子?
回复显示楼层全乱了, 这是CU的一个Bug
作者: zxcbvbbbbb    时间: 2015-04-30 13:44
提示: 作者被禁止或删除 内容自动屏蔽




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2