print a column with the desire format output [closed]












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 "%18sn", $1}'. The ideal is to have an output like this:



   -0.3476890000000000
0.0743938333333333
-1.0990240000000000
0.0743938333333333
-0.2169100000000000
0.0879397500000000









share|improve this question















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
















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 "%18sn", $1}'. The ideal is to have an output like this:



   -0.3476890000000000
0.0743938333333333
-1.0990240000000000
0.0743938333333333
-0.2169100000000000
0.0879397500000000









share|improve this question















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














1












1








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 "%18sn", $1}'. The ideal is to have an output like this:



   -0.3476890000000000
0.0743938333333333
-1.0990240000000000
0.0743938333333333
-0.2169100000000000
0.0879397500000000









share|improve this question















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 "%18sn", $1}'. The ideal is to have an output like this:



   -0.3476890000000000
0.0743938333333333
-1.0990240000000000
0.0743938333333333
-0.2169100000000000
0.0879397500000000






awk text-formatting printf






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 23 '18 at 18:57









Jeff Schaller

39k1053125




39k1053125










asked Dec 23 '18 at 16:59









Dimitris Mintis

755




755




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.






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














  • 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








1




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




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




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










3 Answers
3






active

oldest

votes


















4














Maybe too small a field width. Try



awk '{printf "%19sn", $1}' file
-0.3476890000000000
0.0743938333333333
-1.0990240000000000
0.0743938333333333
-0.2169100000000000
0.0879397500000000





share|improve this answer





























    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.16fn", $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





    share|improve this answer





















    • 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.16fn'
    -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.16fn' $(cat file)





    share|improve this answer























    • 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


















    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    4














    Maybe too small a field width. Try



    awk '{printf "%19sn", $1}' file
    -0.3476890000000000
    0.0743938333333333
    -1.0990240000000000
    0.0743938333333333
    -0.2169100000000000
    0.0879397500000000





    share|improve this answer


























      4














      Maybe too small a field width. Try



      awk '{printf "%19sn", $1}' file
      -0.3476890000000000
      0.0743938333333333
      -1.0990240000000000
      0.0743938333333333
      -0.2169100000000000
      0.0879397500000000





      share|improve this answer
























        4












        4








        4






        Maybe too small a field width. Try



        awk '{printf "%19sn", $1}' file
        -0.3476890000000000
        0.0743938333333333
        -1.0990240000000000
        0.0743938333333333
        -0.2169100000000000
        0.0879397500000000





        share|improve this answer












        Maybe too small a field width. Try



        awk '{printf "%19sn", $1}' file
        -0.3476890000000000
        0.0743938333333333
        -1.0990240000000000
        0.0743938333333333
        -0.2169100000000000
        0.0879397500000000






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Dec 23 '18 at 17:12









        RudiC

        4,2041312




        4,2041312

























            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.16fn", $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





            share|improve this answer





















            • 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
















            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.16fn", $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





            share|improve this answer





















            • 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














            2












            2








            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.16fn", $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





            share|improve this answer












            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.16fn", $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






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Dec 23 '18 at 17:45









            steeldriver

            34.7k35184




            34.7k35184












            • 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


















            • 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
















            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




            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.16fn'
            -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.16fn' $(cat file)





            share|improve this answer























            • 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
















            1














            you can also use printf(1):



            xargs <file printf '%19.16fn'
            -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.16fn' $(cat file)





            share|improve this answer























            • 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














            1












            1








            1






            you can also use printf(1):



            xargs <file printf '%19.16fn'
            -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.16fn' $(cat file)





            share|improve this answer














            you can also use printf(1):



            xargs <file printf '%19.16fn'
            -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.16fn' $(cat file)






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Dec 23 '18 at 19:01

























            answered Dec 23 '18 at 18:20









            Uncle Billy

            3845




            3845












            • 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


















            • 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
















            Strictly speaking, if you're running it through xargs, you aren't running the builtin.
            – G-Man
            Dec 23 '18 at 18:48




            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




            @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



            Popular posts from this blog

            How do I know what Microsoft account the skydrive app is syncing to?

            When does type information flow backwards in C++?

            Grease: Live!