Unix/Linux Go Back    



awk statement help

Shell Programming and Scripting




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

   
Old Unix and Linux 4 Days Ago   -   Original Discussion by brettcasper
Don Cragun's Unix or Linux Image
Don Cragun Don Cragun is online now Forum Staff  
Administrator
 
Join Date: Jul 2012
Last Activity: 18 November 2017, 6:24 PM EST
Location: San Jose, CA, USA
Posts: 10,651
Thanks: 571
Thanked 3,729 Times in 3,183 Posts
Hi brettcasper,
Welcome to the UNIX & Linux Forums. When starting a thread here it always helps if you tell us what operating system and shell you're using so we know what capabilities your system has.

In addition to what Yoda suggested, you might also try the following. By reversing the order in which the files are processed, it can process records from File1.txt one record at a time instead of having to store the entire contents of both files in memory.


Code:
awk -F'~' '
FNR == 1 {
	next
}
NR == FNR {
	site[$1] = site[$1] "Site Id: " $2 "\n"
	next
}
{	printf("Begin Record\nLocation: %s\n%sDate: %s\nContact: %s\nEnd Record\n\n",
	    $2, ($2 in site) ? site[$2] : "Site Id: " $1 "\n", $3, $4)
}' File2.txt File1.txt

If you're running this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk or nawk.
The Following User Says Thank You to Don Cragun For This Useful Post:
brettcasper (3 Days Ago)