The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Reading from while loop into an array ssuresh1999 UNIX for Dummies Questions & Answers 2 08-12-2008 11:53 AM
problem in reading inside a while loop ssuresh1999 UNIX for Dummies Questions & Answers 3 08-07-2008 09:53 AM
Reading from a specific line in a loop kaushikraman Shell Programming and Scripting 3 07-24-2008 05:49 AM
reading from within the loop --std i/p problem mobydick Shell Programming and Scripting 2 07-11-2008 08:42 PM
Reading 5 Students names and grades using while loop???? dlbomber1 Shell Programming and Scripting 1 11-26-2007 02:46 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-19-2008
windows windows is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 5
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 04:26 AM..
  #2 (permalink)  
Old 08-21-2008
otheus's Avatar
otheus otheus is offline Forum Staff  
Moderator ala Mode
  
 

Join Date: Feb 2007
Location: Innsbruck, Austria
Posts: 1,886
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 (permalink)  
Old 08-22-2008
windows windows is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 5
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
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 04:12 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0