Passing variables to sed

Hi folks,

I'm looking for a solution to pass variables to a sed-command. I'm reading a lot of threats and also the q&a "How can I use a variable in sed?". None of these commands works. I'm using AIX 5.2.

I want to do the following:

NUMBER=` echo 38341` | sed -n '/$NUMBER/p'

an obtained this:

+ + sed -n /$NUMBER/p
+ echo 38341
NUMBER=38341

If I try this command

NUMBER=` echo 38341` | sed -n "/$NUMBER/p"

I obtained this:

+ sed -n //p
+ sed: 0602-410 The first regular expression cannot be null.
+ echo 38341
NUMBER=38341

The problem I have is, I need the NUMBER as a line number to access these line in a second file.

Has anybody an idea about this?

THX

WE can not use enviroment variables with sed externally. With pattern match we can use \1, \2. Using awk we can make your requirement easily.

To put line number in a file with sed then,

sed = filename | sed 'N;s/\n/  /g'

Plz. Can you explain on " I need the NUMBER as a line number to access these line in a second file." this with example, so that we can get the solution for that easily.

hth.

No need for the backquoted echo, you also need to change the sed syntax slightly, and also specify the file you're searching. Though the syntax you're using would work with the pipe, you're just piping "nothing" to sed (same as doing a "cat /dev/null | sed blah..." - pointless). Either put the commands on seperate lines, or change that pipe to a semicolon...

NUMBER=38341
sed -n "$NUMBER p" my_file_here
# or
NUMBER=38341; sed -n "$NUMBER p" my_file_here

Cheers
ZB

I found a solution in between:

NUMBER=`printf "38341\n"` | sed -n "$NUMBER"'p'

(watch one the double quotes "$NUMBER" and quotes 'p')

prints:

+ sed -n 38341p
+ + printf 38341\n
NUMBER=38341

and in real live:

NUMBER=`printf "38341\n"`| sed -n "$NUMBER"'p' < save/nb_tables.xml

result:

+ sed -n 38341p
+ + 0< save/nb_tables.xml
+ printf 38341\n
NUMBER=38341
JOBNAME="FCRRAFM"

That's what I want.