![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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
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! |
|
|||||
|
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.
. |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|