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!

How can I right justify using awk?

Status
Not open for further replies.

pjb

Programmer
May 1, 2001
148
US
Continuing wth my problem... I am reading a flat file with awk and picking out the first ten characters of each record using substr($0,1,10) and outputting with a printf. That works fine. In these first ten bytes are character strings that are left justified (starting in col 1) and padded with spaces. There are no imbedded blanks. How can I right justify these character strings and pad with leading spaces on the output?
 
awk '{printf "%10.10s",$0}' /path/to/input > output

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Thanks, it works like the substr, but the output is still left justified. For example, using .'s to represent spaces, my input looks like:
abcdef....
abcde.....

I need the output to be:
....abcdef
.....abcde
 
Those dots are unclear to me.

. Mac for productivity
.. Linux for developement
... Windows for solitaire
 
The dots represent spaces. Either leading or trailing.
 
And this ?
awk '{x=substr($0,1,10);gsub(" ","",x);printf "%10.10s",x}' /path/to/input > output

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
That's it. Thanks, took me a while. Had to use nawk to get the gsub to work. Also the \n to end the line.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top