1
I am trying to extract a substring from a string with sed on cygwin.
#!/bin/bash
var1="foo\ bar"
var2="baz"
var3="$var1 $var2"
# extract "foo\ bar" from "foo\ bar baz"
var4=`echo $var3 | sed "s/"$var1"//"`
echo "$var4"
but I get the following output, caused by the backslash: sed: -e expression #1, char 6: unterminated `s' command
how can I write the sed command to output 'baz'?
1The perl script can be golfed a bit:
perl -pe "s/\Q$var1//"
– glenn jackman – 2013-06-12T20:55:33.980@glennjackman nice! I take it the \Q is shorthand for
quotemeta
? I won't change the answer cause it's clearer as is but that's good to know, thanks. – terdon – 2013-06-13T01:26:21.167