1

I have a list of the following data in a file:

    -0.3476890000000000
    0.0743938333333333
    -1.0990240000000000
    0.0743938333333333
    -0.2169100000000000
    0.0879397500000000

and I am trying to align all data so to have:

    -0.347689000000000
    0.0743938333333333
    -1.099024000000000
    0.0743938333333333
    -0.216910000000000
    0.0879397500000000

unfortunately I couldn't make it with awk '{printf "%18s\n", $1}'. The ideal is to have an output like this:

   -0.3476890000000000
    0.0743938333333333
   -1.0990240000000000
    0.0743938333333333
   -0.2169100000000000
    0.0879397500000000

closed as unclear what you're asking by G-Man, peterh, Jeff Schaller, A.B, Inian Dec 28 '18 at 5:50

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    Your question says “I am trying to have X; the ideal is to have Y.”  What do you want?  Describe it in words.  Do you want the strings to be left aligned, so the - in front of negative numbers lines up with the first digit of non-negative numbers?  Do you want the numbers lined up on the .?  Do you want the strings to be right-aligned?  Do you want to truncate digits from the right?  Will you ever have an entry like .142857 (with no leading zero)?  Will you ever have a number ≥ 10 (i.e., more than one digit)? – G-Man Dec 23 '18 at 22:40
  • P.S. Every line of your sample file has 17 digits, where the rightmost 8 digits are the same.  This is very hard to read.  In the future, please present questions with data that can be looked at and understood. – G-Man Dec 23 '18 at 22:40
4

Maybe too small a field width. Try

awk '{printf "%19s\n", $1}' file
-0.3476890000000000
 0.0743938333333333
-1.0990240000000000
 0.0743938333333333
-0.2169100000000000
 0.0879397500000000
2

Both GNU Awk (gawk) and mawk appear to support the C printf ' ' modifier that adds a space in front of positive numbers to align them with negative ones

   ' '    (a  space)  A  blank should be left before a positive number (or
          empty string) produced by a signed conversion.

So for example:

$ mawk '{printf "% 22.16f\n", $1}' file
   -0.3476890000000000
    0.0743938333333333
   -1.0990240000000000
    0.0743938333333333
   -0.2169100000000000
    0.0879397500000000

Alternatively, with numfmt

$ numfmt --format='% 22.16f' < file
   -0.3476890000000000
    0.0743938333333333
   -1.0990240000000000
    0.0743938333333333
   -0.2169100000000000
    0.0879397500000000
  • As far as I can tell, this is equivalent to %22.16f (without the space) except, with % 22.16f (with the space), numbers  ≥ 10000 will have an extra space added in front, so they line up with numbers  ≤ −10000, but not with numbers strictly between −10000 and 10000.  This might or might not be what the OP wants. – G-Man Dec 23 '18 at 22:41
1

you can also use printf(1):

xargs <file printf '%19.16f\n'
-0.3476890000000000
 0.0743938333333333
-1.0990240000000000
 0.0743938333333333
-0.2169100000000000
 0.0879397500000000

or, if the input file isn't too big, the printf shell built-in:

undef IFS
printf '%19.16f\n' $(cat file)
  • Strictly speaking, if you're running it through xargs, you aren't running the builtin. – G-Man Dec 23 '18 at 18:48
  • @G-Man right, and that's not "simply speaking". It was "simply wrong"; xargs cannot run shell built-ins. – Uncle Billy Dec 23 '18 at 19:04

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