gpt4 book ai didi

linux - 从文件中删除不需要的行

转载 作者:太空宇宙 更新时间:2023-11-04 12:42:25 24 4
gpt4 key购买 nike

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

最佳答案

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

[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 ----------

关于linux - 从文件中删除不需要的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39765930/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com