grep within while read loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting grep within while read loop
# 1  
Old 10-24-2009
grep within while read loop

I have a key file
$ cat klist
5 N:8855 CASA VERDE ROAD :32827 :ORLAND
5 N:585 MOLLY LANE :30189 :WOODST
5 N:320 NINA ROAD :32304 :TALLAH

and a data file, see example of the line below:

N:RT 15 & N 7TH STREET :17837 :U SAVE

I need to search by key (2nd field) from klist within mlist.
My code:

Code:
 
#!/bin/ksh
cut -d':' -f2 klist|while read addr; do 
        grep "'""$addr""'" mlist; 
done
exit 0

When I run the script nothing comes out. I used set -x in the script, see output below:

Code:
 
+ cut -d: -f2 klist
+ read addr
+ grep '8855 CASA VERDE ROAD' mlist
+ read addr
+ grep '585 MOLLY LANE' mlist
+ read addr
+ grep '320 NINA ROAD' mlist
+ read addr
+ exit 0

But when I manually run one of the grep the result is found:

Code:
 
$ grep '320 NINA ROAD' mlist
N:320 NINA ROAD                   :99999 :BARROW
N:320 NINA ROAD                   :32304 :TALLAH          
A:320 NINA ROAD                   :32304 :TALLAH

I suspect the input/output streams being messed up whithin the while read loop, but have no clear understanding of the problem. Can someone clarify the situation, please?
# 2  
Old 10-24-2009
why are you placing single quotes???
Code:
grep "$addr" mlist;

this is enough... because i don't see any single quotes in your search string..
# 3  
Old 10-24-2009
That is irrelevant to my question.

I placed single quotes around search string as a meaure against spaces and such within the key value.

Any pointers regarding my question would be appreciated.
# 4  
Old 10-24-2009
Code:
$> addr="320 NINA ROAD"
$> grep "'""$addr""'" mlist
$> grep "$addr" mlist
N:320 NINA ROAD                   :99999 :BARROW
N:320 NINA ROAD                   :32304 :TALLAH
A:320 NINA ROAD                   :32304 :TALLAH

# 5  
Old 10-24-2009
Hi.

Not sure what the wierd combination of double and single quotes around your grep are meant to achieve, but from the data you post, there's nothing in your data file that matches any key in your klist file.

I modified your klist file to add something that did match, and it worked fine (changing your wierd grep to just grep "$addr" mlist)
# 6  
Old 10-24-2009
You do not need cut BTW:
Code:
#!/bin/ksh
while IFS=: read x addr x; do
  grep "${addr%% }" mlist;
done<klist

# 7  
Old 10-25-2009
Hammer & Screwdriver

Quote:
Originally Posted by migurus
That is irrelevant to my question.

I placed single quotes around search string as a meaure against spaces and such within the key value.

Any pointers regarding my question would be appreciated.
I think you should understand the problem first..
if you don't know and some one suggest you something be patient and analyze...
all my friends suggested the same what i suggested.. so take time and analyze...
then get back... Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Failure: if grep "$Var" "$line" inside while read line loop

Hi everybody, I am new at Unix/Bourne shell scripting and with my youngest experiences, I will not become very old with it :o My code: #!/bin/sh set -e set -u export IFS= optl="Optl" LOCSTORCLI="/opt/lsi/storcli/storcli" ($LOCSTORCLI /c0 /vall show | grep RAID | cut -d " "... (5 Replies)
Discussion started by: Subsonic66
5 Replies

2. UNIX for Dummies Questions & Answers

Piping grep into awk, read the next line using grep

Hi, I have a number of files containing the information below. """"" Fundallinfo 6.3950 14.9715 14.0482 """"" I would like to grep for Fundallinfo and use it to read the next line? I ideally would like to read the three numbers that follow in the next line and... (2 Replies)
Discussion started by: Paul Moghadam
2 Replies

3. Shell Programming and Scripting

Help with while read loop

Hey all, Tried searching the forums but my search-fu may not be strong today; please feel free to redirect me if I have simply missed a post that would be helpful! Trying to create a while loop that reads a list of configuration files, checks for a line that starts with "SOME_CMD" and ends... (4 Replies)
Discussion started by: jdwyer
4 Replies

4. UNIX for Dummies Questions & Answers

Read statement within while read loop

hi, this is my script #!/bin/ksh cat temp_file.dat | while read line do read test if ]; then break else echo "ERROR" fi done when i execute this code , the script does wait for the user input . it directly prints "ERROR" and terminates after the no. of times as there... (3 Replies)
Discussion started by: siva1612
3 Replies

5. Shell Programming and Scripting

until loop and read

Hi guys what I wanna do is to create a script where can I input several times a file - assume with read function that will be inserted into a temp. file for further processing. When I press q I want that the loop will stop and continue in my script I typed this but the options q is not working... (6 Replies)
Discussion started by: kl1ngac1k
6 Replies

6. Shell Programming and Scripting

While read line loop

Hi I'm writing a bash script which will read an input file and look for occurrences of the current user ($USER) executing the script. When i find the occurrence of the username I take that line and append it to a file with a line number and bracket display next to line. The input file has been... (12 Replies)
Discussion started by: BundBash
12 Replies

7. Shell Programming and Scripting

Need help with for loop with while read

Hello, The code below pulls printer information from a three-column file (set by the ${MULTIFILE} variable at the end), that has the printer queue name, IP, and type of printer. It is supposed to use this info to add the printers in the list to one, or more servers . The input file... (3 Replies)
Discussion started by: LinuxRacr
3 Replies

8. Shell Programming and Scripting

read line in a for loop

Hi All, How can we use read line using the index value of a FOR loop? eg: pt_mstr,pt_status,8 pt_mstr,pt_buyer,8 pt_mstr,pt_sfty_stk,8 pt_mstr,pt_ord_pol,3 pt_mstr,pt_capacity,8 pt_mstr,pt_plan_ord,3 pt_mstr,pt_ord_mult,8 From this file i want to read the line2, 3 and 4 only using a FOR... (3 Replies)
Discussion started by: balajim
3 Replies

9. Shell Programming and Scripting

Need Help : while read loop

hi all, Can you please help me with this issue ? while ####infinite loop do cat file1 |while read var1 var2 do func1 $var1 $var2 done cat file2 | while read var11 var22 do func2 $var11 $var22 done done func1 is called till all the values are read... (2 Replies)
Discussion started by: Navatha
2 Replies

10. UNIX for Dummies Questions & Answers

read inside a while loop

Hi all, In a while loop, like below... while read line do read choice case $choice in 1) echo "xxx" esac done < file why I can't run the read choice???? (3 Replies)
Discussion started by: dta4316
3 Replies
Login or Register to Ask a Question