String formatting (397 Views)
Reply
Occasional Visitor
rkraj
Posts: 2
Registered: ‎05-01-2008
Message 1 of 8 (397 Views)

String formatting

LIke sprintf in 'C' is there any option available in UNIX shell scripting

For eg)
awk 'BEGIN { printf ("%s %-51s %s %-7s %s",$var1,$var2,$var3,$var4,$var5)}'

I tried to the above and got the error message.

Is there any other way to format strings so that my purpose of writing the whole string into the file will be successful.
Please help

Regards
rkraj
Honored Contributor
Steven Schweda
Posts: 9,091
Registered: ‎02-23-2005
Message 2 of 8 (397 Views)

Re: String formatting

> [...] UNIX [...]

"UNIX" is not a very well-defined quantity.

man printf

td192> printf ">%10s< >%-10s<\n" 'aaa' 'bbb'
> aaa< >bbb <
Honored Contributor
Peter Nikitka
Posts: 1,575
Registered: ‎02-10-2003
Message 3 of 8 (397 Views)

Re: String formatting

Hi,

there is a 'printf' binary, which you can use in shell scripts, like:
printf "%02d %32s\n" $i $text

In awk, your syntax needs modification: drop the "$" before variable names.

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Acclaimed Contributor
Dennis Handly
Posts: 25,198
Registered: ‎03-06-2006
Message 4 of 8 (397 Views)

Re: String formatting

As Steven said, there is printf(1).
If you still want to go the awk route, you need to know how to pass variables. (It would be helpful if you printed the error message.)

Your issue is that you have $var1 within single quotes, where variables aren't expanded and in awk $var1 basically prints field ($) var1, where var1 is likely 0.

To pass variables, you either need to do stuttering quoting or:
awk -v var1="$var1" -v var2="$var2" ... '
BEGIN {
printf "%s ....\n", var1, var2, var3, var4, var5
exit
} '
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 5 of 8 (397 Views)

Re: String formatting

Hi:

And for completeness in the discussion, 'printf' is also intrinsic to Perl. So you can write in C, shell, awk or Perl as best suits your needs and those of your intended maintainers.

Regards!

...JRF...
Occasional Visitor
rkraj
Posts: 2
Registered: ‎05-01-2008
Message 6 of 8 (397 Views)

Re: String formatting

I am supposed to build it only on shell scripting but not on Perl.

Also i tried this way
var1="CTCT 0001000Y"

printf '%-50s' $var1
CTCT 0001000Y $

A single space is there between CTCT and 0001000Y and that is getting expanded while i am using the formatted one.
if var1="CTCT" then it works fine as expected.

Any clues
let me know

Regards
rkraj
Honored Contributor
Peter Nikitka
Posts: 1,575
Registered: ‎02-10-2003
Message 7 of 8 (397 Views)

Re: String formatting

Hi,

I think, it is all done correctly: You are using an unquoted variable, so contained whitespace leads to mutliple arguments of your command.
Look at the difference of your
var1="CTCT 0001000Y"
printf '%-50s\n' $var1

to a double quoted version
printf '%-50s\n' "$var1"

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 8 of 8 (397 Views)

Re: String formatting

Hi (again):

> Also i tried this way
> var1="CTCT 0001000Y"

> printf '%-50s' $var1
> CTCT 0001000Y $

> A single space is there between CTCT and 0001000Y and that is getting expanded while i am using the formatted one.
if var1="CTCT" then it works fine as expected.

> Any clues

I'm not sure what it is you are asking.

First, you probably want to include a newline when you print. Second, if your variable has embedded spaces, but you want to treat that as *one* argument to 'printf', then you need to quote the variable argument:

# printf '%-50s\n' "$var1"
CTCT 0001000Y

Otherwise, something like this:

# printf '%-50s\n' $var1 and yet more here

...becomes:

CTCT
0001000Y
and
yet
more
here

...instead of:

# printf '%-50s\n' "$var1 and yet more here"
CTCT 0001000Y and yet more here

Regards!

...JRF...
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.