pass a variable to sed p in a loop?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting pass a variable to sed p in a loop?
# 1  
Old 01-02-2008
Question 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?
# 2  
Old 01-02-2008
try:

sed "$j"p
# 3  
Old 01-02-2008
Hi.

The {} are usually used:
Code:
#!/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:
Code:
% ./s1
245

''
"$jp"
20p

 Best practice:
sed -n 245p

Best wishes ... cheers, drl
# 4  
Old 01-02-2008
MySQL it woiks!

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.
# 5  
Old 01-02-2008
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!
# 6  
Old 01-02-2008
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 https://www.unix.com/unix-for-dummies...#post302124422

This might considered advanced, but it can be very useful in production environments:
Code:
#!/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:
Code:
% ./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
# 7  
Old 01-02-2008
pass a variable to sed p in a loop?

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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Need to pass variable in a command and assign value to a variable

Hello All, Hope you're doing well ! I am trying below command to be passed in a shell script, header_date_14 is a variable and $1 is the name of a file I intend to pass as a command line argument, however command line argument is not being accepted. header_date_14=$(m_dump... (8 Replies)
Discussion started by: ektubbe
8 Replies

2. Shell Programming and Scripting

Using sed in a loop/to remove lines contained in variable from file

I've tried numerous commands, but I am not sure how to use sed in a loop. This is what I have: VARZ contains CARD_FILE_LIST and it also contains CARD_FILE_LIST2 so echo "$VARZ" CARD_FILE_LIST CARD_FILE_LIST2 I have a file with 60 lines in /tmp/testfile it and I want those lines deleted... (3 Replies)
Discussion started by: newbie2010
3 Replies

3. Red Hat

pass a variable line number to sed

num=10 sed -n '$num p' test.txt sed -n '10 p' test.txt works however i am putting the sed command in a loop and the line number is not static Can someone please help me how to achive this. (1 Reply)
Discussion started by: figure20012
1 Replies

4. Red Hat

How to pass value of pwd as variable in SED to replace variable in a script file

Hi all, Hereby wish to have your advise for below: Main concept is I intend to get current directory of my script file. This script file will be copied to /etc/init.d. A string in this copy will be replaced with current directory value. Below is original script file: ... (6 Replies)
Discussion started by: cielle
6 Replies

5. Shell Programming and Scripting

Using SED with variable to replace strings in while loop

Hi guys, Hi have this input (Menu.xml)<?xml version="1.0" encoding="ISO-8859-1"?> <breakfast_menu> <food> <name>Berry-Berry Belgian Waffles</name> <price>$8.95</price> <calories>900</calories> </food> <food> <name>French Toast</name> ... (6 Replies)
Discussion started by: cgkmal
6 Replies

6. Shell Programming and Scripting

How to pass a function with a variable parameter into another variable?

Hello again :) Am currently trying to write a function which will delete a record from a file. The code currently looks as such: function deleteRecord() { clear read -p "Please enter the ID of the record you wish to remove: " strID ... (2 Replies)
Discussion started by: U_C_Dispatj
2 Replies

7. Shell Programming and Scripting

how to pass a variable to an update sql statement inside a loop

hi all, i am experiencing an error which i think an incorrect syntax for the where clause passing a variable was given. under is my code. sqlplus -s ${USERNAME}/${PASSWORD}@${SID} << END1 >> $LOGFILE whenever sqlerror exit set serveroutput on size 1000000 declare l_rc ... (0 Replies)
Discussion started by: ryukishin_17
0 Replies

8. Shell Programming and Scripting

pass variable to sed like in awk (-v switch)

hi all is possible to pass shell (bash) variable to sed like it is in awk? example: awk script is storred in awk.awk file and I am passing variable called var to this file. $ cat awk.awk {if ($5==var) print $0} so it works when i issue $ bash_var=24 $ ls -l | awk -v... (1 Reply)
Discussion started by: wakatana
1 Replies

9. Shell Programming and Scripting

Pass perl variable to sed

Hello, I'd like to pass a variable to a sed command in a perl script. The script is like this : #!/usr/bin/perl -w $newline="new"; system q(sed '/insert/ i\ '$newline <sed1.txt >sed2.txt); But the interpretor wouldn't recognize $newline, it inserts a "\n" instead. I've also... (4 Replies)
Discussion started by: hi_ryo
4 Replies

10. Shell Programming and Scripting

Pass variable to sed?

Hi there. If variables are named inside of a ksh script, how is it possible to pass these to sed? The affected portion of my script goes something like this: A=`cut -d. -f1 $FILE` B=`cut -d. -f2 $FILE` C=`cut -d. -f3 $FILE` sed 's/1111/$A/g;s/2222/$B/g;s/3333/$C/g' file > anotherfile ... (2 Replies)
Discussion started by: kristy
2 Replies
Login or Register to Ask a Question