I get some data from a query in mysql that I need to convert to a nice looking table.
Then I use awk and printf to align columns, format numbers, etc.
cat /home/jm/mysql/pruebas/pruebat.txt | awk 'BEGIN { FS = ";" } ; {printf "%-'$A's %-'$B's %'\'''$C'd %'\'''$D'd %'\'''$E'd %'\'''$F'.2f %'\'''$F'.2f %'\'''$F'.2f\n", $1, $2, $3, $4, $5, $6, $7, $8 }' >> /home/jm/mysql/pruebas/prn.tx
My problem appears when some fields are too long (say, the % variation between too years is -12.345,67%). I would prefer to show just ###### in that case.
If I use a case/when/then/end in my query, I can substitute the long value by the string ######, but as awk+printf reads this field as a decimal number, it shows 0.00% instead of the desired ######.
Is there any way to write “if this variable is too long, just print ######”?
Thanks,
jm
EDIT:
My source file (forget about headers):
S_V;NO INVENTARIABLES;-41.7000;-67.5200;-25.8200;61.91846523;100.00000000;100.00000000
S_A;ARTICULOS PROMOCIONES;54.4400;631.3200;576.8800;1059.66201323;-1412.30712711;-19.76018501
S_B;PRODUCTOS TECNICOS;975.6000;1951.2000;975.6000;100.00000000;36.01476015;34.61254613
S_S;MATERIAL ELECTRICO;2237.6600;5103.2500;2865.5900;128.06190395;40.21030898;43.42859942
My output:
fam nombre 2011 2012 var % %mgn11 %mgn12
--------------------------------------------------------------------------------------------------
S_V NO INVENTARIABLES -41 -67 -25 61.92 100.00 100.00
S_A ARTICULOS PROMOCIONES 54 631 576 1,059.66 -1,412.31 -19.76
S_B PRODUCTOS TECNICOS 975 1,951 975 100.00 36.01 34.61
S_S MATERIAL ELECTRICO 2,237 5,103 2,865 128.06 40.21 43.43
My wish (or similar):
fam nombre 2011 2012 var % %mgn11 %mgn12
--------------------------------------------------------------------------------------------------
S_V NO INVENTARIABLES -41 -67 -25 61.92 100.00 100.00
S_A ARTICULOS PROMOCIONES 54 631 576 ###### ###### -19.76
S_B PRODUCTOS TECNICOS 975 1,951 975 100.00 36.01 34.61
S_S MATERIAL ELECTRICO 2,237 5,103 2,865 128.06 40.21 43.43
Let’s start by rewriting your script in reasonable shell+awk syntax, best I can tell:
Now, the problem you have is that your format string is specifically saying to print a number when in fact you want to print a string, so use sprintf to create your numbers and then if you’re not happy with the result change it to a string for output. Something like this (untested):