Sequential Reading from two file in a loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sequential Reading from two file in a loop
# 1  
Old 08-11-2017
Sequential Reading from two file in a loop

Hello All,

I have two files with me file1.txt and file2.txt
Code:
file1.txt has:

333
222
111

file2.txt has

ccc
bbb
aaa

ccc is related to 333 only, bbb is related to 222 only and aaa is related to 111 only.

I have to get the values from each of the file and pass them in the URL like:

Code:
https://red.com/$(first value from file1.txt)/${first value from file2.txt}
https://red.com/$(second value from file1.txt)/${second value from file2.txt}

I wrote a nested for reading each of them line by line but obviously, the inner loop for file2 runs 3 times for processing first record of the first loop.

Any help please?
# 2  
Old 08-11-2017
Please become accustomed to provide decent context info of your problem.
It is always helpful to support a request with system info like OS and shell, related environment (variables, options), preferred tools, adequate (representative) sample input and desired output data and the logics connecting the two, and, if existent, system (error) messages verbatim, to avoid ambiguities and keep people from guessing.

Should your desire be to solve above in shell, and yours is bourne compatible, there are many threads dealing with and presenting solutions to your problem. A very simplified approach would be to use redirection from extra file descriptors:
Code:
exec 3<file1 4<file2
while read X <&3 && read Y <&4; do echo $Y $X; done
ccc 333
bbb 222
aaa 111
exec 3<&- 4<&-


Last edited by RudiC; 08-14-2017 at 09:42 AM..
This User Gave Thanks to RudiC For This Post:
# 3  
Old 08-11-2017
Have you tried the paste command?
Code:
paste file1.txt file2.txt

The output will be from the same line number from each file separated with a tab unless you choose some other character. The output can be piped or redirected like other unix commands.
Code:
ccc\t333
bbb\t222
aaa\t111

This User Gave Thanks to wbport For This Post:
# 4  
Old 08-14-2017
The shell has builtin output formatting.
With redirection of the while block there is no need to cancel the redirection afterwards
Code:
while read val1 <&3 && read val2 <&4
do
  echo "https://red.com/$val1/$val2"
done 3<file1.txt 4<file2.txt

This User Gave Thanks to MadeInGermany For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Removing \r and \n during reading file through while loop

Hi, I am writing in a file through cat command. This file will contain the path of file along with filename. e.g. /home/user/folder1/folder2/filename.txt There might be very large number of this path in same file like say 140 when I try to run while command: while read -r file do //command... (8 Replies)
Discussion started by: Pulkit Lall
8 Replies

2. Shell Programming and Scripting

reading the values from a file in C Shell for loop

Hi All, I need small help on for loop syntax in C shell. How can we read the values from a file (line by line) through C shell loop. For Ex: $Cat file1 data1 data2 data3 data4 $ I have to print those values in a variable and have to perform some steps... Can anyone help on... (2 Replies)
Discussion started by: raghu.iv85
2 Replies

3. Shell Programming and Scripting

Loop is not reading tabs from the file

Hi, I am on HP-UX and K shell. When I am using while/for loop for reading a file. It is working fine but not reading tabs: Suppose, if the line is: ; ;COMP; ; ; ; then loop is reading as ; ;COMP; ;... (5 Replies)
Discussion started by: ezee
5 Replies

4. Hardware

checking non sequential reading/writing of hard drive

what command check that? Does it depend in the time it take to access the file? (1 Reply)
Discussion started by: programAngel
1 Replies

5. Programming

Tool to simulate non-sequential disk I/O (simulate db file sequential read) in C POSIX

Writing a Tool to simulate non-sequential disk I/O (simulate db file sequential read) in C POSIX I have over the years come across the same issue a couple of times, and it normally is that the read speed on SAN is absolutely atrocious when doing non-sequential I/O to the disks. Problem being of... (7 Replies)
Discussion started by: vrghost
7 Replies

6. UNIX for Advanced & Expert Users

File Status 92 reading a sequential file

Hi. I have a process that execute a Microfocus Cobol. This process read a large sequential file and update or insert in a table Oracle. This process run smoothly in some ocasions but in a file whor have more than 400,000 registers, the cobol program doesn't finish read all the records,... (3 Replies)
Discussion started by: hornam
3 Replies

7. 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

8. Shell Programming and Scripting

Not access variable outside loop when a reading a file

I am writing a shell script using the korn shell. It seems that I am only able to use local variables within a while loop that is reading a file. (I can't access a variable outside a previously used while loop.) It's been a while since I wrote shell scripts. Here is a sample cat file.txt... (4 Replies)
Discussion started by: ricardo.ludwig
4 Replies

9. UNIX for Dummies Questions & Answers

How to make a loop base on reading a file?

To make it clearer: I have a file, List.txt List.txt contains: (these are actually splitted files w/c I got from ls command and dump them to the List.txt file) SAMPLEa SAMPLEb SAMPLEc SAMPLEd SAMPLEe SAMPLEf . . . . . And I want to rename these files to have a .dat extension.... (3 Replies)
Discussion started by: JohnBalayo
3 Replies

10. Programming

Reading special characters while converting sequential file to line sequential

We have to convert a sequential file to a 80 char line sequential file (HP UX platform).The sequential file contains special characters. which after conversion of the file to line sequential are getting coverted into "new line" or "tab" and file is getting distorted. Is there any way to read these... (2 Replies)
Discussion started by: Rajeshsu
2 Replies
Login or Register to Ask a Question