I'm using sed to do the following:
- Match pattern (eg: line starting with
#
) - Substitute match (eg: substitute the
#
char at the start to nothing/remove it) - Print to stdout (eg: print the resulting subtitution to stdout, since we're using
-i
) - Delete match (eg: delete the whole line that contained the
#
char at the start of the line)
This is done on a file, and as such, sed
wouldn't output to stdout when used with the -i
flag, unless I use the following trick:
sed -i -e 's/^\(#\)/\1/w /dev/stdout' -e '/^#/d' test
on a file called test
, with the following example content:
#this
#is
a
test
The above work for everything, except substitution...
Here the one implementing substitution (where I try to delete the #
character before printing it to stdout):
sed -i -e 's/^\(#\)//w /dev/stdout' -e '/^#/d' test
This is similar to above, except I replaced \1
with //
so it delete #
instead of printing the whole line...
Problem is, the last one doesn't work in deleting the line, and instead only substitute and print. The first one does work for everything except substitution (eg: removing the #
sign at the start of the line)
Why isn't this working (the second attempt that is, first one work except it doesn't delete the #
char)?
-i
prevent the output from being shown on stdout, unlike when not using-i
...) :) @berndbausch – Nordine Lotfi May 19 '21 at 00:41