[英]How to find a specific file recursively and delete the last line from all files found in linux?
I want to find a specific file eg foo.txt
recursively and delete the last line from it if the line contains a specific word (eg bar)
.我想递归查找特定文件,
eg foo.txt
,如果该行包含特定单词(eg bar)
,则从中删除最后一行。 Basically combine -基本上结合——
find . -type f -iname "foo.txt"
sed '$d' foo.txt
dir1 -
|-sub-dir1-
|-foo.txt
|-sub-dir2-
|-foo.txt
|-
.
.
|-
|-sub-dirn-
|-foo.txt
Delete last line from all foo.txt
files under dir1
if the last line contains bar
.如果最后一行包含
bar
,则从dir1
下的所有foo.txt
文件中删除最后一行。
Original contents -原创内容——
foo.txt
1
2
bar
After deletion -删除后——
foo.txt
1
2
Thanks.谢谢。
Use the -exec
option to run the sed
command.使用
-exec
选项运行sed
命令。 Add the /bar/
regular expression to check that the last line also matches that pattern.添加
/bar/
正则表达式以检查最后一行是否也匹配该模式。
find . -type f -iname "foo.txt" -exec sed -i '${/bar/d;}' {} +
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.