![](/img/trans.png)
[英]how to search for a line match from all files recursively found in a directory
[英]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.