Gripsiden Gripsiden - 7 months ago 12
Bash Question

Add Column to text file using Heading value ( mutliple Heading in the file )

Have been searching through various solns this morning. Finding nothing close enough for me to chew on. ( Im a Sys Admin but not a profound developer of scripts but am familiar with awk/sed/ etc )

My preference is Bash or even perl

I have data like this : ( data is dummy )


====================================================================================
Person Name: Joe Soap
Code: 0
====================================================================================
Purchases:12th June 2013 - Awk for Dummies Vol1
Purchases:42th June 2013 - Awk for Dummies Vol2
Purchases:22th June 2013 - Awk for Dummies Vol3
====================================================================================

====================================================================================
Person Name: Mary Soap
Code: 0
====================================================================================
Purchases:52th June 2013 - Sed for Microsoft Lovers Vol1
Purchases:82th June 2013 - Sed for Microsoft Lovers Vol2
====================================================================================

====================================================================================
Person Name: Don Soap
Code: 0
====================================================================================
Purchases:3rd March 2013 - Self Healing


I need to get :


Joe Soap:Purchases:12th June 2013 - Awk for Dummies Vol1
Joe Soap:Purchases:42th June 2013 - Awk for Dummies Vol2
Joe Soap:Purchases:22th June 2013 - Awk for Dummies Vol3
Mary Soap:Purchases:52th June 2013 - Sed for Microsoft Lovers Vol1
Mary Soap:Purchases:82th June 2013 - Sed for Microsoft Lovers Vol2
Don Soap:Purchases:3rd March 2013 - Self Healing


I would imagine its straight forward.. but would appreciate pointers :)P

Thanks all!

Answer

Using awk you can do:

awk -F ': *' -v OFS=: '$1~/Person Name/{p=$2} $1~/Purchases/{print p, $0}' file

Joe Soap:Purchases:12th June 2013 - Awk for Dummies Vol1
Joe Soap:Purchases:42th June 2013 - Awk for Dummies Vol2
Joe Soap:Purchases:22th June 2013 - Awk for Dummies Vol3
Mary Soap:Purchases:52th June 2013 - Sed for Microsoft Lovers Vol1
Mary Soap:Purchases:82th June 2013 - Sed for Microsoft Lovers Vol2
Don Soap:Purchases:3rd March 2013 - Self Healing