2013-03-09 48 views
0

我試圖用一個bash腳本,以確定我的代碼,關閉}如何找到空行後}使用bash

我能夠搜索和查找文本之後有一個以上的空行的地方使用該腳本:

TAGS="text" 
find "${SRCROOT}" \(-name "*.h" -or -name "*.m" \) -print0 | xargs -0 egrep --with-filename --line-number --only-matching "\$match = 1 if s/($TAGS)/ error: \$1/; END { exit \$match; }" 

但我不知道如何搜索關閉}字符後出現的空行。我需要某種類型的正則表達式嗎?

有人可以幫我把這些碎片放在一起嗎?

編輯

爲了澄清,它實際上是真正有用的發現只有在有一個以上的空行後}。

回答

1

嘗試這一個班輪:

awk '/}/{f=1;next} $0 && f{f=0;next} !$0 && f{print "empty line found:"NR}' file 

讓我們看一個例子:

kent$ nl -ba file 
    1 foo 
    2 bar} 
    3 
    4 foo2 
    5 
    6 
    7 bar2{ 
    8 
    9 } 
    10 
    11 
    12 
    13 eof 

kent$ awk '/}/{f=1;next} $0 && f{f=0;next} !$0 && f{print "empty line found:"NR}' file 
empty line found:3 
empty line found:10 
empty line found:11 
empty line found:12 

編輯

髒,快速爲新更新的問題:

 awk '/}/{f=1;m=0;next} $0 && f{f=0;m=0;next} !$0 && f &&!m{m=1;next} m{print "empty line found:"NR}' file 

新測試:

kent$ nl -ba file                        
    1 foo 
    2 bar} 
    3 
    4 foo2 
    5 
    6 
    7 bar2{ 
    8 
    9 } 
    10 
    11 
    12 
    13 blah{} 
    14 
    15 
    16 
    17 
    18 eof 

kent$ awk '/}/{f=1;m=0;next} $0 && f{f=0;m=0;next} !$0 && f &&!m{m=1;next} m{print "empty line found:"NR}' file 
empty line found:11 
empty line found:12 
empty line found:15 
empty line found:16 
empty line found:17 
+0

感謝您的回答!我編輯了我的問題,添加了一個我沒有意識到我正在尋找的細節。我們如何調整你的答案,以便只顯示}之後有多於一條空行的地方?謝謝! – Lizza 2013-03-09 20:32:24

+0

@Casper請參閱編輯答案 – Kent 2013-03-09 20:57:50