1

I want to right align the 3rd column using awk or any other UNIX tool such that all floating point numbers are centered with respect to decimal point.

Al     11.134  15.250 2.393
Al     11.134  5.825 2.393
Al     12.888  10.537 2.393

Please let me know if you have any suggestions. I tried to use formatting methods, but for floating point it seems that they are not working.

So the expected output is

Al     11.134  15.250 2.393
Al     11.134   5.825 2.393
Al     12.888  10.537 2.393
  • Hi Prasanth, What is the expected output. Share the sampel output. – Sagar Oct 16 '17 at 19:24
0

Awk solution:

awk '{ for(i=1;i<=NF;i++) printf (i==1?"%s%s":"%6s%s"),$i,(i==NF? ORS:OFS) }' file

The output:

Al 11.134 15.250  2.393
Al 11.134  5.825  2.393
Al 12.888 10.537  2.393
  • I want to have decimal centered columns. Please see the output in the question. I have updated it now. – Prasanth Oct 16 '17 at 19:27
  • @Prasanth, will you extend your input presenting possible number length variation? I mean, could be there numbers 102.12222 1003.181922 ? Is there a maximum number length? – RomanPerekhrest Oct 16 '17 at 19:34
  • I am sorry, I did not understand your question, but I want to keep the column length constant. I want only %2.3f column length. I tried to use %-2.3f and %2.3f with awk , but it did not work – Prasanth Oct 16 '17 at 19:38
  • @Prasanth, I got, see my update – RomanPerekhrest Oct 16 '17 at 19:39
  • 1
    @RobertL see the coments above by the OP. There will only ever be 3 digits after the decimal. – terdon Oct 20 '17 at 11:07
0

For the input data on file z6, code:

align z6

produces:

Al 11.134 15.250  2.393
Al 11.134  5.825  2.393
Al 12.888 10.537  2.393

Details for (perl) code align:

align   Align columns of text. (what)
Path    : ~/p/stm/common/scripts/align
Version : 1.7.0
Length  : 270 lines
Type    : Perl script, ASCII text executable
Shebang : #!/usr/bin/perl
Help    : probably available with --help
Home    : http://kinzler.com/me/align/ (doc)
Modules : (for perl codes)
 Getopt::Std    1.10

On a system like:

OS, ker|rel, machine: Linux, 3.16.0-4-amd64, x86_64
Distribution        : Debian 8.9 (jessie) 
bash GNU bash 4.3.30
align 1.7.0

Best wishes ... cheers, drl

0

This can be accomplished using awk's printf statement.

The reason "%2.3f" didn't work is because with %f<field-width>.<precision> the field-width is the total width of the field, including the decimal point and fraction part. <precision> is the number of digits after the decimal point.

When you typed %2.3f you probably expected to have 2 digits to the left of the decimal point and 3 digits to the right. To accomplish that, use %6.3f, which will align on the decimal point as long as there are 2 or fewer digits on the left of the decimal point.

You can try this:

awk '{ printf "%-6s ", $1; for (i=2; i<=NF; i++) printf "%6.3f ", $i; printf "\n"; }'

Here's the same code nicely formatted for readability and executed with sample data. I've added some extra data so you can see the formatting behavior when the input data is longer than the field widths:

$ awk '
    {
        printf "%-6s ", $1;
        for (i = 2; i <= NF; i++)
            printf "%6.3f ", $i;
        printf "\n";
    }
' <<EOF
Al     11.134  15.250 2.393
Al     11.134  5.825 2.393                                                                                                             
Al     12.888  10.537 2.393
Bundy  1.1 1.22 1.333
Jr     22.333 22.1 0.0
Longman 333.4444 4444.0 1.55555
EOF
Al     11.134 15.250  2.393 
Al     11.134  5.825  2.393 
Al     12.888 10.537  2.393 
Bundy   1.100  1.220  1.333 
Jr     22.333 22.100  0.000 
Longman 333.444 4444.000  1.556
$

See the awk and printf(3) man pages for more details.

  • your solution is also working. Thank you very much. – Prasanth Oct 16 '17 at 19:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.