我有样品:
This is a test
localhost% This is a test第一个This is a test将在下一行的txt文件中重新出现,该文件的前面将有一些字符localhost%。如果下一行包含类似于This is a test的内容,我希望删除第一个% This is a test
因此,我想检查两行,如果前面的行This is a test与下一行localhost% This is a test (关于%的注释)匹配,那么删除前一行。
我怎样才能做到这一点?
发布于 2018-09-20 13:42:54
也许:
sed '$!N;/^\(.*\)\n.*% \1$/!P;D'也就是说,让sed使用带有N的D在滚动的2行模式空间上工作,如果在"% "之后的第二行($)找到第一行(\(...\)捕获在\1中),则删除第一行(通过not (!) P漂洗它跳过)。
这也适用于以下情况:
这是测试foo%这是测试栏% foo%这是测试
(移除前2行)。
发布于 2018-09-20 10:26:35
您可以使用以下命令
sed -i "/This is a test/s/^This is a test$//g" test && sed -i '/^$/d' test输入文件
cat试验
This is a test
localhost% This is a test在执行上述命令后
输出文件
cat试验
localhost% This is a testhttps://unix.stackexchange.com/questions/470225
复制相似问题