pass a variable to sed p in a loop?

Hi, Thanks for looking,,,,
(kornshell)
tmp3 is a list of line numbers
I want to print the lines from my list
code:
while read j
do
echo $j #works fine
echo $filename #works fine
#sed "'$jp'" "$filename" #NO GOOD
#sed '"$jp"' $filename #NO GOOD
#sed -n 20p $filename #works fine
done < tmp3 > tmp4

How do i get sed to treat $j as a variable and not get garbled?

try:

sed "$j"p

Hi.

The {} are usually used:

#!/bin/sh -

# @(#) s1       Demonstrate disambiguation.

j=245

echo $j #works fine
echo $filename #works fine
echo "'$jp'" "$filename" #NO GOOD
echo '"$jp"' $filename #NO GOOD
echo -n 20p $filename #works fine


echo
echo
echo " Best practice:"
echo sed -n "${j}p"

exit 0

producing:

% ./s1
245

''
"$jp"
20p

 Best practice:
sed -n 245p

Best wishes ... cheers, drl

It's always best to follow "Best Practises"
sed -n "${j}p"

Thanks very kindly drl,

note to SMAC, thanks,,,, but
no errors, but it returned every line
not the specific lines in the list.

ah...sorry, forgot the -n...

hhmm

j=245
sed -n "$j"p $filename #works for me

but i'm obviously too new to understand...much less help!

sorry! and cheers!

Hi.

Using a separate invocation of sed for each line can be expensive if you have long files and / or are printing a large number of lines. Here is an efficient technique that is a two-step process. First, a sed script is created containing the appropriate print commands: line-number followed by "p". Second, the script is processed in a single pass over the file with sed. Look at the script file "script" afterwards to see the commands that have been created.

This is similar to the technique in post #7 of thread http://www.unix.com/unix-for-dummies-question-and-answers/39347-sed-in-a-for-loop.html\#post302124422

This might considered advanced, but it can be very useful in production environments:

#!/usr/bin/env sh

# @(#) s1       Demonstrate creation of sed script for single-pass.

set -o nounset

## The shebang using "env" line is designed for portability. For
#  higher security, use:
#
#  #!/bin/sh -

## Use local command version for the commands in this demonstration.

echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version bash sed
echo

# Create the line number test file, can be in any order.

cat >numbers <<EOF
4
7
3
EOF

# Create the test data file.

cat >data1 <<EOF
one
two
three
four
five
six
seven
eight
nine
ten
EOF

# Create the sed script file with sed itself:

sed 's|$|p|' numbers >script

# Now efficiently run sed once instead of n times, display the
# data.

sed -n -f script data1

exit 0

producing:

% ./s1
(Versions displayed with local utility "version")
GNU bash 2.05b.0
GNU sed version 4.1.2

three
four
seven

See man pages for details ... cheers, drl

drl, This is above and beyond helpful.
Thanks very much,
You were quite right, about the inefficiency.
my data file is 219532, non-delimited records. the list is 15,500.
(and I have dozens of different files I have to do this for)
I'm kindof self taught, and may be going about this all wrong.
first with a cut -b command, then I'm trying to fgrep -n a value and create the list of line numbers.
then return the records that match.
it was taking forever.
I really appreciate if you'd recommend a completely different approach.

This was my first-ever post for help,
and you are the nicest person in the world.

Hi, JohnMario.

I'm not sure I understand. I think you are saying that in the simplest case, you have a data file, such as I called "data1", and you have a line-number list file, such as I called "numbers".

If you are having trouble creating the line-number file from yet a third file with cut, then please post a sample of that file.

Then we may be able to suggest a solution ... cheers, drl