comment a line of the patterns is a the beginning of the line

I need to comment the lines starting with pattern "exclude" or "exclude=". If the work exclude comes at any other part, ignore it. Also, ignore, excludes, excluded etc. Ie only comment the line starting with exclude.

File contents.

exclude
exclude=
hi I am excluded
excludes
excludes=

Desired output

#exclude
#exclude=
hi I am excluded
excludes
excludes=

Hi

$ awk -F= '$1 == "exclude"{printf "#";}1' file
#exclude
#exclude=
hi I am excluded
excludes
excludes=

Guru.

1 Like

Works great :slight_smile:

You can also try

sed 's/^exclude/#/g' filename

That will not produce the output desired. Have you tried it?

Ah how i read it incorrectly.. Hope this works..

sed 's/\(exclude\)/#\1/g;s/\(exclude=\)/#\1/g'

@Elixir: Am on my mobile.. couldn't try..

That will not work either.

sed -e 's/exclude$/#exclude/g' -e 's/exclude=/#exclude=/g' file
sed 's/^exclude\($\|[\t =]\)/#&/' file

--
Bye

gnu sed

sed '/^\bexclude\b/s/^/#/' infile