![]() |
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 |
| how to merge these two files? | fedora | Shell Programming and Scripting | 3 | 02-12-2008 06:45 PM |
| merge files | koti_rama | Shell Programming and Scripting | 5 | 12-24-2007 10:59 PM |
| use of sed over cat to merge files | miwinter | UNIX for Advanced & Expert Users | 2 | 11-28-2007 01:36 PM |
| How to merge files | lestat_ecuador | Shell Programming and Scripting | 3 | 06-07-2007 06:45 AM |
| help in merge files | u263066 | Shell Programming and Scripting | 5 | 07-24-2006 03:24 AM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
||||
|
Using NAWK to merge two files
Two files need to be joined on a second column.
File 1 PHP Code:
PHP Code:
PHP Code:
Code:
nawk -f merge.awk f1.txt f2.txt
BEGIN {
FS=" "
OFS=" "
}
FNR==NR {
arr[FNR] = $1 OFS $2
next
}
{
print arr[FNR], $2
}
PHP Code:
|
|
||||
|
I also tried to do this way
Code:
#!/usr/bin/ksh
awk '
BEGIN { OFS = " " } { print $2 , $1, $2 }
' f1.txt > temp_f1.txt
awk '
BEGIN { OFS = " " } { print $2 , $2, $3 ,$4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20 }
' f2.txt > temp_f2.txt
join -t " " -j1 1 -j2 1 -o 1.2 1.3 2.3 2.4 2.5 2.6 2.7 2.8 2.9 2.10 2.11 2.12 2.13 2.14 2.15 2.16 2.17 2.18 2.19 2.20 2.21 2.22 2.23 2.24 2.25 temp_f1.txt te
mp_f2.txt > f3.dat
Please help me with the best way to do this... |
|
||||
|
Thank you Jim...
Somehow the awk one didn't work...But the tr function worked... Code:
#!/usr/bin/ksh
awk '
BEGIN { OFS = "\t" } { print $2 , $1, $2 }
' f1.txt > temp_f1.txt
awk '
BEGIN { OFS = "\t" } { print $2 , $2, $3 ,$4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21, $22, $23, $24, $25, $26, $27 }
' f2.txt > temp_f2.txt
join -j1 1 -j2 1 -o 1.2 1.3 2.3 2.4 2.5 2.6 2.7 2.8 2.9 2.10 2.11 2.12 2.13 2.14 2.15 2.16 2.17 2.18 2.19 2.20 2.21 2.22 2.23 2.24 2.25 2.26 2.27 temp_f1.tx
t temp_f2.txt | tr -s ' ' '\t' > f3.dat
|
|
||||
|
Thank you again Jim...
In the above script, I have declared variables till $27 for File 2. File 1 is standard and it has only two columns. However, File 2 will be changing on the number of columns. If I get column $28 in File2, I would not be outputting field 28 with the current code.. Is there any way to enhance the code so that it will take any number of columns from File 2 rather than hard coding in the script... My final script looks like this.. Code:
#!/usr/bin/ksh
DIR=$1
CODES_FILE=$2
METADATA_FILE=$3
cd ${DIR}
sort -k 2,2 ${CODES_FILE} > f1.dat
sort -k 2,2 ${METADATA_FILE} > f2.dat
awk '
BEGIN { OFS = "\t" } { print $2 , $1, $2 }
' f1.dat > temp_f1.dat
awk '
BEGIN { OFS = "\t" } { print $2 , $2, $3 ,$4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21, $22, $23, $24, $25, $26, $27 }
' f2.dat > temp_f2.dat
join -j1 1 -j2 1 -o 1.2 1.3 2.3 2.4 2.5 2.6 2.7 2.8 2.9 2.10 2.11 2.12 2.13 2.14 2.15 2.16 2.17 2.18 2.19 2.20 2.21 2.22 2.23 2.24 2.25 2.26 2.27 temp_f1.da
t temp_f2.dat | tr -s ' ' '\t' > f3.dat
|
| Sponsored Links | ||
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|