reading from 2 files through while loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting reading from 2 files through while loop
# 1  
Old 08-19-2008
Question reading from 2 files through while loop

hi
i have two files

cat input.txt
123456| 43256
456482|5893242

cat data.txt
xv 123456 abcd dsk
sd 123456 afsfn dd
df 43256 asdf ff
ss 456482 aa
sf 5893242 ff ff
aa 5893242 aa aa

i need to read inputs from input.txt and find data for data.txt.
then i need to print them as a table as belw
(if we took 1st line from input)

123456 | 43256 |xv 123456 abcd dsk | df 43256 asdf ff
| |sd 123456 afsfn dd | ss 456482 aa

but i couldnt do it. Pleas help
i have the below script


Quote:
while IFS='|' && read a b
do
c=`egrep ''$a|$b'' data.txt |sort`
awk '{print $a "|" $b "|" $c}'`
done < input.txt

Last edited by windows; 08-19-2008 at 05:26 AM..
# 2  
Old 08-21-2008
Kind of close.

Code:
cat input.txt |
while IFS='|' read a b; do
  adata=`grep $a data.txt |head -1`
  bdata=`grep $b data.txt |head -1`
  echo $a '|' $b '|' $adata '|' $bdata
done

The problems here are that (1) subsequent matches in data.txt are not matched, and (2) unmatched entries are not reported. To fix these, you need a more elaborate script with awk or perl. It CAN be done in shell script. Let's see where this gets you first.
# 3  
Old 08-22-2008
Thank you dear otheus

it worked fine. I did it without ' head -1' since i need all the matches for both $a and $b

BUT..

if $a has more than 1 match in data.txt, it wont be printed in tablular form.
I need to export the results to Excel so the can more proccessed further.
lets say from data.txt we have more than 1 line matching $a, and output will be:

Quote:
$a | $b| $adata|$bdata
$adata
$adata
What i need now ,at least, is to have the output as follows:

Quote:
$a | $b| $adata|$bdata
| | $adata |
| | $adata
|

so i can use text wizard in Excel to import it and keep it inshape.
Hope u got my point
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Reading line in while loop

Hello Team, i have to read line by line in a while loop, and the input file has:. # The script will start cppunit test application to run unit tests. -LD_LIBRARY_PATH=$CPPUNIT_HOME/lib:\ +LD_LIBRARY_PATH=$VOBTAG/SS_UnitTest/lib:\ $VOBTAG/SS_BFD/BFDSCLI/build:\ ... (7 Replies)
Discussion started by: chandana hs
7 Replies

2. Shell Programming and Scripting

While loop not reading all files if ssh fails

The below while loop is in ksh on a SunOs server: SPARC-Enterprise 5.10 The ksh version is: Version M-11/16/88i The intention of the below while loop is to read through a list of file names in files.txt and delete each file from a server, one at a time. The delete works, the problem is that if... (6 Replies)
Discussion started by: LES2013
6 Replies

3. Shell Programming and Scripting

for loop - reverse reading

All, Here is my for loop export CFGLIST="LIST1 LIST2 LIST3" for i in $CFGLIST do echo print $i done The output will be LIST1 LIST2 LIST3 But i want it display LIST3 LIST2 LIST1 (8 Replies)
Discussion started by: baluchen
8 Replies

4. AIX

How to pause a while loop while reading from a file

Hi, I am building a script to grep for a string in all the files from a folder and display the results. I am reading the files one by one by placing the names in other file using while loop my code is as below while read inp do chk=`grep -c "$str" $pth/$inp` ... (2 Replies)
Discussion started by: sekhar gajjala
2 Replies

5. UNIX for Dummies Questions & Answers

Reading inside a loop

Hi I am trying read in side a do statement but it is not working it is just printing abc before read but not stopping in abc for user input Can anybody please help #!/usr/bin/ksh cat sample_file | while read ln_source3 do param=`echo $ln_source3 | nawk... (1 Reply)
Discussion started by: ssuresh1999
1 Replies

6. Shell Programming and Scripting

While loop reading a record

Hi, I am trying to create a while loop that will do the following: INFILE= list of new records that need to be added after last previous record while read record do find the last record processed create list of new records output to a file echo "$record">> $NEWFILE done ... (9 Replies)
Discussion started by: shortyball24
9 Replies

7. Shell Programming and Scripting

while loop not reading last line

hi all, i have a while loop which i am using to read lines into an array: k=0 exec 10<file while read LINE <&10; do ARRAY=$LINE ((k++)) done exec 10>&- echo ${ARRAY} for some reason when i display the array it is not showing the last row in the file. any help appreciated. (1 Reply)
Discussion started by: npatwardhan
1 Replies

8. UNIX for Dummies Questions & Answers

reading more than one variable into a for loop

Hi, I have a file (details.txt) with 3 rows of variables ie... name postcode age john D fr25dd 25 mark W ab122aa 22 phil C cd343bb 33 What I want to do is read down the list with a loop and add each field into a one line piece of text... So I have a file (test1) which reads;... (3 Replies)
Discussion started by: starsky
3 Replies

9. UNIX for Dummies Questions & Answers

Reading from while loop into an array

Hi I have something like cat $HOME/all_dirs | while read ln_old_dirs do if then echo "$ln_all_old_dirs" fi done As you know that the variable ln_all_old_dirs is not accessable from outside the... (2 Replies)
Discussion started by: ssuresh1999
2 Replies

10. UNIX for Dummies Questions & Answers

problem in reading inside a while loop

I am not able to read inside a while though i get the message "inside read" the cursor doesnt prompt from the console cat file | while read ln_new_engine_dirs do echo "inside $ln_new_engine_dirs" if then read nn echo "inside read" fi done Thanks in advance (3 Replies)
Discussion started by: ssuresh1999
3 Replies
Login or Register to Ask a Question