Reputation: 7028
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
Reputation: 785196
Two issues:
$my_line
in single quotes/
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
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