jcxz100
jcxz100

Reputation: 61

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:


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.

Upvotes: 1

Views: 332

Answers (2)

Mat Ford
Mat Ford

Reputation: 73

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.

Upvotes: 2

slitvinov
slitvinov

Reputation: 5768

$ cat r.sh 
awk '{
   a = $0 ~ /xyz/
   b = $0 ~ /xyz[^ \t]/
   if (a && !b) print "# " $0
   else         print $0
 }' "$@"

Usage

sh r.sh file

Upvotes: 0

Related Questions