LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 12-14-2007, 03:33 PM   #1
demxkn66
LQ Newbie
 
Registered: Nov 2007
Location: The Netherlands
Posts: 8

Rep: Reputation: 0
Post Using cat readline within cat readline while loop


Hi All,

I'working using bash and the SHELL script that is executed is written in #!/bin/sh .

Situation:
A There are 2 textfiles (file1 + file2) that contain lines that need to be processed individually.
B File1 consist of 5 lines
C File2 consist of 11 lines

The following functionality is required:
I) 1st line of file1 need to be retrieved and then all lines of file2 need to be processed
II) repeat step I for 2nd line of file1, 3rd line ... etc until all lines of file1 are processed.

My code that i'm using:

cat file 1 |
while read line
do
echo $line
... do some processing

cat file2 |
while read line
do
echo $line
... do some processing
done
done


Result when running above code:
- Line 1 of file1 is read then All lines of file 2 are read
- script continues, Line 2,3,4 and 5 of file1 isn't processed

After testing it seems having 2 filehandles open (cat read line within cat read line is techinically possible?

Maybe there is a better solution in geting this functionality, maybe using (n)awk, I'm open for it?

Thanks in advance for your hints.

CU

Olaf
 
Old 12-14-2007, 03:49 PM   #2
demxkn66
LQ Newbie
 
Registered: Nov 2007
Location: The Netherlands
Posts: 8

Original Poster
Rep: Reputation: 0
I did also check other forums:

Is the problem using the same variable name 'line'?

So orginal code:
Quote:
Originally Posted by demxkn66 View Post
Hi All,

My code that i'm using:

cat file 1 |
while read line
do
echo $line
... do some processing

cat file2 |
while read line
do
echo $line
... do some processing
done
done
Should be replaced by

cat file 1 |
while read linefile1
do
echo $linefile1
... do some processing

cat file2 |
while read linefile2
do
echo $linefile2
... do some processing
done
done


Unfortunately I'm not able to test it right nwo not having unix environment at my current location :-)
 
Old 12-14-2007, 04:29 PM   #3
radoulov
Member
 
Registered: Apr 2007
Location: Milano, Italia/Варна, България
Distribution: Ubuntu, Open SUSE
Posts: 212

Rep: Reputation: 35
If you post a sample from your input files and the desired output,
it would be easier.
 
Old 12-14-2007, 10:13 PM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
see here for similar situation.
 
Old 12-16-2007, 05:41 PM   #5
demxkn66
LQ Newbie
 
Registered: Nov 2007
Location: The Netherlands
Posts: 8

Original Poster
Rep: Reputation: 0
Question

Quote:
Originally Posted by radoulov View Post
If you post a sample from your input files and the desired output,
it would be easier.
file1:
carrier a
carrier b
carrier c
carrier d

file2:
carrier a item 1
carrier a item 2
carrier a item 3
carrier a item 4
carrier a item 5
carrier a item 6
carrier a item 7
carrier a item 8
carrier a item 9
carrier a item 10

file 1 1st line is read 'carrier a', then file 2 is read line by line and some processing is done. Finally script stops after processing 1st line of file1, 2nd line until last line of file1 aren't processed anymore altough it supposed to do so.

What can be the case why these aren't processed anymore?

Thanks to you all contributors. :-)
 
Old 12-16-2007, 06:10 PM   #6
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Strange... your original code works for me, using BASH 3.2.25. Anyway, you can try to explicitly assign file descriptors and keep input separate (use -u option to read from one descriptor or another)
Code:
#!/bin/bash
exec 3< file1
while read -u 3 line
do
   echo $line
   exec 4< file2
   while read -u 4 line
   do
      echo $line
   done
   exec 4<&-
done
exec 3<&-
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
for loop cat problem jong357 Slackware 4 04-09-2007 01:05 PM
cat in asm/ cat --show-all option Tux Linux - Software 1 09-02-2006 10:31 PM
Method readLine() in Java...help KissDaFeetOfSean Programming 1 09-09-2005 01:41 AM
readline compile feetyouwell Programming 1 10-06-2004 07:01 PM
readline problem when compiling dark_forests Linux - Software 0 10-12-2003 05:13 PM


All times are GMT -5. The time now is 06:34 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration