Read two files at a time.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Read two files at a time.
# 1  
Old 06-22-2009
Read two files at a time.

Hi friends,
It's urgent. I am facing a problem. I want to read two files at a time, i mean line by line.

My requirement is like , pls here find below

while read line1fromfile1;do
echo $line1fromfile1|
.. echo $line1fromfile2---How i will do this

done< file 1

can anyone help me out. Pls don't use perl.

Thanks
Pritish
# 2  
Old 06-22-2009
Use for loop,

for i in `cat file1`
do
<< commands>>
for j in `cat file2`
do
<< commands>>
done
done

This might be useful...
# 3  
Old 06-22-2009
Code:
C1=1
C2=1
exec 3< my_second_file
while read LINE1; do
  read LINE2 <&3
  echo "Line $C1 from file 1: $LINE1"
  echo "  Line $C2 from file 2: $LINE2"
  let C1=$C1+1
  let C2=$C2+1
done < my_first_file


Last edited by vgersh99; 06-22-2009 at 10:14 AM.. Reason: code tags, PLEASE!
# 4  
Old 06-22-2009
To keep the forums high quality for all users, please take the time to format your posts correctly.

First of all, use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags [code] and [/code] by hand.)

Second, avoid adding color or different fonts and font size to your posts. Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.

Third, be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums
# 5  
Old 06-22-2009
Lightbulb Thanks a lot yarr

It's working..Smilie

I also tried for a logic, but it's very complex.

cnt=1;
while read line1_f_1;do
line1_f_2=$(head -$cnt|tail +$cnt file2)
echo $line1_f_$line1_f_2 >> tmp_file
cnt=$(expr $cnt + 1)

done < file1
# 6  
Old 06-22-2009
Not clear what you output should look like. The paste command is a candidate.

Code:
man paste

For example.

paste -d'|' file1.txt file2.txt

file 1 line 1|file 2 line 1
file 1 line 2|file 2 line 2
file 1 line 3|file 2 line 3

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to read multiple files at same time through UNIX scripting?

How to read multiple files at simultaneously? (1 Reply)
Discussion started by: Priyanka_M
1 Replies

2. IP Networking

Read() time out in socket programming

Hi, When can the read() system call gives timeout error when the same connection worked for writing data to the other end of the socket, while the next call with read() gives timeout error? Can anyone please explain when this kind of situation appears? Thanks, Sanzee (2 Replies)
Discussion started by: sanzee007
2 Replies

3. Shell Programming and Scripting

Read two lines at time from a file

Hello community, what I need to do is read 2 rows at time from a file. I have this simple solution: File to read: LINE1 LINE2 LINE3 LINE4 LINE5 LINE6 LINE7 LINE8Read routine:#!/bin/ksh sed '1,3d' /out.txt | while read line; do read line2 echo $line $line2 doneResult:LINE1... (5 Replies)
Discussion started by: Lord Spectre
5 Replies

4. Shell Programming and Scripting

Help with Bash piped while-read and a read user input at the same time

Hi I am new to writing script and want to use a Bash Piped while-read and read from user input. if something happens on server.log then do while loop or if something happend on user input then do while loop. Pseudocode something like: tail -n 3 -f server.log | while read serverline || read... (8 Replies)
Discussion started by: MyMorris
8 Replies

5. Shell Programming and Scripting

File read time

hi, how to check the file latest read time.. (2 Replies)
Discussion started by: rsivasan
2 Replies

6. UNIX for Advanced & Expert Users

read() wont allow me to read files larger than 2 gig (on a 64bit)

Hi the following c-code utilizing the 'read()' man 2 read method cant read in files larger that 2gig. Hi I've found a strange problem on ubuntu64bit, that limits the data you are allowed to allocate on a 64bit platform using the c function 'read()' The following program wont allow to allocate... (14 Replies)
Discussion started by: monkeyking
14 Replies

7. Shell Programming and Scripting

How to read max of 10 file at a time?

Hi All, Please advise . Welcome more suggestions. For examples, I have 1000 file with prefix x??? In fact, I want to convert them to x???.txt with max 10 files at a time. As such, I will need to call another script to read from those 10 *txt files and sleep 5000 to convert the next 10 again.... (10 Replies)
Discussion started by: cedrichiu
10 Replies

8. Shell Programming and Scripting

read a list one at a time

just have a muddled head at the moment... bare with me. say i have a variable $count... and a list in a file i want to use the $count line from that textfile but don't seem to recall how to access it? eg user=`cat file1.txt` # user to be $count line in that list? eg david... (7 Replies)
Discussion started by: nortypig
7 Replies

9. Shell Programming and Scripting

Terminal Hungup at the time of read

Hi, There are two scripts. The second script is called from the first one. These will create two processes on unix. In the second script, there is a read statement in the while loop. Under unexpected conditions, at the time of reading response from the user, if the terminal hungup happens,... (1 Reply)
Discussion started by: pkusumam
1 Replies

10. UNIX for Dummies Questions & Answers

How to read and write files one line at a time.

Hi! All! I am wirting a shell script in which i want to read one line at a time from the file and write it simultaneouly to other file one line at a time. Please let me know about some shell utility which can help me out. Thanx. If further clarifications are needed then please let me know... (2 Replies)
Discussion started by: s_chopra
2 Replies
Login or Register to Ask a Question