Issue
How do you replace a line that only has # with an empty line using sed?
I have tried to find on google but I haven't gotten anything.
File Content:
#test
another test
#
another test2
Expected result:
#test
another test
another test2
So, under expected result, after another test the line should be blank without the #.
Any help is greatly appreciated.
Solution
With regular expressions you can match for the beginning of a line with ^
and the end of a line with $
. The s/regexp/replacement/
command will replace text that matches regexp
with replacement
.
This sed
command gives the desired output:
sed 's/^#$//' < input.txt
On each line, sed
looks for the start of a line, a #
character, and then the end of a line, and replaces it with nothing. The newline character remains, however, so you are left with a blank line.
Answered By - remcycles Answer Checked By - Katrina (WPSolving Volunteer)