Need shell script to read two file at same time and print out in single file | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Need shell script to read two file at same time and print out in single file

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 03-07-2009
sreedhargouda sreedhargouda is offline
Registered User
 
Join Date: Mar 2009
Last Activity: 16 July 2011, 2:39 PM EDT
Posts: 21
Thanks: 0
Thanked 1 Time in 1 Post
Data Need shell script to read two file at same time and print out in single file

Need shell script to read two file at same time and print output in single file


Example I have two files 1) file1.txt 2) file2.txt

File1.txt contains

Aaa
Bbb
Ccc
Ddd
Eee
Fff


File2.txt contains

Zzz
Yyy
Xxx
www
vvv
uuu


I need output file like output.txt

Aaa
Zzz
-----
Bbb
Yyy
----
Ccc
Xxx
----
Ddd
www


please help me any one


Sponsored Links
    #2  
Old 03-07-2009
mjd_tech mjd_tech is offline
Registered User
 
Join Date: Mar 2009
Last Activity: 17 August 2009, 10:45 AM EDT
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
There are probably a million better ways to do this...
but here goes...

Code:
#!/bin/sh
linecount1=$(wc -l file1.txt | cut -d\  -f 1 )
linecount2=$(wc -l file2.txt | cut -d\  -f 1 )
i=1 j=1
while [ $i -le $linecount1 -a $j -le $linecount2 ]; do
    sed -n -e "${i}p" file1.txt >> output.txt
    sed -n -e "${j}p" file2.txt >> output.txt
    echo "-----" >> output.txt
    i=$(( i + 1 )); j=$(( j + 1))
done

I had to do the cut stuff when assigning linecount, because my wc -l prints the file name after the number of lines. (Debian)

Regards,
MD
Sponsored Links
    #3  
Old 03-08-2009
ripat ripat is offline Forum Advisor  
Registered User
 
Join Date: Oct 2006
Last Activity: 17 February 2014, 7:51 AM EST
Location: Belgium
Posts: 537
Thanks: 4
Thanked 38 Times in 28 Posts
With (g|n|m)awk


Code:
awk 'NR==FNR{_[NR]=$0}NR!=FNR{print _[FNR] "\n" $0 "\n---"}' file1 file2

    #4  
Old 03-08-2009
mjd_tech mjd_tech is offline
Registered User
 
Join Date: Mar 2009
Last Activity: 17 August 2009, 10:45 AM EDT
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
ripat,

I knew there would be a better way, most likely awk.
Here, I try to explain how it works...

Code:
awk '
NR==FNR {_[NR]=$0}  
                    # NR is total number of lines read in both files.
                    # FNR is number of lines read, reset for each file.
                    # _[NR] is an array named _ with NR as index.
                    # This loads array with contents of file1,
                    # and ignores file2.
                    
NR!=FNR {print _[FNR] "\n" $0 "\n---"}
                    # This part ignores file1.
                    # For each line in file 2,
                    # Print the corresponding line from the array,
                    # using FNR as index.
                    # then a newline, then the line from file2,
                    # another newline, then the dashes, then another newline.
' file1 file2

Hopefully, this is a correct explanation.
I was initially confused by the _[NL] thing, thinking this was some kind of special awk reserved variable or something, but then I realized it's just an array named _
I guess that if file1 were super huge, you could run out of memory because it gets loaded into an array. There's probably a way to do this in perl that can handle huge files without slurping into an array...

Regards,
MD
Sponsored Links
    #5  
Old 03-08-2009
ripat ripat is offline Forum Advisor  
Registered User
 
Join Date: Oct 2006
Last Activity: 17 February 2014, 7:51 AM EST
Location: Belgium
Posts: 537
Thanks: 4
Thanked 38 Times in 28 Posts
Quote:
Originally Posted by mjd_tech View Post
Hopefully, this is a correct explanation.
That's exactly it.
Quote:
Originally Posted by mjd_tech View Post
I was initially confused by the _[NL] thing, thinking this was some kind of special awk reserved variable or something, but then I realized it's just an array named _
_ has no special meaning. It's just a fast and, I confess, obfuscated way of writing array names.

Quote:
Originally Posted by mjd_tech View Post
I guess that if file1 were super huge, you could run out of memory because it gets loaded into an array. There's probably a way to do this in perl that can handle huge files without slurping into an array...
Well, maybe. However, awk never hit the memory barrier on my systems even on large test files (1 Mega lines and more). But you are probably right. You could do that by parallel looping through two file handles in perl.
Sponsored Links
    #6  
Old 03-08-2009
Franklin52's Avatar
Franklin52 Franklin52 is offline Forum Staff  
Moderator
 
Join Date: Feb 2007
Last Activity: 28 July 2014, 1:54 PM EDT
Location: The Netherlands
Posts: 7,692
Thanks: 120
Thanked 531 Times in 502 Posts
Quote:
Originally Posted by mjd_tech View Post
I guess that if file1 were super huge, you could run out of memory because it gets loaded into an array.

Code:
awk '{print;getline < "file1";print $0 "\n---"}' file2

Assuming the files have the same number of lines.

Regards
Sponsored Links
    #7  
Old 03-08-2009
sreedhargouda sreedhargouda is offline
Registered User
 
Join Date: Mar 2009
Last Activity: 16 July 2011, 2:39 PM EDT
Posts: 21
Thanks: 0
Thanked 1 Time in 1 Post
thank you all for quick reply
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
search file and print results with shell script dodasajan Shell Programming and Scripting 3 02-07-2009 09:42 AM
how to write a shell script that print the last modified file ? FunnyWolF Shell Programming and Scripting 5 08-13-2008 02:25 AM
Executing Multiple .SQL Files from Single Shell Script file anushilrai Shell Programming and Scripting 3 04-07-2008 10:09 AM
search for the contents in many file and print that file using shell script cdfd123 Shell Programming and Scripting 3 10-07-2007 10:17 PM
why shell scripting takes more time to read a file brkavi_in Shell Programming and Scripting 1 06-23-2006 08:20 AM



All times are GMT -4. The time now is 06:43 AM.