Reputation: 567
I want to find a specific file e.g. foo.txt
recursively and delete the last line from it if the line contains a specific word (e.g. 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
.
Original contents -
foo.txt
1
2
bar
After deletion -
foo.txt
1
2
Thanks.
Upvotes: 0
Views: 96
Reputation: 781741
Use the -exec
option to run the sed
command. Add the /bar/
regular expression to check that the last line also matches that pattern.
find . -type f -iname "foo.txt" -exec sed -i '${/bar/d;}' {} +
Upvotes: 2