0

I am trying to delete a line inside a file using sed. This is the line:

PROC="Start: 05-03 20:02:51 | Version: OP | Cmd: 300 ./gzip.bc --sym-args 0 1 10 --sym-args 0 2 2 --sym-files 1 8 --sym-stdin 8 --sym-stdout"

This is the sed command I'm using:

sed -i '/"${PROC}"/d' ../lists/file

It's not working and it feels like hours trying to find out why... The command is run in a bash cript. I am using Linux Arch in a docker running on Mac (maybe that's the problem?)...

SuperTasche
  • 479
  • 1
  • 7
  • 17
  • 6
    See: [Difference between single and double quotes in bash](http://stackoverflow.com/q/6697753/3776858) – Cyrus May 03 '18 at 20:07
  • 2
    When you put double quotes *inside single quotes*, you're still in a single-quoted context. You need to end the single quotes before you start the double quotes if you want to be in a double-quoted context. (That said, your string isn't a valid regex, so you can't just drop it into a sed expression and expect it to match itself; generally, if that kind of behavior is what you want, `sed` is the wrong tool for the job at hand). – Charles Duffy May 03 '18 at 20:07
  • 3
    ...for example, the `|`s in your string will (per standard regular-expression syntax) be treated as "OR"s by `sed`, so if it *were* otherwise valid (which it isn't, because there are `/`s in the string, which `sed` will see as part of the command to run and not as part of the pattern to match) your current code would delete every line that contains `VERSION: OP`, no matter whether it has the other parts as well. – Charles Duffy May 03 '18 at 20:09
  • 1
    Not a duplicate: the problems here are single quotes and not escaping `$PROC` – Andrea Corbellini May 03 '18 at 20:16
  • 1
    @SuperTasche: `sed -i "/$(sed -e 's/[\/&]/\\&/g' <<< "$PROC")/d" ../lists/file` should do the trick – Andrea Corbellini May 03 '18 at 20:16
  • 2
    FYI, [Escape a string for a sed replace pattern](https://stackoverflow.com/questions/407523/escape-a-string-for-a-sed-replace-pattern) goes into the details of escaping – Andrea Corbellini May 03 '18 at 20:19
  • Thanks a lot for the support! I will definitely read up on correct sed syntax. Unfortunately I haven't been able to do so for now since I need to get my script to run ASAP. You may lynch me now... :( – SuperTasche May 03 '18 at 20:20

0 Answers0