we want to mark with "#" the matched line by sed or perl line liner

for example

we want to mark all lines in file that include the DatePattern word


expected output


note - in case line already marked then it will not add another "#" before the line


Prepending # to the line and also removing the = and whatever comes after it:

sed 's/^\([^#].*DatePattern\)=.*/#\1/' file

Alternatively, not removing the =:

sed 's/^[^#].*DatePattern/#&/' file

In case you don't want to remove the trailing date pattern (your spec is not quite clear), try

sed '/^[^#].*DatePattern/ s/^/#/' file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.