Michael F. Stemper <
michael...@gmail.com> wrote:
[snip]
> Then, I tried to actually pass the variable in to the program as a
> command-line argument:
>
> username@hostname$ cat OldForm.sed
> s/,/) ($1,/g
> s/^/($1,/
> s/$/)/
> username@hostname$ echo 7 | sed -f OldForm.sed $order
> sed: can't read 42: No such file or directory
> username@hostname$
>
> What's happened is obvious: sed is interpreting the "42" as the
> name of a file to read, not as an argument.
>
> I've gone through the O'Reilly book without finding out how to do
> this. I've also spent some time searching on-line, and been told
> over and over again how to use environment variables in one-liners
> (like my opening example had).
>
> What I have been unable to find is any description of how to pass
> something into a sed *program*. This seems to be a very basic
> action. Is it really true that it's not possible?
Hi, I'm a little late in replying, but here is a solution.
I wrote a bash script called OldForm2.sh and I gave it execute
permission:
#!/bin/sh
sed "s/,/) ($1,/g" | \
sed 's/^/('$1',/' | \
sed 's/$/)/'
Here is a sample execution:
joe@zillo:/tmp/joe/scratch$ order=42
joe@zillo:/tmp/joe/scratch$ echo 2,3,5,7 | ./OldForm2.sh $order
(42,2) (42,3) (42,5) (42,7)
What I wrote is very close to what you wrote. The differences are:
1. It is a bash script instead of a sed file.
2. I pass $order not to sed, but to the bash script.
3. I use the continuation symbol "\" to arrange the script in a
format that resembles your sed file.
4. I was careful to use ' and " correctly. Note that for
demonstration I used " in the first line and ' in the second line.
The third line has no $1 so either ' or " could be used in the same
way.
Observe what I did in the second line. $1 is not *in* a string,
because it *is* a string. Bash concatenates strings placed end to
end. The use of " is different because it allows dereferencing of
positional parameters.
I hope this is not too late to be of value. By the way, thanks for the
interesting illustration of use of sed. Although I knew how to solve
the positional parameter problem, I did not know how to pass comma
separated values to sed as you did. Cool.
-Joe