Before deleting lines with sed
sed -E -i '' '/hello/I d' myimportantfile.txt
(BSD sed)
How can I check which lines would be deleted? I thought changing the d
to a p
would work,
sed -E -i '' '/hello/I p' myimportantfile.txt
but that actually just in-place duplicates the lines that match in myimportantfile.txt
. I want the matching lines to be printed to stdout instead.
How can I check which lines a sed delete would delete?
Leaving the -i
flag there is useful so you can dry-run exactly the same command line you actually want to run, without risking changes of behaviour between testing and running, or when the sed -i
is inside of a function you're calling with just the sed command as parameter.
Replace the d
with w /dev/stdout
to write the matched line to stdout:
sed -E -i '' '/hello/I w /dev/stdout' myimportantfile.txt
You can even delete and log the deleted lines at the same time
sed -E -i '' -e '/hello/I {w /dev/stdout' -e 'd;}' myimportantfile.txt
{ ... }
is a function-list, each command within it will be run on the same match.
-e
joins the commands with a newline, because the w
command needs to end with a newline.