繁体   English   中英

如何递归查找特定文件并从linux中找到的所有文件中删除最后一行?

[英]How to find a specific file recursively and delete the last line from all files found in linux?

我想递归查找特定文件, eg foo.txt ,如果该行包含特定单词(eg bar) ,则从中删除最后一行。 基本上结合——

find . -type f -iname "foo.txt"
sed '$d' foo.txt
dir1 -
      |-sub-dir1-
                 |-foo.txt
      |-sub-dir2-
                 |-foo.txt
      |-
      .
      .
      |-
      |-sub-dirn-
                 |-foo.txt

如果最后一行包含bar ,则从dir1下的所有foo.txt文件中删除最后一行。

原创内容——

foo.txt
1
2
bar

删除后——

foo.txt
1
2

谢谢。

使用-exec选项运行sed命令。 添加/bar/正则表达式以检查最后一行是否也匹配该模式。

find . -type f -iname "foo.txt" -exec sed -i '${/bar/d;}' {} +

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM