亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

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
復(fù)制代碼

作者: Herowinter    時間: 2014-09-18 13:32
回復(fù) 1# shintoky
  1. sed -n '/^==/{N;/\n\s*$/P}' i
  2. ==belta==
復(fù)制代碼

作者: yinyuemi    時間: 2014-09-18 13:36
本帖最后由 yinyuemi 于 2014-09-18 13:37 編輯

回復(fù) 1# shintoky
  1. awk -vRS='\n\n' '!$2'

  2. awk -vRS='\n\n' '!(NF-1)'
復(fù)制代碼

作者: shintoky    時間: 2014-09-18 13:39
回復(fù) 4# yinyuemi


   謝謝 yinyuemi
作者: 這個冬天不冷    時間: 2014-09-18 13:42
回復(fù) 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 ~]#
復(fù)制代碼

作者: 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
回復(fù) 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
復(fù)制代碼

作者: yinyuemi    時間: 2014-09-18 13:50
回復(fù) 10# Herowinter


    NF is the equal to /^\s*$/, but the simpler
作者: 這個冬天不冷    時間: 2014-09-18 13:51
回復(fù) 10# Herowinter


    ,,,明白了,當(dāng)$0=0的時候 會出現(xiàn)。。。。判斷不夠細致、、、

學(xué)習(xí)了。

作者: yestreenstars    時間: 2014-09-18 13:53
回復(fù) 10# Herowinter

低版本的awk不支持\s
   
作者: Herowinter    時間: 2014-09-18 14:00
回復(fù) 13# yestreenstars

我的也不支持呀,我一直用的[[:space:]]*
作者: Kasiotao    時間: 2014-09-18 14:00
  1. sed -n '/^==/{h;n;/^$/{g;p}}' testfile
復(fù)制代碼

作者: yestreenstars    時間: 2014-09-18 14:05
回復(fù) 15# Kasiotao

把/^$/換成/^\s*$/會更好些
   
作者: Kasiotao    時間: 2014-09-18 14:10
回復(fù) 16# yestreenstars
頓悟!謝謝星辰大大

   
作者: zooyo    時間: 2014-09-18 14:42
提示: 作者被禁止或刪除 內(nèi)容自動屏蔽
作者: jcdiy0601    時間: 2014-09-18 15:01
回復(fù) 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
回復(fù) 18# zooyo

這個比較押韻,不好意思了...
   
作者: Herowinter    時間: 2014-09-18 15:29
回復(fù) 19# jcdiy0601

看一下sed命令的N P參數(shù).

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
回復(fù) 3# Herowinter


    這里/s是什么意思啊?
作者: jcdiy0601    時間: 2014-09-18 15:54
回復(fù) 3# Herowinter


    哦哦,我明白了
\s 匹配任意的空白符
\S 匹配任意不是空白符的字符
作者: Herowinter    時間: 2014-09-18 16:01
回復(fù) 24# jcdiy0601
樓主說的是空白行,可以是
^$ 空行,也可以是行首行尾間有0-n個空白字符的行.

   
作者: louis0o0    時間: 2014-09-18 16:06
也來一個~
awk 'last~/==.*==/&&/^[[:space:]]*$/{print last}{last=$0}' file
作者: jcdiy0601    時間: 2014-09-18 16:07
回復(fù) 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==
復(fù)制代碼

作者: klainogn    時間: 2014-09-18 16:54
本帖最后由 klainogn 于 2014-09-18 17:21 編輯

其實getline不給參數(shù)的話是會自動更新NF,NR及記錄信息的,所以可以這樣寫:
  1. $ awk '/==/{t=$0;getline;if(NF==0)print t}' urfile
復(fù)制代碼
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
回復(fù) 29# klainogn

多謝多謝,前面yinyuemi大神也提過了,好像有人刪了一個帖子?
回復(fù)顯示樓層全亂了, 這是CU的一個Bug
作者: zxcbvbbbbb    時間: 2015-04-30 13:44
提示: 作者被禁止或刪除 內(nèi)容自動屏蔽




歡迎光臨 Chinaunix (http://www.72891.cn/) Powered by Discuz! X3.2