awk repeat one field at all lines and modify field repetitions


Login or Register to Reply

 
Thread Tools Search this Thread
# 1  
Old 09-18-2015
awk repeat one field at all lines and modify field repetitions

Hello experts

I have a file with paragraphs begining with a keeping date and ending with "END":

Code:
20120301 num num
John        num num A keepnum1 num num
kathrin     num num A keepnum1 num num
kathrin     num num B keepnum2 num num
Pete        num num A keepnum1 num num
Jacob       num num A keepnum1 num num
Jacob       num num B keepnum2 num num
END
20111203 num num
Eva        num num A keepnum1 num num
Adam       num num A keepnum1 num num
Adam       num num B keepnum2 num num       
Jacob      num num A keepnum1 num num
Jacob      num num B keepnum2 num num
END
...

where "num"=random number that will be discarded, keepnum=keeping random number
What I want is
1. to repeat the date in all lines of each paragraph before keepnum1
2. to take the B keepnum2 of each duplicate name and place it in the first accuring line.
So that the output would look like:
Code:
John        A 20120301 keepnum1 
kathrin     A 20120301 keepnum1  B keepnum2 
Pete        A 20120301 keepnum1 
Jacob        A 20120301 keepnum1  B keepnum2 
END
20111203 
Eva        A 20111203 keepnum1 
Adam      A 20111203  keepnum1  B keepnum2       
Jacob     A 20111203 keepnum1 B keepnum2 
END
....

I have only managged to take the dates with the following command
Code:
 awk ' {RS="END"}; {print $1};'

But with no further luck. Any suggestions? (OS Opensuse)
# 2  
Old 09-18-2015
The following seems to do what you want, but since your output format is not consistent (no date before first output group, inconsistent spacing), I'm not sure if this is what you want:
Code:
awk '
!head {	print date = $1
	head = 1
	last = ""
	next
}
/^END/ {head = 0
	if(last != "")
		print ""
	print "END"
	next
}
$1 != last {
	if(last != "")
		print ""
	printf("%s\tA %s %s", last = $1, date, $5)
	next
}
{	# To get here, $1 on this line and $1 on the previous line must match...
	printf("\tB %s", $5)
}' file

which, if file contains your sample input, produces the output:
Code:
20120301
John	A 20120301 keepnum1
kathrin	A 20120301 keepnum1	B keepnum2
Pete	A 20120301 keepnum1
Jacob	A 20120301 keepnum1	B keepnum2
END
20111203
Eva	A 20111203 keepnum1
Adam	A 20111203 keepnum1	B keepnum2
Jacob	A 20111203 keepnum1	B keepnum2
END

If someone wants to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk or nawk.
This User Gave Thanks to Don Cragun For This Post:
phaethon (09-19-2015)
Login or Register to Reply

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
awk to update field using matching value in file1 and substring in field in file2 cmccabe Shell Programming and Scripting 2 06-18-2017 07:38 AM
awk to adjust coordinates in field based on sequential numbers in another field cmccabe Shell Programming and Scripting 3 01-30-2017 07:39 AM
awk to update value in field based on another field cmccabe Shell Programming and Scripting 2 12-03-2016 10:52 AM
How can awk ignore the field delimiter like comma inside a field? gopal.biswal Shell Programming and Scripting 6 11-29-2016 05:49 AM
awk to parse field and include the text of 1 pipe in field 4 cmccabe Shell Programming and Scripting 7 11-07-2015 07:05 PM
Using awk, print all the lines where field 8 is equal to x charles33 Shell Programming and Scripting 2 11-13-2011 02:27 PM
AWK: Pattern match between 2 files, then compare a field in file1 as > or < field in file2 right_coaster Shell Programming and Scripting 4 10-06-2011 06:07 PM
Awk Search text string in field, not all in field. rocket_dog Shell Programming and Scripting 3 09-12-2011 09:09 AM
counting lines containing two column field values with awk origamisven Shell Programming and Scripting 6 06-22-2011 11:22 AM
AWK : Add Fields of lines with matching field DerSeb Shell Programming and Scripting 6 01-16-2011 08:41 PM
awk, comma as field separator and text inside double quotes as a field. kevintse Shell Programming and Scripting 8 11-15-2010 05:31 PM
awk field equal something, then add something to the field jimmy_y Shell Programming and Scripting 9 06-18-2010 01:59 AM
awk - Summing a field based on another field treesloth UNIX for Dummies Questions & Answers 7 06-21-2009 06:04 PM
Awk counting lines with field match fredted40x UNIX for Dummies Questions & Answers 8 05-11-2009 01:29 PM
Moving Part of a field to another field using AWK rjsha1 Shell Programming and Scripting 5 08-04-2006 05:39 AM