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
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

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 06-06-2006
madhunk madhunk is offline
Registered User
  
 

Join Date: Nov 2005
Posts: 91
Using NAWK to merge two files

Two files need to be joined on a second column.

File 1
PHP Code:
          FRO     FRONTIER
    MWC     MIDWEST
    ORB     ORBITZ
    ATR     AIRTRAN
    BWI     BESTWESTERN 
File 2
PHP Code:
           M       AIRTRAN 8       TRIPS_YTD       A
    M       BESTWESTERN 8       TRIPS_YTD       B       20      TRIPS_TOTAL     A       17
    M       FRONTIER 8
    M       CAESAR 8       TRIPS_YTD       D       10
    M       ORBITZ 8       TRIPS 
Desired Output
PHP Code:
          ATR       AIRTRAN 8       TRIPS_YTD       A
    BWI       BESTWESTERN 8       TRIPS_YTD       B       20      TRIPS_TOTAL     A       17
    FRO       FRONTIER 8
    ORB       ORBITZ 8       TRIPS 
I am not a big expert in nawk... Any inputs are highly appreciated....

Code:
      nawk -f merge.awk f1.txt f2.txt
      
        BEGIN {
          FS=" "
          OFS=" "
        }
        
        FNR==NR {
          arr[FNR] = $1 OFS $2
          next
        }
        {
           print arr[FNR], $2
    }
Output

PHP Code:
    ATR AIRTRAN AIRTRAN
    BWI BESTWESTERN BESTWESTERN
    FRO FRONTIER CAESAR
    MWC MIDWEST FRONTIER
    ORB ORBITZ ORBITZ 
Please advise...
  #2 (permalink)  
Old 06-06-2006
madhunk madhunk is offline
Registered User
  
 

Join Date: Nov 2005
Posts: 91
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
It was able to produce the required output....but the output file is not tab delimited...

Please help me with the best way to do this...
  #3 (permalink)  
Old 06-06-2006
jim mcnamara jim mcnamara is online now Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,724
what if you piped into one last awk:
Code:
....... lastfilename | awk 'BEGIN {OFS="\t"} { print $0 }' > f3.dat 
# or a tr
....... lastfilename | tr -s ' ' '\t' > f3.dat
  #4 (permalink)  
Old 06-06-2006
madhunk madhunk is offline
Registered User
  
 

Join Date: Nov 2005
Posts: 91
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
But is this the right way to merge two files?
  #5 (permalink)  
Old 06-06-2006
jim mcnamara jim mcnamara is online now Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,724
Merge means to "intertwine" records - join means to put two records together into one - your choice of join is the best way to do it, because that's what join is for.
  #6 (permalink)  
Old 06-06-2006
madhunk madhunk is offline
Registered User
  
 

Join Date: Nov 2005
Posts: 91
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
Please advise
  #7 (permalink)  
Old 06-07-2006
Ygor's Avatar
Ygor Ygor is offline Forum Staff  
Moderator
  
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,407
Try..
Code:
$ cat f1.txt
FRO     FRONTIER
    MWC     MIDWEST
    ORB     ORBITZ
    ATR     AIRTRAN
    BWI     BESTWESTERN
$ cat f2.txt
M       AIRTRAN 8       TRIPS_YTD       A
    M       BESTWESTERN 8       TRIPS_YTD       B       20      TRIPS_TOTAL     A       17
    M       FRONTIER 8
    M       CAESAR 8       TRIPS_YTD       D       10
    M       ORBITZ 8       TRIPS
$ awk 'NR==FNR{a[$2]=$1;next}a[$2]&&sub($1,a[$2])' f1.txt f2.txt > f3.txt
$ cat f3.txt
ATR       AIRTRAN 8       TRIPS_YTD       A
    BWI       BESTWESTERN 8       TRIPS_YTD       B       20      TRIPS_TOTAL     A       17
    FRO       FRONTIER 8
    ORB       ORBITZ 8       TRIPS
$
Sponsored Links
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 05:03 PM.


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