我有这样的档案:
apple 1
apple 2
mango 3
apple 2
mango 4
mango 5
apple 3
apple 4
cherry 6
cherry 7
cherry 8
apple 5
cherry 9我希望在单行中使用awk或sed来删除与保留第二行的模式apple匹配的行。我还希望保留与模式不匹配的其余行,并获得如下输出:
apple 2
mango 3
apple 2
mango 4
mango 5
apple 4
cherry 6
cherry 7
cherry 8
apple 5
cherry 9发布于 2019-08-30 06:38:54
更多地优化awk脚本,您的:
awk '$1=="apple"{l=$0;next}l{print l; l=""}1' file 这依赖于将与apple模式匹配的行存储到变量D2中。只有在找不到模式时,存储行才会打印。
发布于 2019-08-30 04:34:31
经过反复试验,我得到了答案:
awk '{a=$1;b=$2;row1=NR;getline;row2=NR;c=$1;d=$2;if(row1==row2 || (a=="apple" && c=="apple")) {print c " " d;} else {print a " " b;print c " " d;}}' FILE这给出了输出:
apple 2
mango 3
apple 2
mango 4
mango 5
apple 4
cherry 6
cherry 7
cherry 8
apple 5
cherry 9https://unix.stackexchange.com/questions/538178
复制相似问题