摆脱文件中不​​需要的行

时间:2016-09-29 09:03:10

标签: linux shell

在下面的例子中,^ [ - 是转义终端输出的转义字符(只需输入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,...

2 个答案:

答案 0 :(得分:0)

我提出的最简单,也可能是最愚蠢的解决方案:

[steelrat@archlinux ~]$ 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 ----------

答案 1 :(得分: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)改回到行尾。