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

awk help


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

Honored Contributor
Posts: 6,592
Registered: ‎05-19-2003
Message 2 of 4 (124 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.


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.


For example, using your output as input:

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

Re: awk help

yep, use printf
Support Fatherhood - Stop Family Law
Super Advisor
Posts: 287
Registered: ‎08-13-2007
Message 4 of 4 (124 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.