Search code examples
sedtext-fileswhitespaceend-of-line

Comment lines with string possibly followed by space/tab


In a text file I want to comment out these lines:

<whatever>xyz
<whatever>xyz <whatever>

... that's a certain string followed by either end-of-line or whitespace.

But I want to leave these lines alone:

<whatever>xyz<something><whatever>

... that's the string followed by a character that is not whitespace.

Where the following are of course not literal strings:

  • < whatever > zero or more characters that may be white-space.
  • < something > anything except white-space.

I've tried this:

sed -e '/xyz[ $]/s/^/# /g' in.txt > out.txt

... but it doesn't match the lines with end-of-line immediately after the string. Seems the $ sign is taken as a literal when it is inside square brackets.


This is my current hack:

sed -e '/xyz /s/^/# /g' in.txt > out.txt
sed -e '/xyz$/s/^/# /g' -i out.txt

... but I'd much rather only parse the file once due to speed. I'd also like to match \t as well as ordinary space character; but that is not compulsory.

For this input file, "in.txt":

xyz
xyz #
xyz.

I'm running Linux Mint, i.e. gnu sed.


Solution

  • Special characters lose their meaning in bracket expressions.

    Try this:

    sed -Ee '/(xyz$)|(xyz )|(xyz\t)/s/^/# /g'
    
    > gsed -Ee '/(xyz$)|(xyz )|(xyz)\t/s/^/# /g' in.txt
    # xyz
    # xyz #
    xyz.