Formatting Output

Hi

I tried running the below
awk 'BEGIN { printf ("%s %-51s %s %-7s %s",$var1,$var2,$var3,$var4,$var5)}'

from the command prompt and it is not working.

Getting the error
awk: Field $() is not correct.
The source line number is 1.

Actually my requirement is to form a string based on a particular format.
The format i have mentiioned above.
But in UNIX script i am not able to do the formatting.

Let me know if you have any clues.

Regards
Dhanamurthy

Try var1, var2, var3, ... instead. $var1 means "the field whose position is stored in var1[/b], but since you have not even read any records yet it is meaningless.

Unlike perl and shells, awk does not require its variables to be prefixed by $.

Annihilannic.

Hi
I have the values stored in all the variables

var1="ONE"
var2="TWO"
-----
-----
----

i tried this

awk 'BEGIN { printf ("%s %s",var1,var2) }'

Even then i don;t see any output coming.

My aim is to form a string in a format required

If there is any other way also please let me know

Regards

You have to pass the variables to awk

awk -v var1=$var1 -v var2=$var2 'BEGIN { printf ("%s %s\n",var1,var2) }'

Just use printf on its own, not through awk, e.g...

var1="ONE"
var2="TWO"
-----
-----
----
printf '%s %-51s %s %-7s %s\n' $var1 $var2 $var3 $var4 $var5

Hi
I checked with printf
and it is formatting but did;t not completely..

var1="CTCT 0001000Y"

I have the blank between CTCT and 0001000Y

It is giving the output like the below

CTCT 0001000Y $
The space is extended between CTCT and 0001000Y.
Is there any option that i need to choose for this

Regards
Dhana

Hi
My previous reply does not give the actual output
Please find the space between CTCT and 0001000 which is not correct.
CTCT 0001000Y

Please help

Regards
Dhana