Sed unknown command

Hi everyone, I am trying to replace a string that contains a space using sed. A typical input file is:

CATEGORY=1 1:1.0
CATEGORY=2 13:1.0
CATEGORY=3 1:99.0
CATEGORY=8 14:1.0
CATEGORY=11 1:1.0

I want to use a string (as it can change) to make replacements using

toReplace='s/CATEGORY=11 /+1 /g'
sed -e $toReplace $outputFile > $outputFile.tmp

I get a command unknown error from sed. Can anyone help please?

You need to properly quote anything with spaces or other shell specials in it. As a matter of fact, unless you really know what you are doing, get into the habit of double-quoting all variables.

sed -e "$toReplace" "$outputFile" > "$outputFile".tmp

Probably the shell fails to expand your variables the way you want them to be expanded. If you have variables containing spaces use double quotes to make sure they get expanded into one argument and not several.

To understand try the following: save the following to a file countargs.sh and make it executable:

#! /bin/ksh

print - "Number of arguments passed: $#"

while [ -n $# ] ; do
     print - "Argument passed: \"$1\""
     shift
done

exit 0

Now lets experiment: what do you think is the result if you substitute the "sed" in your command with the "countargs.sh" script? What is it really?

sed has a command syntax of "sed COMMAND FILE" if you present it something like "sed s/x /y /g some_file" it will think "s/x" is the command (which leads to a syntactical error of course) "/y" is the file and the rest is ignored. Only if you write sed "s/x /y /g" "some_file" it will treat "s/x /y /g" as the command argument and "some_file" as the file argument.

Solution for your problem: write

sed "$toReplace" "$outputFile" > "${outputFile}.tmp"

to be on the save side. Generally: quote enthusiastically - always! ;-)) In programming it never hurts to be overly pedantic.

I hope this helps.

bakunin

Many thanks to the both you.

In particular, found the example very useful and have definitely learned a thing or two. I'll be wearing my double quoting hat from now on!