2016-09-29 55 views
0

在下面的例子中^ [ - 是轉義字符來染色終端輸出(只需鍵入ctrl + v + [)。擺脫文件中不​​需要的行

1)我的文件:

-------- just to mark start of file ---------- 
^[[1;31mbla bla bla^[[0m 



^[[0;36mTREE;01;^[[0m 


^[[1;31m^[[0m 
^[[1;31m^[[1;31mapple tree:^[[0m^[[0m 
^[[1;31m4 apples^M^M^[[0m 
^[[1;31m6 leafs^M^[[0m 


^[[0;36mTREE;02;^[[0m 


^[[0;36mTREE;03;^[[0m 

withered 

^[[0;36mTREE;04;^[[0m 


^[[0;36mTREE;05;^[[0m 

^[[0;36mTREE;06;^[[0m 

^[[0;36mTREE;07;^[[0m 


^[[1;31m^[[0m 
^[[1;31m^[[1;31mcherry tree:^[[0m^[[0m 
^[[1;31mbig branches^M^M^[[0m 
^[[1;31mtchick roots^M^[[0m 



^[[0;36mTREE;08;^[[0m 


^[[0;36mMy tree ^[[0m I have tree house on it^[[0;31m:-)^[[0m 



^[[0;36mTREE;09;^[[0m 

-------- just to mark end of file ---------- 

2)我想擺脫所有的「空標籤」 - 這是有其下沒有意見的所有標籤。
所以結果我想實現的是:

-------- just to mark start of results ---------- 
^[[1;31mbla bla bla^[[0m 



^[[0;36mTREE;01;^[[0m 


^[[1;31m^[[0m 
^[[1;31m^[[1;31mapple tree:^[[0m^[[0m 
^[[1;31m4 apples^M^M^[[0m 
^[[1;31m6 leafs^M^[[0m 


^[[0;36mTREE;03;^[[0m 

withered 

^[[0;36mTREE;07;^[[0m 


^[[1;31m^[[0m 
^[[1;31m^[[1;31mcherry tree:^[[0m^[[0m 
^[[1;31mbig branches^M^M^[[0m 
^[[1;31mtchick roots^M^[[0m 



^[[0;36mTREE;08;^[[0m 


^[[0;36mMy tree ^[[0m I have tree house on it^[[0;31m:-)^[[0m 



-------- just to mark end of results ---------- 

3)我做的:

pcregrep -M 'TREE.*\n(\n|\s)+(?=.*TREE|\z)' my_file 

和它的作品,因爲我期待的 - 它的葉子只有標籤沒有評論

-------- just to mark start of results ---------- 
^[[0;36mTREE;02;^[[0m 


^[[0;36mTREE;04;^[[0m 


^[[0;36mTREE;05;^[[0m 

^[[0;36mTREE;06;^[[0m 

^[[0;36mTREE;09;^[[0m 

-------- just to mark end of results ---------- 

4)但命令:

pcregrep -Mv 'TREE.*\n(\n|\s)+(?=.*TREE|\z)' my_file 

產品「有線結果」我不明白。

*)如何獲得我想要的結果?
有了這樣的任何工具:pcregrep,AG,ACK,用sed,awk中,...

+0

我也不認爲你需要標記結果的開始和結束,並將它們標記爲足夠的代碼。 – stee1rat

回答

0

嗯,我做到了。

(1) sed 's/^M//g; 
(2) s/$/#VAV#/' my_file | \ 
(3) paste -sd "" | \ 
(4) sed 's/^[\[0;36mTREE[[:print:]]\+^[\[0m\(\(#VAV#\)\|\([[:blank:]]\)\|\(^[\[0;36mTREE[[:print:]]\+^[\[0m\)\)*\(\(^[\[0;36mTREE[[:print:]]\+^[\[0m\)\|$\)/\6/g; 
(5) s/#VAV#/\n/g' 

(1)擺脫如果^ M轉義炭 - 它的障礙的東西。 (2)在每行的末尾放置「一些有意識的」字符串。 (3)將所有行連接成一個字符串。
(4)做適當的正則表達式替換。
(5)將該字符串從點(2)改回到行尾。

0

最簡單和可能,我已經想出了最愚蠢的解決方案:

[[email protected] ~]$ awk '/TREE/ {f=$0;p=1} !/^ *$/&&!/TREE/ {if (p==1) {print f; p=0} print $0}' my_file 

-------- just to mark start of results ---------- 
^[[1;31mbla bla bla^[[0m 
^[[0;36mTREE;01;^[[0m 
^[[1;31m^[[0m 
^[[1;31m^[[1;31mapple tree:^[[0m^[[0m 
^[[1;31m4 apples^M^M^[[0m 
^[[1;31m6 leafs^M^[[0m 
^[[0;36mTREE;03;^[[0m 
withered 
^[[0;36mTREE;07;^[[0m 
^[[1;31m^[[0m 
^[[1;31m^[[1;31mcherry tree:^[[0m^[[0m 
^[[1;31mbig branches^M^M^[[0m 
^[[1;31mtchick roots^M^[[0m 
^[[0;36mTREE;08;^[[0m 
^[[0;36mMy tree ^[[0m I have tree house on it^[[0;31m:-)^[[0m 
-------- just to mark end of results ---------- 

如果您需要空間(需要一些額外的工作來獲取從空的部分去掉空格):

$ awk '/^ *$/ {print $0} /TREE/ {f=$0;p=1} !/^ *$/&&!/TREE/ {if (p==1) {print f; p=0} print $0}' my_file 

-------- just to mark start of results ---------- 
^[[1;31mbla bla bla^[[0m 





^[[0;36mTREE;01;^[[0m 
^[[1;31m^[[0m 
^[[1;31m^[[1;31mapple tree:^[[0m^[[0m 
^[[1;31m4 apples^M^M^[[0m 
^[[1;31m6 leafs^M^[[0m 





^[[0;36mTREE;03;^[[0m 
withered 







^[[0;36mTREE;07;^[[0m 
^[[1;31m^[[0m 
^[[1;31m^[[1;31mcherry tree:^[[0m^[[0m 
^[[1;31mbig branches^M^M^[[0m 
^[[1;31mtchick roots^M^[[0m 





^[[0;36mTREE;08;^[[0m 
^[[0;36mMy tree ^[[0m I have tree house on it^[[0;31m:-)^[[0m 




-------- just to mark end of results ----------