conditional merging two files using awk

December 23, 2010 at 04:04:09
Specs: Linux i686
I have 2 files file1 and file2
file1:
a
a
c
c
c
b
d
d
d
d

file2:
a1
a2
c1
c2
c3
b1
d1
d2
d3
d4

I want output as....(uning awk or sed)
output:
a a1,a2
c c1,c2,c3
b b1
d d1,d2,d3,d4


See More: conditional merging two files using awk

Report •

#1
December 24, 2010 at 13:02:07

#!/bin/bash

# get rid of the dups in file1
uniq file1 > newfile1

awk '
BEGIN {
# read file2 into an array grabbing the first character of the string
while ( getline < "file2" > 0)
   {
   xx=substr($0,1,1)
   s[$0]=xx
   }
}
{
printf("%s ", $0)
# cycle thru the array and
# determine how many elements in the array equal the line read
cnt=0
for(i in s)
   if(s[i] == $0)
      cnt++

tot=0
# cycle thru the array and find each equal element.  while the count
# is less than the total print the index of the array with a comma.  When
# the count is equal print the index with a carriage return.
for(i in s)
   if(s[i] == $0)
     {
     # count is less than total, use comma
     tot++
     if(tot < cnt)
        printf("%s,", i)
     else # count is equal, use carriage return
        printf("%s\n", i)
     }
} ' newfile1


Report •
Related Solutions


Ask Question