简体   繁体   中英

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 ?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM