awk help (115 Views)
Reply
Super Advisor
Allanm
Posts: 287
Registered: ‎08-13-2007
Message 1 of 4 (115 Views)

awk help

Hi ALL,

I am doing an awk on a file and getting the output as below, is there a way I can have spacing between $2 and $4 so that $4 is lined up symmetrically unlike what I am getting below?


awk -F: '{print $2"\t\t\t"$4}' filename

xxxxxxxxxxxxx 8581
xxx 8585
xxxxxx 8590
xxxx 8595
xxxxxxx 9400
xx 9510
xxxxxxxxxxxxxxx 9520


Thanks,
Allan.
Honored Contributor
Hein van den Heuvel
Posts: 6,588
Registered: ‎05-19-2003
Message 2 of 4 (115 Views)

Re: awk help

>> symmetrically

Not sure anyone understand clearly what that means.
Terms folks tend to understand are columns and field and left of right aligned.

Anyway...

Use a PRINTF instead of PRINT
Use '%-?ns' for a left-aligned ?-columns wide string field.

For the second column ($4) you may want a %0?d formatting if you want to play with leading zeroes.

Read any C, AWK, PERL or SHell book or man-page for all the details.

Hein


For example, using your output as input:

$ awk '{printf ("%-20s%6s\n",$1,$2)}' x.x
Honored Contributor
Michael Steele_2
Posts: 4,934
Registered: ‎08-14-2002
Message 3 of 4 (115 Views)

Re: awk help

yep, use printf
Support Fatherhood - Stop Family Law
Super Advisor
Allanm
Posts: 287
Registered: ‎08-13-2007
Message 4 of 4 (115 Views)

Re: awk help

The original awk statement worked for me once I redirected the output to xls file.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.