2016-09-29 56 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 ----------