Computing.Net > Forums > Unix > Script to read from 2 files

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Script to read from 2 files

Reply to Message Icon

Name: poornimajayan
Date: October 23, 2009 at 09:16:15 Pacific
OS: KSH
Subcategory: Software Problems
Comment:

Hi Gurus,
I have 2 files--1.txt & 2.txt both have lots of filenames.
Ex:1.txt has (a.txt b.txt c.txt,...as separate lines) 2.txt has (d.txt,f.txt,... as separate lines). Both won't have same names.
I need to write a ksh script where using a loop, I want to read the first line of first file and process it (may be write to another file 3.txt).After the first line of first file, I should read first line of second file and process(may be add to 3.txt).Then read second line of first file and process(add to 3.txt) and then second line of second file,...and go on.when data of one file is over, then keep on adding the second file data only or viceversa.How can I loop this?Please let me know.

I started with only looping of file 1 alone as follows:
for i in `cat 1.txt`;do
echo $i >> 3.txt
done

Any thoughts?
Thanks,
Poornima



Sponsored Link
Ads by Google

Response Number 1
Name: lankrypt0
Date: October 23, 2009 at 11:50:17 Pacific
Reply:

Try this:
#!/usr/bin/ksh
x=1
en=$(cat 1.txt|wc -l)
until [[ $x -gt $en ]];do
head -$x 1.txt|tail -1
head -$x 2.txt|tail -1
x=$(($x+1))
done


0
Reply to Message Icon

Related Posts

See More






Use following form to reply to current message:

Login or Register to Reply
LoginRegister


Sponsored links

Ads by Google


Results for: Script to read from 2 files

reading from a file in unix www.computing.net/answers/unix/reading-from-a-file-in-unix/7355.html

Script to Read a .CSV file www.computing.net/answers/unix/script-to-read-a-csv-file-/7784.html

Writing to/reading from 3, 4 etc. www.computing.net/answers/unix/writing-toreading-from-3-4-etc/7388.html