Misaligned AWK output
I have simple awk command generating output and then sending an email:
awk '(NR==FNR){a[$2]=sprintf("%.2f",$1*value); next} {print $1,$2,a[$2]}' OFS="\t\t\t" value=$COST /tmp/1.txt /tmp/2.txt
however the last column is misaligned based on the second one:
510G /path/to/aaaaaaaaaaaaa/ 0.00
157G /path/to/bbbbbbbbb/ 0.00
253M /path/to/ccccccccccccccc/ 0.00
61M /path/to/dddddddd/ 0.00
16K /path/to/eeeee/ 0.00
8.0K /path/to/fffffffff/ 0.00
How can I enforce this to be always aligned? /path/to
is static.
Solution 1:
You can pipe the output of your command to column -t
to format any output to columns. The input needs to be delimited by whitespace, so you don't need to guess how many tabs you need to add.
For example, supposing a file contains this:
510G /path/to/aaaaaaaaaaaaa/ 0.00
157G /path/to/bbbbbbbbb/ 0.00
253M /path/to/ccccccccccccccc/ 0.00
61M /path/to/dddddddd/ 0.00
16K /path/to/eeeee/ 0.00
8.0K /path/to/fffffffff/ 0.00
the cat the.file|column -t
command produces this output:
510G /path/to/aaaaaaaaaaaaa/ 0.00
157G /path/to/bbbbbbbbb/ 0.00
253M /path/to/ccccccccccccccc/ 0.00
61M /path/to/dddddddd/ 0.00
16K /path/to/eeeee/ 0.00
8.0K /path/to/fffffffff/ 0.00