Groups | Search | Server Info | Login | Register


Groups > alt.comp.lang.shell.unix.bourne-bash > #228

Pass variable into sed program

From "Michael F. Stemper" <michael.stemper@gmail.com>
Newsgroups alt.comp.lang.shell.unix.bourne-bash
Subject Pass variable into sed program
Date 2018-05-04 09:53 -0500
Organization A noiseless patient Spider
Message-ID <pchs5d$26c$1@dont-email.me> (permalink)

Show all headers | View raw


I'm in the process of modifying some existing scripts to account for
the fact that the format of their input has changed. The following
piping of multiple sed commands did the trick in one place:

username@hostname$ order=42
username@hostname$ echo 7 | sed "s/,/) ($order,/g" | sed "s/^/($order,/" 
| sed "s/$/)/"
(42,7)
username@hostname$ echo 2,3,5,7 | sed "s/,/) ($order,/g" | sed 
"s/^/($order,/" | sed "s/$/)/"
(42,2) (42,3) (42,5) (42,7)
username@hostname$

Exactly what I needed. Then, I found another spot in the same script
where I needed the same idiom. I naturally decided to move these three
commands to a sed program:

username@hostname$ cat OldForm.sed
s/,/) ($order,/g
s/^/($order,/
s/$/)/
username@hostname$ echo 7 | sed -f OldForm.sed
($order,7)
username@hostname$

So far, so good.

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?

-- 
Michael F. Stemper
What happens if you play John Cage's "4'33" at a slower tempo?

Back to alt.comp.lang.shell.unix.bourne-bash | Previous | NextNext in thread | Find similar


Thread

Pass variable into sed program "Michael F. Stemper" <michael.stemper@gmail.com> - 2018-05-04 09:53 -0500
  Re: Pass variable into sed program Grant Taylor <gtaylor@tnetconsulting.net> - 2018-05-04 12:37 -0600
    Re: Pass variable into sed program "Michael F. Stemper" <michael.stemper@gmail.com> - 2018-05-05 14:52 -0500
  Re: Pass variable into sed program Joe Rosevear <Joe_Rosevear@localhost.invalid> - 2018-06-18 04:17 +0000
    Re: Pass variable into sed program "Michael F. Stemper" <michael.stemper@gmail.com> - 2018-08-12 12:16 -0500

csiph-web