1

I have a variable pattern. And I want to match pattern in file and if pattern is matched then line should be deleted.

I tried with:

sed '/$pattern/d' file.txt

But it doesn't work.

Please give me guidence for the same.

Thanks.

SLePort
  • 15,211
  • 3
  • 34
  • 44
user5689562
  • 21
  • 1
  • 2
  • 1
    use double quotes: `sed "/$pattern/d" file.txt` – Sundeep Oct 07 '16 at 09:26
  • 3
    Possible duplicate of [sed substitution with bash variables](http://stackoverflow.com/questions/7680504/sed-substitution-with-bash-variables) – Sundeep Oct 07 '16 at 09:28

1 Answers1

2

Just do that:

sed /$pattern/d file.txt

The quotes were transforming your variable in a string. Then you need to remove that.

And if you need to to write the changes in the file, just add -i

sed -i /$pattern/d file.txt
Marcel
  • 2,810
  • 2
  • 26
  • 46