0

I'm creating a variable from an array which build up multiple -e clauses for a sed command.

The resulting variable is something like:

sedArgs="-e 's/search1/replace1/g' -e 's/search2/replace2/g' -e 's/search3/replace3/g'"

But when I try to call sed with this as the argument I get the error sed: -e expression #1, char 1: unknown command: ''

I've tried to call sed the following ways:

cat $myFile | sed $sedArgs
cat $myFile | sed ${sedArgs}
cat $myFile | sed `echo $sedArgs`
cat $myFile | sed "$sedArgs"
cat $myFile | sed `echo "$sedArgs"`

and all give the same error.


UPDATE - Duplicate question

As has been identified, this is a 'quotes expansion' issue - I thought it was something sed specific, but the duplicate question that has been identified put me on the right track.

I managed to resolve the issue by creating the sedArgs string as:

sedArgs="-e s/search1/replace1/g -e s/search2/replace2/g -e s/search3/replace3/g"

and calling it with:

cat $myFile | sed $sedArgs

which works perfectly.

Then I took the advice of tripleee and kicked the useless cat out!

sed $sedArgs $myFile

also works perfectly.

Community
  • 1
  • 1
Fat Monk
  • 2,077
  • 1
  • 26
  • 59
  • Ugh. Why? If you post a new question showing whatever it is you're trying to do, complete with sample input and expected output, we can help you do it the right way. Also google `UUOC` and "always quote shell variables" and "deprecated backticks". – Ed Morton Jun 28 '16 at 16:31
  • @ed-morton This is a simplification of what I am trying to achieve so as to demonstrate the problem efficiently and without the confusion of everything else I am doing. In my real project the `$sedArgs` variable is being built via a whole bunch of logic derived from an array of filenames. Each of the files in the array reference each other and my task is to replace all references to all other files in each file efficiently while providing user feedback. There are probably many ways to achieve this, but I have everything else working and the issue above was my only sticking point. – Fat Monk Jun 29 '16 at 09:24

2 Answers2

2

Use BASH arrays instead of simple string:

# sed arguments in an array
sedArgs=(-e 's/search1/replace1/g' -e 's/search2/replace2/g' -e 's/search3/replace3/g')

# then use it as    
sed "${sedArgs[@]}" file
anubhava
  • 761,203
  • 64
  • 569
  • 643
0

Here is no sane way to do that, but you can pass the script as a single string.

sedArgs='s/search1/replace1/g
s/search2/replace2/g
s/search3/replace3/g'

: then

sed "$sedArgs" "$myFile"

The single-quoted string spans multiple lines; this is scary when you first see it, but perfectly normal shell script. Notice also how the cat is useless as ever, and how the file name needs to be quoted, too.

tripleee
  • 175,061
  • 34
  • 275
  • 318