Variable inside AWK printf

Hi,

Is it possible to print a variable inside awk printf ??

the above is not working

What is the problem ?
The statement seems valid.

Jean-Pierre.

Am sorry when i give more than one variable am getting an error

 
cat $FILENAME |grep ^1 | while read line
do
echo "$line" | nawk -v FD="$F_D" SS="$S_S" DS="$D_S" 'BEGIN {FS=","} {printf("%-1s%-4s%-30s","0",$2,FD,SS,DS)}' >> HEAD
done

ERROR

  
+ read line
+ cat testf
+ grep ^1
+ nawk -v FD=ALPHARMA_JDE                   SS=JDE_BRIDGE      DS=COMPUTRON    BEGIN {FS=","} {printf("%-1s%-4s%-30s","0",$2,FD,SS,DS)}
+ echo 1,2011,07,99999
+ 1>> HEAD
nawk: can't open file BEGIN {FS=","} {printf("%-1s%-4s%-30s","0",$2,FD,SS,DS)}
 source line number 1
+ read line

1) Add -v options

nawk -v FD="$F_D" -v SS="$S_S" -v DS="$D_S" 'BEGIN {FS=","} {printf("%-1s%-4s%-30s","0",$2,FD,SS,DS)}'

2) Remove loop (cat,grep while read) :

nawk -v FD="$F_D" -v SS="$S_S" -v DS="$D_S" 'BEGIN {FS=","} /^1/ {printf("%-1s%-4s%-30s","0",$2,FD,SS,DS)}' $FILENAME 

Jean-Pierre.

1 Like

When specifying multiple variables, you need to put -v in front of each of them:

nawk -v FD="$F_D" -v SS="$S_S" -v DS="$D_S" 'BEGIN {FS=","} {printf("%-1s%-4s%-30s","0",$2,FD,SS,DS)}'
1 Like

Thank you all... I have one more question
When i sum up the values of a column, i get the total in exponent, is it possible to convert in to a round fingure with 2 decimal places ?

awk -F, '{value=$28 ;if(value>0) {credit+=value}} END {print credit} ' joegtest_0705.txt
 
Output
1.20994e+07
 

Use printf "%.2f",credit in the END section.

1 Like

Hi

am getting this error and the output value gets rounded off to 120994150.00

 
printf  "%-0.2f"

printf: 12099415.79 not completely converted

Basically i need to get the value printed with
Right justified, zero filled, 2 decimal places like 999999999999999.99

Can you post sample output for that number - 12099415.79? While using code tags please, so we can see the alignment...

Actual input value : 12099415.79

Error message i get is : printf: 12099415.79 not completely converted

i used

 
printf "%-1s%-2s%-4s%-30s%-15s%-12s%-15s%-14s%-06d%-0.2f" "0" $H1 $H2 $F_D $S_S $D_P $S_P $F_L $T_R $CT 

Output is 120994150.00

If this printf part of the AWK code?

No the printf is just a standlone to print

am passing this value to printf as a variable value 12099415.79

12,2010
12099415.79
printf: 12099415.79 not completely converted
printf: \+ expected numeric value
printf: X expected numeric value
~~~~~~~~~ cat Final
0122010GJHKKJK_KKJ                  KKJ_BRIDGE     CJKHHGFFFFD  CTRON-GL       1030          120994150.00-11898578.290.00140X 
 
```text
printf "%-1s%-2s%-4s%-30s%-15s%-12s%-15s%-14s%-06d%-0.2f%-1s%-0.2f%-1s%-3s%-3s%-9s%-1s" "0" $H1 $H2 $F_D $S_S $D_P $S_P $F_L $T_R $CT "+" $DT "X" $SP $CMP $SPP "X" >> Final
```

What does it say before you run printf:

echo "$CT"

Also what shell are you using? What operating system?

echo $CT gives me 12099415.79

AM using sun Solaris 5.9 and KSH shell