Visit Our UNIX and Linux User Community


merging of 2 files AWK, SHELL or something else


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting merging of 2 files AWK, SHELL or something else
# 15  
Old 01-17-2008
Hi, thanks for helping me, i try to explain it the best i can,

AWK FILE : match_files2_o.awk

BEGIN { printf "%-5s|%-10s|%-9s|%-9s|%-9s|%-70s|%-9s|%-s\n","Count", "VT", "Date", "Time","From","Alert Message","Instance",""}
NR == FNR {
x[$1,$7]
y[FNR] = $2
next
}
!(($1,$7) in x) {
if (y[FNR]) {
$2 = y[FNR]
}
{ printf "%-5s|%-10s|%-9s|%-9s|%-9s|%-70s|%-9s|%-s\n", $1, $2, $3, $4, $6, $7, $8, ""}
}

FILE 2: managed_event_log.170108

12~13432013~ 17-01-08~10:02:32~2~KAZUAT1~kaz/switch/bin/ifstart for KAZUAT1 TOO MANY PROCESSES 001459~emeflxci~FLEX_daemons~SWITCH:KAZ:T01~CRITICAL~~100
12~13432030~ 17-01-08~10:02:32~1~KAZUAT1~ifreader_KZ for KAZUAT1 NOT EXIST 001459~emeflxci~FLEX_daemons~SWITCH:KAZ:T01~CRITICAL~~100

FILE 1: all_event_log.170108
14~~ 17-01-08~10:02:32~2~KAZUAT1~kaz/switch/bin/ifstart for KAZUAT1 TOO MANY PROCESSES 001459~emeflxci~FLEX_daemons~SWITCH:KAZ:T01~CRITICAL~~100
12~~ 17-01-08~10:02:32~1~KAZUAT1~ifpoll_KZ for KAZUAT1 NOT EXIST 001459~emeflxci~FLEX_daemons~SWITCH:KAZ:T01~CRITICAL~~100
12~~ 17-01-08~10:02:32~1~KAZUAT1~ifreader_KZ for KAZUAT1 NOT EXIST 001459~emeflxci~FLEX_daemons~SWITCH:KAZ:T01~CRITICAL~~100
14~~ 17-01-08~10:02:32~2~ISRUAT3~isr/switch/bin/ifstart for ISRUAT3 TOO MANY PROCESSES 102802~emeflxci~FLEX_daemons~SWITCH:ISR:T01~CRITICAL~~100
1~~ 17-01-08~00:03:52~1~AUTOSYS~T01_ADM_155700_YYYYMMDD_C~T01~autosys~~JOBFAILURE~~
1~~ 17-01-08~00:03:10~1~AUTOSYS~T01_ADM_BO_yy-mm-dd_today_C~T01~autosys~~JOBFAILURE~~
1~~ 17-01-08~00:02:46~1~AUTOSYS~T01_ADM_BO_yy_mm_dd_today_C~T01~autosys~~JOBFAILURE~~
1~~ 17-01-08~00:03:11~1~AUTOSYS~T01_ADM_BO_yymmdd_today_C~T01~autosys~~JOBFAILURE~~
1~~ 17-01-08~00:03:08~1~AUTOSYS~T01_ADM_BO_yyyy-mm-dd_today_C~T01~autosys~~JOBFAILURE~~
1~~ 17-01-08~00:03:05~1~AUTOSYS~T01_ADM_BO_yyyy_mm_dd_today_C~T01~autosys~~JOBFAILURE~~
1~~ 17-01-08~00:03:04~1~AUTOSYS~T01_ADM_BO_yyyymmdd_today_C~T01~autosys~~JOBFAILURE~~
1~~ 17-01-08~00:05:39~1~AUTOSYS~T01_ADM_DateGvars_B~T01~autosys~~JOBFAILURE~~
1~~ 17-01-08~00:02:43~1~AUTOSYS~T01_ADM_GVAR_10235_DDMMYY_TODAY_C~T01~autosys~~JOBFAILURE~~
1~~ 17-01-08~00:04:17~1~AUTOSYS~T01_ADM_GVAR_149388_DATEDDMMMYYYY_C~T01~autosys~~JOBFAILURE~~
1~~ 17-01-08~00:04:23~1~AUTOSYS~T01_ADM_GVAR_149388_DATEMMMDD_C~T01~autosys~~JOBFAILURE~~
1~~ 17-01-08~00:03:11~1~AUTOSYS~T01_ADM_GVAR_35612_DATEyyyymmdd_LASTBUSDAY_C~T01~autosys~~JOBFAILURE~~
1~~ 17-01-08~00:04:57~1~AUTOSYS~T01_ADM_make_priv_C~T01~autosys~~JOBFAILURE~~

##My Comments: in FILE 1 line with $7="kaz/switch/bin/ifstart for KAZUAT1 TOO MANY PROCESSES" $1 increase to 14 [ when in FILE 2 is 12 ]

CURRENT OUTPUT:
Count|VT |Date |Time |From |Alert Message |Instance |
14 | | 17-01-08|10:02:32 |KAZUAT1 |kaz/switch/bin/ifstart for KAZUAT1 TOO MANY PROCESSES 001459 |emeflxci |
12 | | 17-01-08|10:02:32 |KAZUAT1 |ifpoll_KZ for KAZUAT1 NOT EXIST 001459 |emeflxci |
14 |13432030 | 17-01-08|10:02:32 |ISRUAT3 |isr/switch/bin/ifstart for ISRUAT3 TOO MANY PROCESSES 102802 |emeflxci |
1 | | 17-01-08|00:05:39 |AUTOSYS |T01_ADM_DateGvars_B |T01 |
1 | | 17-01-08|00:04:57 |AUTOSYS |T01_ADM_make_priv_C |T01 |
1 | | 17-01-08|00:04:23 |AUTOSYS |T01_ADM_GVAR_149388_DATEMMMDD_C |T01 |
1 | | 17-01-08|00:04:17 |AUTOSYS |T01_ADM_GVAR_149388_DATEDDMMMYYYY_C |T01 |
1 | | 17-01-08|00:03:52 |AUTOSYS |T01_ADM_155700_YYYYMMDD_C |T01 |
1 | | 17-01-08|00:03:11 |AUTOSYS |T01_ADM_GVAR_35612_DATEyyyymmdd_LASTBUSDAY_C |T01 |
1 | | 17-01-08|00:03:11 |AUTOSYS |T01_ADM_BO_yymmdd_today_C |T01 |
1 | | 17-01-08|00:03:10 |AUTOSYS |T01_ADM_BO_yy-mm-dd_today_C |T01 |
1 | | 17-01-08|00:03:08 |AUTOSYS |T01_ADM_BO_yyyy-mm-dd_today_C |T01 |
1 | | 17-01-08|00:03:05 |AUTOSYS |T01_ADM_BO_yyyy_mm_dd_today_C |T01 |
1 | | 17-01-08|00:03:04 |AUTOSYS |T01_ADM_BO_yyyymmdd_today_C |T01 |
1 | | 17-01-08|00:02:46 |AUTOSYS |T01_ADM_BO_yy_mm_dd_today_C |T01 |
1 | | 17-01-08|00:02:43 |AUTOSYS |T01_ADM_GVAR_10235_DDMMYY_TODAY_C |T01 |

##My Comments: in OUTPUT line with $7="kaz/switch/bin/ifstart for KAZUAT1 TOO MANY PROCESSES" exist but $2is null [ should be 13432013 - $2 from FILE 2 ]
## line where $7="isr/switch/bin/ifstart for ISRUAT3 TOO MANY PROCESSES 102802" have $2=13432030 instead of null [ becouse this line is not present in FILE 2]

##I see that for those 2 lines $1 is same and $7 is similar [ maybe this is the reason ]

EXPECTED OUTPUT :
Count|VT |Date |Time |From |Alert Message |Instance |
14 |13432013 | 17-01-08|10:02:32 |KAZUAT1 |kaz/switch/bin/ifstart for KAZUAT1 TOO MANY PROCESSES 001459 |emeflxci |
12 | | 17-01-08|10:02:32 |KAZUAT1 |ifpoll_KZ for KAZUAT1 NOT EXIST 001459 |emeflxci |
14 | | 17-01-08|10:02:32 |ISRUAT3 |isr/switch/bin/ifstart for ISRUAT3 TOO MANY PROCESSES 102802 |emeflxci |
1 | | 17-01-08|00:05:39 |AUTOSYS |T01_ADM_DateGvars_B |T01 |
1 | | 17-01-08|00:04:57 |AUTOSYS |T01_ADM_make_priv_C |T01 |
1 | | 17-01-08|00:04:23 |AUTOSYS |T01_ADM_GVAR_149388_DATEMMMDD_C |T01 |
1 | | 17-01-08|00:04:17 |AUTOSYS |T01_ADM_GVAR_149388_DATEDDMMMYYYY_C |T01 |
1 | | 17-01-08|00:03:52 |AUTOSYS |T01_ADM_155700_YYYYMMDD_C |T01 |
1 | | 17-01-08|00:03:11 |AUTOSYS |T01_ADM_GVAR_35612_DATEyyyymmdd_LASTBUSDAY_C |T01 |
1 | | 17-01-08|00:03:11 |AUTOSYS |T01_ADM_BO_yymmdd_today_C |T01 |
1 | | 17-01-08|00:03:10 |AUTOSYS |T01_ADM_BO_yy-mm-dd_today_C |T01 |
1 | | 17-01-08|00:03:08 |AUTOSYS |T01_ADM_BO_yyyy-mm-dd_today_C |T01 |
1 | | 17-01-08|00:03:05 |AUTOSYS |T01_ADM_BO_yyyy_mm_dd_today_C |T01 |
1 | | 17-01-08|00:03:04 |AUTOSYS |T01_ADM_BO_yyyymmdd_today_C |T01 |
1 | | 17-01-08|00:02:46 |AUTOSYS |T01_ADM_BO_yy_mm_dd_today_C |T01 |
1 | | 17-01-08|00:02:43 |AUTOSYS |T01_ADM_GVAR_10235_DDMMYY_TODAY_C |T01 |

Regards
PP
# 16  
Old 01-17-2008
First, the output I get is different than yours
(I suppose that there are some characters that disappear
with the copy/paste):

Code:
$ cat match_files2_o.awk 
BEGIN { printf "%-5s|%-10s|%-9s|%-9s|%-9s|%-70s|%-9s|%-s\n","Count", "VT", "Date", "Time","From","Alert Message","Instance",""}
NR == FNR {
x[$1,$7]
y[FNR] = $2
next
}
!(($1,$7) in x) {
if (y[FNR]) {
$2 = y[FNR]
}
{ printf "%-5s|%-10s|%-9s|%-9s|%-9s|%-70s|%-9s|%-s\n", $1, $2, $3, $4, $6, $7, $8, ""}
}
$ cat managed_event_log.170108 
12~13432013~ 17-01-08~10:02:32~2~KAZUAT1~kaz/switch/bin/ifstart for KAZUAT1 TOO MANY PROCESSES 001459~emeflxci~FLEX_daemons~SWITCH:KAZ:T01~CRITICAL~~100
12~13432030~ 17-01-08~10:02:32~1~KAZUAT1~ifreader_KZ for KAZUAT1 NOT EXIST 001459~emeflxci~FLEX_daemons~SWITCH:KAZ:T01~CRITICAL~~100
$ cat all_event_log.170108     
14~~ 17-01-08~10:02:32~2~KAZUAT1~kaz/switch/bin/ifstart for KAZUAT1 TOO MANY PROCESSES 001459~emeflxci~FLEX_daemons~SWITCH:KAZ:T01~CRITICAL~~100
12~~ 17-01-08~10:02:32~1~KAZUAT1~ifpoll_KZ for KAZUAT1 NOT EXIST 001459~emeflxci~FLEX_daemons~SWITCH:KAZ:T01~CRITICAL~~100
12~~ 17-01-08~10:02:32~1~KAZUAT1~ifreader_KZ for KAZUAT1 NOT EXIST 001459~emeflxci~FLEX_daemons~SWITCH:KAZ:T01~CRITICAL~~100
14~~ 17-01-08~10:02:32~2~ISRUAT3~isr/switch/bin/ifstart for ISRUAT3 TOO MANY PROCESSES 102802~emeflxci~FLEX_daemons~SWITCH:ISR:T01~CRITICAL~~100
1~~ 17-01-08~00:03:52~1~AUTOSYS~T01_ADM_155700_YYYYMMDD_C~T01~autosys~~JOBFAILURE~~
1~~ 17-01-08~00:03:10~1~AUTOSYS~T01_ADM_BO_yy-mm-dd_today_C~T01~autosys~~JOBFAILURE~~
1~~ 17-01-08~00:02:46~1~AUTOSYS~T01_ADM_BO_yy_mm_dd_today_C~T01~autosys~~JOBFAILURE~~
1~~ 17-01-08~00:03:11~1~AUTOSYS~T01_ADM_BO_yymmdd_today_C~T01~autosys~~JOBFAILURE~~
1~~ 17-01-08~00:03:08~1~AUTOSYS~T01_ADM_BO_yyyy-mm-dd_today_C~T01~autosys~~JOBFAILURE~~
1~~ 17-01-08~00:03:05~1~AUTOSYS~T01_ADM_BO_yyyy_mm_dd_today_C~T01~autosys~~JOBFAILURE~~
1~~ 17-01-08~00:03:04~1~AUTOSYS~T01_ADM_BO_yyyymmdd_today_C~T01~autosys~~JOBFAILURE~~
1~~ 17-01-08~00:05:39~1~AUTOSYS~T01_ADM_DateGvars_B~T01~autosys~~JOBFAILURE~~
1~~ 17-01-08~00:02:43~1~AUTOSYS~T01_ADM_GVAR_10235_DDMMYY_TODAY_C~T01~autosys~~JOBFAILURE~~
1~~ 17-01-08~00:04:17~1~AUTOSYS~T01_ADM_GVAR_149388_DATEDDMMMYYYY_C~T01~autosys~~JOBFAILURE~~
1~~ 17-01-08~00:04:23~1~AUTOSYS~T01_ADM_GVAR_149388_DATEMMMDD_C~T01~autosys~~JOBFAILURE~~
1~~ 17-01-08~00:03:11~1~AUTOSYS~T01_ADM_GVAR_35612_DATEyyyymmdd_LASTBUSDAY_C~T01~autosys~~JOBFAILURE~~
1~~ 17-01-08~00:04:57~1~AUTOSYS~T01_ADM_make_priv_C~T01~autosys~~JOBFAILURE~~

$ /usr/xpg4/bin/awk -f match_files2_o.awk FS="~" managed_event_log.170108 all_event_log.170108 
Count|VT        |Date     |Time     |From     |Alert Message                                                         |Instance |
14   |13432013  | 17-01-08|10:02:32 |KAZUAT1  |kaz/switch/bin/ifstart for KAZUAT1 TOO MANY PROCESSES 001459          |emeflxci |
12   |13432030  | 17-01-08|10:02:32 |KAZUAT1  |ifpoll_KZ for KAZUAT1 NOT EXIST 001459                                |emeflxci |
14   |          | 17-01-08|10:02:32 |ISRUAT3  |isr/switch/bin/ifstart for ISRUAT3 TOO MANY PROCESSES 102802          |emeflxci |
1    |          | 17-01-08|00:03:52 |AUTOSYS  |T01_ADM_155700_YYYYMMDD_C                                             |T01      |
1    |          | 17-01-08|00:03:10 |AUTOSYS  |T01_ADM_BO_yy-mm-dd_today_C                                           |T01      |
1    |          | 17-01-08|00:02:46 |AUTOSYS  |T01_ADM_BO_yy_mm_dd_today_C                                           |T01      |
1    |          | 17-01-08|00:03:11 |AUTOSYS  |T01_ADM_BO_yymmdd_today_C                                             |T01      |
1    |          | 17-01-08|00:03:08 |AUTOSYS  |T01_ADM_BO_yyyy-mm-dd_today_C                                         |T01      |
1    |          | 17-01-08|00:03:05 |AUTOSYS  |T01_ADM_BO_yyyy_mm_dd_today_C                                         |T01      |
1    |          | 17-01-08|00:03:04 |AUTOSYS  |T01_ADM_BO_yyyymmdd_today_C                                           |T01      |
1    |          | 17-01-08|00:05:39 |AUTOSYS  |T01_ADM_DateGvars_B                                                   |T01      |
1    |          | 17-01-08|00:02:43 |AUTOSYS  |T01_ADM_GVAR_10235_DDMMYY_TODAY_C                                     |T01      |
1    |          | 17-01-08|00:04:17 |AUTOSYS  |T01_ADM_GVAR_149388_DATEDDMMMYYYY_C                                   |T01      |
1    |          | 17-01-08|00:04:23 |AUTOSYS  |T01_ADM_GVAR_149388_DATEMMMDD_C                                       |T01      |
1    |          | 17-01-08|00:03:11 |AUTOSYS  |T01_ADM_GVAR_35612_DATEyyyymmdd_LASTBUSDAY_C                          |T01      |
1    |          | 17-01-08|00:04:57 |AUTOSYS  |T01_ADM_make_priv_C                                                   |T01      |
     |          |         |         |         |                                                                      |         |

I think that you _don't_ want to set $2 based on the record number.
I feel like that the join key is only $7, am I missing something?

Code:
$ cat match_files2_o.awk 
BEGIN { 
  printf "%-5s|%-10s|%-9s|%-9s|%-9s|%-70s|%-9s|%-s\n",
  "Count", "VT", "Date", "Time","From","Alert Message","Instance",""
}
NR == FNR {
  x[$1,$7]
  y[$7] = $2
  next
}
($1,$7) in x {
 next 
}
$7 in y {
    $2 = y[$7]
}
{ printf "%-5s|%-10s|%-9s|%-9s|%-9s|%-70s|%-9s|%-s\n", 
    $1, $2, $3, $4, $6, $7, $8, "" }
$ /usr/xpg4/bin/awk -f match_files2_o.awk FS="~" managed_event_log.170108 all_event_log.170108 
Count|VT        |Date     |Time     |From     |Alert Message                                                         |Instance |
14   |13432013  | 17-01-08|10:02:32 |KAZUAT1  |kaz/switch/bin/ifstart for KAZUAT1 TOO MANY PROCESSES 001459          |emeflxci |
12   |          | 17-01-08|10:02:32 |KAZUAT1  |ifpoll_KZ for KAZUAT1 NOT EXIST 001459                                |emeflxci |
14   |          | 17-01-08|10:02:32 |ISRUAT3  |isr/switch/bin/ifstart for ISRUAT3 TOO MANY PROCESSES 102802          |emeflxci |
1    |          | 17-01-08|00:03:52 |AUTOSYS  |T01_ADM_155700_YYYYMMDD_C                                             |T01      |
1    |          | 17-01-08|00:03:10 |AUTOSYS  |T01_ADM_BO_yy-mm-dd_today_C                                           |T01      |
1    |          | 17-01-08|00:02:46 |AUTOSYS  |T01_ADM_BO_yy_mm_dd_today_C                                           |T01      |
1    |          | 17-01-08|00:03:11 |AUTOSYS  |T01_ADM_BO_yymmdd_today_C                                             |T01      |
1    |          | 17-01-08|00:03:08 |AUTOSYS  |T01_ADM_BO_yyyy-mm-dd_today_C                                         |T01      |
1    |          | 17-01-08|00:03:05 |AUTOSYS  |T01_ADM_BO_yyyy_mm_dd_today_C                                         |T01      |
1    |          | 17-01-08|00:03:04 |AUTOSYS  |T01_ADM_BO_yyyymmdd_today_C                                           |T01      |
1    |          | 17-01-08|00:05:39 |AUTOSYS  |T01_ADM_DateGvars_B                                                   |T01      |
1    |          | 17-01-08|00:02:43 |AUTOSYS  |T01_ADM_GVAR_10235_DDMMYY_TODAY_C                                     |T01      |
1    |          | 17-01-08|00:04:17 |AUTOSYS  |T01_ADM_GVAR_149388_DATEDDMMMYYYY_C                                   |T01      |
1    |          | 17-01-08|00:04:23 |AUTOSYS  |T01_ADM_GVAR_149388_DATEMMMDD_C                                       |T01      |
1    |          | 17-01-08|00:03:11 |AUTOSYS  |T01_ADM_GVAR_35612_DATEyyyymmdd_LASTBUSDAY_C                          |T01      |
1    |          | 17-01-08|00:04:57 |AUTOSYS  |T01_ADM_make_priv_C                                                   |T01      |
     |          |         |         |         |                                                                      |         |

This could be rewritten as:

Code:
BEGIN { 
  printf "%-5s|%-10s|%-9s|%-9s|%-9s|%-70s|%-9s|%-s\n",
  "Count", "VT", "Date", "Time","From","Alert Message","Instance",""
}
NR == FNR {
  x[$1,$7]
  y[$7] = $2
  next
}
!(($1,$7) in x) {
  if ($7 in y)
    $2 = y[$7]
  printf "%-5s|%-10s|%-9s|%-9s|%-9s|%-70s|%-9s|%-s\n", 
    $1, $2, $3, $4, $6, $7, $8, "" }


Last edited by radoulov; 01-17-2008 at 12:57 PM..
# 17  
Old 01-18-2008
Thanks again Radoulov,
Last awk program works perfecly fine, this is what i need,
can you tell me how to substitute $2 and $3 from file2 to file1, so then VT and STAT will be display from file2 rest of the code will have same logic.

$ cat managed_event_log.170108
12~13432013~OPEN~ 17-01-08~10:02:32~2~KAZUAT1~kaz/switch/bin/ifstart for KAZUAT1 TOO MANY PROCESSES 001459~emeflxci~FLEX_daemons~SWITCH:KAZ:T01~CRITICAL~~100
12~13432030~CLOSE~ 17-01-08~10:02:32~1~KAZUAT1~ifreader_KZ for KAZUAT1 NOT EXIST 001459~emeflxci~FLEX_daemons~SWITCH:KAZ:T01~CRITICAL~~100

$ cat all_event_log.170108
14~~~ 17-01-08~10:02:32~2~KAZUAT1~kaz/switch/bin/ifstart for KAZUAT1 TOO MANY PROCESSES 001459~emeflxci~FLEX_daemons~SWITCH:KAZ:T01~CRITICAL~~100
12~~~ 17-01-08~10:02:32~1~KAZUAT1~ifpoll_KZ for KAZUAT1 NOT EXIST 001459~emeflxci~FLEX_daemons~SWITCH:KAZ:T01~CRITICAL~~100

OUTPUT:
Count|VT |Stat| Date |Time |From |Alert Message |Instance |
14 |13432013 |OPEN| 17-01-08|10:02:32 |KAZUAT1 |kaz/switch/bin/ifstart for KAZUAT1 TOO MANY PROCESSES 001459 |emeflxci |
12 | |NONE| 17-01-08|10:02:32 |KAZUAT1 |ifpoll_KZ for KAZUAT1 NOT EXIST 001459 |emeflxci |

Also please explain how this works:
if ($7 in y) # if found $7 in array y then replace $2 by y[$7] ? but why before y[$7] = $2 ? i'm confuse Smilie
$2 = y[$7]
# 18  
Old 01-18-2008
But we have another problem here: the primary key ($1,$7)
is not unique in managed_event_log.170108:
12,KAZUAT1 for both records. We have to add another element
(field?) to the key, it has to be unique. Which part of $8 can be considered ("TOO MANY PROCESSES", "NOT EXIST" or something
else)?
# 19  
Old 01-18-2008
Yes i know that now our primary key will be $8
so now awk will looks like that:
BEGIN {
printf "%-3s|%-10s|%-4s|%-9s|%-9s|%-9s|%-70s|%-9s|%-s\n", "Cnt", "VT", "STAT", "Date", "Time","From","Alert Message","Instance",""
}
NR == FNR {
x[$1,$8]
y[$8] = $2
z[$8] = $3
next
}
!(($1,$8) in x) {
if ($8 in y)
$2 = y[$8]
$3 = z[$8]
printf "%-3s|%-10s|%-4s|%-9s|%-9s|%-9s|%-70s|%-9s|%-s\n", $1, $2, $3, $4, $5, $7, $8, $9, "" }

It's working!!!! Smilie
Thanks
# 20  
Old 03-19-2009
Hi,

I have stumbled upon many awk scripts which does merging of information from 2 files. I tried to use some of them for my task, but nothing really helped me in accomplishing my desired output Smilie
Here is what I am try to do ....
I have 2 files - main.txt and alternate.txt. Contents of main.txt is as given below -
A123|msg a 1|date a 1|other info a 1
B234|msg b 1|date b 1|other info b 1
C123|msg c 1|date c 1|other info c 1
D456|msg d 1|date d 1|other info d 1
.....
and so on

Contents of alternate.txt is as given below -
B234|1.1|msg b 2|remarks 1
B234|1.2|msg b 3|remarks 2
D456|1.1|msg d 2|remarks 1
D456|1.2|msg d 3|remarks 2
D456|1.3|msg d 4|remarks 3
D456|1.4|msg d 5|remarks 4
..... and so on.

Common key between the above files is column 1 (A123, B234, etc.). I want to consolidate the information available in the 2 files such that the result is as given below -

A123|msg a 1|date a 1|other info a 1
B234|msg b 1|date b 1|other info b 1
B234|1.1|msg b 2|remarks 1
B234|1.2|msg b 3|remarks 2
C123|msg c 1|date c 1|other info c 1
D456|msg d 1|date d 1|other info d 1
D456|1.1|msg d 2|remarks 1
D456|1.2|msg d 3|remarks 2
D456|1.3|msg d 4|remarks 3
D456|1.4|msg d 5|remarks 4

Basically, for every record which is present in main file, if there are any entries in alternate file, print those records subsequently in the result file. Any help with this is highly appreciated.

Thanks and Regards,
Vidya
# 21  
Old 03-19-2009
You may try something like this (use nawk or /usr/xpg4/bin/awk on Solaris):

Code:
awk -F\| 'NR == FNR {_[$1] = _[$1] ? _[$1] RS $0 : $0; next }
($1 in _ && $0 = $0 RS _[$1]) || 1' alternate.txt main.txt


Previous Thread | Next Thread
Test Your Knowledge in Computers #194
Difficulty: Easy
Perl was ranked in the top 10 according to the TIOBE Index for October 2019.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk Merging multiple files with symbol representing new file

I just tried following ls *.dat|sort -t"_" -k2n,2|while read f1 && read f2; do awk '{print}' $f1 awk FNR==1'{print $1,$2,$3,$4,$5,"*","*","*" }' OFS="\t" $f2 awk '{print}' $f2 donegot following result 18-Dec-1983 11:45:00 AM 18.692 84.672 0 25.4 24 18-Dec-1983 ... (3 Replies)
Discussion started by: Akshay Hegde
3 Replies

2. Shell Programming and Scripting

Merging Files in UNIX shell script

I have the urge to merge some files using unix shell script but I'm very new using this language and I haven't succeeded yet. The requirement is to merge the header, body and footer into one file with the name "ANY-NAME" in below example. To identify which files should be merged, I have flagged... (9 Replies)
Discussion started by: black_soul
9 Replies

3. Shell Programming and Scripting

Merging rows in awk

Hello, I have a data format as follows: Ind1 0 1 2 Ind1 0 2 1 Ind2 1 1 0 Ind2 2 2 0 I want to use AWK to have this output: Ind1 00 12 21 Ind2 12 12 00 That is to merge each two rows with the same row names. Thank you very much in advance for your help. (8 Replies)
Discussion started by: Homa
8 Replies

4. Shell Programming and Scripting

Merging two files in awk

Hi, How I can merge two file columns such as the followings using awk: file 1 2 3 2 2 1 1 file 2 4 3 4 5 7 6 Result: 2 3 4 3 2 2 4 5 1 1 7 6 This is an example, at the end, I will have about 25 files that I want to merge them, it is important for me that the order in the... (7 Replies)
Discussion started by: Homa
7 Replies

5. Shell Programming and Scripting

merging files using awk

Hi, I have 2 files. File 1 chr1 1234 2468 ABC chr1 3456 4567 DEF chr2 5643 6154 XYZ : : : : so on.... File 2 chr1 1500 2500 positive chr1 2500 3500 negative chr1 3000 4500 neutral (10 Replies)
Discussion started by: Diya123
10 Replies

6. Shell Programming and Scripting

AWK Script For Merging Text Files

Hello, I am trying to merge data from two text files. One file (File1) contains a listing of data which includes the trial number in Column 5, while the other text file (File2) contains what category the trial belongs to. Here is a snippet of what File1 looks like. 1 Arrow_ST 9.738 0.905... (2 Replies)
Discussion started by: Jahn
2 Replies

7. Shell Programming and Scripting

awk command : row by row merging of two files

I want to write a scrpit to merge files row wise (actually concatinating) main.txt X Y Z file 1 A B C file 2 1 2 3 now i want the script to check if the file1 is empty or not, if empty then make it like A B C 1 2 3 again to check if second file is empty if not do as done... (0 Replies)
Discussion started by: shashi792
0 Replies

8. Shell Programming and Scripting

awk merging files based on 2 complex conditions

1. if the 1st row IDs of input1 (ID1/ID2.....) is equal to any IDNames of input2 print all relevant values together as defined in the output. 2. A bit tricky part is IDno in the output. All we need to do is numbering same kind of letters as 1 (aa of ID1) and different letters as 2 (ab... (4 Replies)
Discussion started by: ruby_sgp
4 Replies

9. Shell Programming and Scripting

merging of 2 files AWK - part 2

i have try , but i think i will never learn awk :( now i have 2 files : a 1:aaa:2:aaa1 2:bbb:2:bbb1 3:ccc:3:ccc1 b aaa:2 bbb:0 ccc:3 output: for all lines where a.$2 == b.$1 i want to compare a.$3 != b.$2 if true then set err=1 if false set err=0 and print all lines from file a +... (2 Replies)
Discussion started by: pp56825
2 Replies

10. Shell Programming and Scripting

Merging files with AWK filtering and counting lines

Hi there, I have a couple of files I need to merge. I can do a simple merge by concatenating them into one larger file. But then I need to filter the file to get a desired result. The output looks like this: TRNH 0000000010941 ORDH OADR OADR ORDL ENDT 1116399 000000003... (2 Replies)
Discussion started by: Meert
2 Replies

Featured Tech Videos