rp346
rp346

Reputation: 7028

sed to delete line (path) from file

I am trying to delete a line from file, line is actually a path.

echo "/user/eventprocessor/prod/20150827" > /home/s3.success

cat /home/s3.success
/user/eventprocessor/prod/20150827

my_line=/user/eventprocessor/prod/20150827

Now I am trying to delete the line passed into my_line var, but doesnt work

sec -i '/$my_line/d' /home/s3.success

This does work

sed -i '/\/user\/eventprocessor\/prod\/20150827/d' /home/s3.success

Any help here please ?

Upvotes: 1

Views: 533

Answers (2)

anubhava
anubhava

Reputation: 785196

Two issues:

  1. Shell won't expand $my_line in single quotes
  2. Due to presence of / in your variable /$var/ will give error.

You can use:

my_line='/user/eventprocessor/prod/20150827'
sed -i "\~$my_line~d" /home/s3.success

Here ~ is used as an alternate reges delimiter.

Upvotes: 4

Anthony Geoghegan
Anthony Geoghegan

Reputation: 11993

I would modify the my_line shell variable to add backslashes in front of the forward slashes (using sed).

my_line=$(echo $my_line | sed 's!/!\\/!g')

The contents of the my_line variable is now \/user\/eventprocessor\/prod\/20150827 and can be used in your sed command:

sed -i "!$my_line!d" /home/s3.success

Note that double quotes are used so that the shell uses the contents of the my_line shell variable. Single quotes prevent the parameter expansion.

Upvotes: 0

Related Questions