1748166 Members
3775 Online
108758 Solutions
New Discussion юеВ

Re: String formatting

 
rkraj
New Member

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
7 REPLIES 7
Steven Schweda
Honored Contributor

Re: String formatting

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

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

man printf

td192> printf ">%10s< >%-10s<\n" 'aaa' 'bbb'
> aaa< >bbb <
Peter Nikitka
Honored Contributor

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"
Dennis Handly
Acclaimed Contributor

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
} '
James R. Ferguson
Acclaimed Contributor

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...
rkraj
New Member

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
Peter Nikitka
Honored Contributor

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"
James R. Ferguson
Acclaimed Contributor

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