Show Password

Hello Unregistered
Welcome from United States
You are somewhere around Ashburn
Last Visit: 1 Minute Ago @ 03:21 PM
Bits: 0
UNIX Time: 1531682511


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 11-13-2017   -   Original Discussion by brettcasper
Don Cragun's Unix or Linux Image
Don Cragun Don Cragun is offline Forum Staff  
Join Date: Jul 2012
Last Activity: 15 July 2018, 2:45 PM EDT
Location: San Jose, CA, USA
Posts: 11,405
Thanks: 647
Thanked 3,967 Times in 3,391 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.

awk -F'~' '
FNR == 1 {
NR == FNR {
	site[$1] = site[$1] "Site Id: " $2 "\n"
{	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 (11-15-2017)