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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I set a variable that doesn't print the first char of field? 1

Status
Not open for further replies.

johngiggs

Technical User
Oct 30, 2002
492
US
I am trying to parse a file and have a few fields that need to have character replacement done before the field is printed.

So the input file looks like this:

D10/04/2011
T$5.29
C
PA STORE
MSOMEPLACE, STATE
^

I need the ^D, ^T, and ^P to find what I want, but I then I want to set the variables so the first character (D, T, and P) are not printed.

Ex. Here's what I wrote for my awk script so far.

/^D/{date = $1}
/^T/{amount = $1}
/^P/{bill = $0}
/\^/{printf ("%-10s %-12s %-30s\n", date, amount, bill)}

I think I need to do something with sub or match, but I can't seem to get it to work.

The desired output is:


10/04/2011 $5.29 THE HOME DEPOT

Any help would be greatly appreciated.

Thanks,

John
 
Hi

John said:
I think I need to do something with sub or match, but I can't seem to get it to work.
Not necessarily. You could solve it with simple string manipulation using [tt]substr()[/tt] :
Code:
[fuchsia]/^D/[/fuchsia][teal]{[/teal]date [teal]=[/teal] [b]substr[/b][teal]([/teal][navy]$1[/navy][teal],[/teal] [purple]2[/purple][teal])[/teal][teal]}[/teal]
[fuchsia]/^T/[/fuchsia][teal]{[/teal]amount [teal]=[/teal] [b]substr[/b][teal]([/teal][navy]$1[/navy][teal],[/teal] [purple]2[/purple][teal])[/teal][teal]}[/teal]
[fuchsia]/^P/[/fuchsia][teal]{[/teal]bill [teal]=[/teal] [b]substr[/b][teal]([/teal][navy]$0[/navy][teal],[/teal] [purple]2[/purple][teal])[/teal][teal]}[/teal]
[fuchsia]/\^/[/fuchsia][teal]{[/teal][b]printf[/b] [teal]([/teal][green][i]"%-10s %-12s %-30s[/i][/green][lime][i]\n[/i][/lime][green][i]"[/i][/green][teal],[/teal] date[teal],[/teal] amount[teal],[/teal] bill[teal])[/teal][teal]}[/teal]

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top