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

formatting text with spaces using printf

Status
Not open for further replies.

dendenners

Programmer
Jul 17, 2001
110
IE
Hi there,
I'm trying to use printf in awk to format some text a certain way. However I'm encountering problems when passing in the text to awk as a shell variable when the variable contains spaces. Say I want to use the text "hello there" as my formatable text. The following works fine:
echo "" | awk '{printf("%-16.16s", "hello there")}'
However, say I set header="hello there" in the shell and try
echo "" | awk '{printf("%-16.16s", '$header')}'
I get a parse error because of the space in the variable. How can I print this properly? Thanks
Denis
 
Try echo "" | awk '{printf("%-16.16s", header)}' header="$header"

Greg.
 
Hi there.
I have another question about this. I want to zero pad a number of the form '12345+' where the plus signifies the sign of the number. The fact that the sign is at the end of the number means that printf does not recognise it as a valid number, and therefore will not print the + when I sihnifiy the variable as being %d. When I signify it as being %s, I can't seem to left-zero-pad the number! Is there a way to zero pad a variable in printf instead of space padding it? Thanks
 
If you wanted to pad it to 10 chars, you would do printf("%010s","12345+")

Greg.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top