Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Awk Retaining Old Values 1

Status
Not open for further replies.

johngiggs

Technical User
Oct 30, 2002
492
US
I have created an awk script to parse through a contacts list that looks like the example below.

Full Name: Joe Schmoe
Last Name: Schmoe
First Name: Joe
Job Title: Vice President
Company: JS Inc.
Business: (999) 999-9999
E-mail: jschmoe@js.com

Full Name: Henry Schmoe
Last Name: Schmoe
First Name: Henry
Mobile: (999) 999-9999
E-mail: jschmoe@js.com

When I parse this input using my awk script, if the second record does not have a value for something that is in the first record, the value for the first record is used. Basically, I just need to know how to "unset" the variable.

Below is my awk script.
Code:
BEGIN{printf "%-18s %-12s %-12s %-12s %-12s\n", "Name", "Work", "Cell", "Home",
"E-Mail\n==============================================================================="}
/Full Name:/{name = $3" "$4}
/Business:/{if ($3=="Ext") business = $2;if ($2 ~ /[0-9]{3}/) business = $2" "$3
; else business = ""}
/Home:/{if ($2 ~ /[0-9]{3}/) home = $2" "$3; else home = ""}
/Mobile:/{if ($2 ~ /[0-9]{3}/) cell = $2" "$3; else cell = ""}
/E-mail:/{email= $2; printf "%-18s %-12s %-12s %-12s %-12s\n", name, business, h
ome, cell, email}

Any help would be greatly appreciated.

Thanks,

John
 
Just add this after your printf statement, because once you've printed them you can discard them.

Code:
name=business=home=cell=email="";

Annihilannic.
 
Annihilannic,

Thank you for your help. I had tried doing the same thing in another statement like this:

Code:
{name=business=home=cell=email="";}

I was on the right track, but couldn't figure it out. Have a star!!

John
 
As you probably figured out that solution would have reset the values for every single line of input. Thanks for the star. :)

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top