Unix/Linux Go Back    



awk statement help

Shell Programming and Scripting




Kindly Note - This is a Single User Post by Forum Member Yoda Regarding:
awk statement help.
Please Follow The Primary Link Above to View the Full Discussion.

   
Old Unix and Linux 1 Week Ago   -   Original Discussion by brettcasper
Yoda's Unix or Linux Image
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 23 November 2017, 12:39 AM EST
Location: Galactic Empire
Posts: 3,603
Thanks: 253
Thanked 1,316 Times in 1,233 Posts
Here is an awk approach:-

Code:
awk -F'~' '
        NR == FNR {
                if ( FNR > 1 )
                        A_F1[$2] = $3 FS $4
                next
        }
        FNR > 1 {
                A_F2[$1] = ( A_F2[$1] ? A_F2[$1] FS $2 : $2 )
        }
        END {
                for ( k in A_F1 )
                {
                        n = split ( A_F1[k], T1 )
                        print "Begin Record"
                        print "Location: " k

                        if ( k in A_F2 )
                        {
                                m = split ( A_F2[k], T2 )
                                for ( i = 1; i <= m; i++ )
                                        print "Site Id: " T2[i]
                        }
                        else
                                print "Site Id: NULL"

                        print "Date: " T1[1]
                        print "Contact: " T1[2]
                        printf "End Record\n\n"
                }
        }
' file1.txt file2.txt

The Following User Says Thank You to Yoda For This Useful Post:
brettcasper (1 Week Ago)