The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Script to place selected columns from a group of files side by side in a new file ks_reddy Shell Programming and Scripting 8 02-12-2009 12:09 PM
Merge 3 columns side by side amaulana Shell Programming and Scripting 7 02-08-2009 02:50 AM
Merge 2 text files to one text file side by side ahinkebein Shell Programming and Scripting 15 02-04-2009 11:28 AM
How to Merge Two .xls files side by side into a single .xls jagadish99 Shell Programming and Scripting 2 09-24-2008 06:44 AM
How to Merge Two .xls files side by side into a single .xls jagadish99 Shell Programming and Scripting 0 08-27-2008 06:38 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-27-2009
cajunfries cajunfries is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 7
How to Merge / combine / join / paste 2 text files side-by-side

I have 2 text files, both have one simple, single column. The 2 files might be the same length, or might not, and if not, it's unknown which one would be longer.

For this example, file1 is longer:

---file1
Joe
Bob
Mary
Sally
Fred
Elmer
David

---file2
Tomato
House
Car
Elephant

All I want is to paste them side-by side into a new file like so:
No logic, no matching - just a simple paste, with a simple tab formatting to separate the 2 columns.

---file3
Joe Tomato
Bob House
Mary Car
Sally Elephant
Fred
Elmer
David

I've tried the "paste" command, like "paste file1 file2 > file3" but it works works only if file1 is longer than or the same length as file2. If file2 is longer, the extra values are misaligned too far over to the left and appear to belong to file1 as follows:

---file3
Tomato Joe
House Bob
Car Mary
Elephant Sally
Fred
Elmer
David

Also tried this awk I found in the forums, but it works only if file1 is longer or same length as file2 - it truncates file2 if file1 is shorter.
Code:
awk 'NR==FNR{_[NR]=$0;next}{print $1,$2,_[FNR]}' file2 file1
---file3
Tomato Joe
House Bob
Car Mary
Elephant Sally

I don't care if the solution is awk, sed, shell script, or what.
Reading both files into an array might be the solution, but I don't know enough on how to write the code for that.

Any help is greatly appreciated, thanks!
  #2 (permalink)  
Old 04-27-2009
rubin's Avatar
rubin rubin is offline Forum Advisor  
Registered User
  
 

Join Date: Nov 2007
Posts: 321
Quote:
Originally Posted by cajunfries View Post
...
All I want is to paste them side-by side into a new file like so:
No logic, no matching - just a simple paste, with a simple tab formatting to separate the 2 columns.
...
Also tried this awk I found in the forums, but it works only if file1 is longer or same length as file2 - it truncates file2 if file1 is shorter.
...

Take a look at awk's printf :

Code:
awk 'NR==FNR { a[c=FNR]=$0; next }
     { printf "%-8s\t%s\n", a[FNR], $0 } 
     END { for(i=FNR+1;i<=c;i++) print a[i] }' file2 file1
  #3 (permalink)  
Old 04-27-2009
cajunfries cajunfries is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 7
yes!!

Thanks rubin!!

That works perfectly - regardless as to which of the 2 files is longer.

Exactly what I've been looking for. (Now if I could just understand it)

Thanks again!!
  #4 (permalink)  
Old 04-27-2009
rubin's Avatar
rubin rubin is offline Forum Advisor  
Registered User
  
 

Join Date: Nov 2007
Posts: 321
Code:
NR==FNR { a[c=FNR]=$0; next }
{ printf "%-8s\t%s\n", a[FNR], $0 }  

This part is similar to the code you posted, there are many threads here that explain it, you can search the forums using the keyword NR==FNR.
Regarding printf, check this: The GNU Awk User's Guide
Code:
END { for(i=FNR+1;i<=c;i++) print a[i] }'

This last section will print the remaining records of the file that has more records, already stored at the NR==FNR part, and it'll start after
all records of the smaller file are exhausted. This part will get executed only if say file2  has more records than file1, otherwise it won't
run at all.
Hope I made it clear .
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 11:38 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0