ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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?
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<&-
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.