Add text to each column [duplicate]

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
3
down vote

favorite













This question already has an answer here:



  • Formatted shell script output

    3 answers



I have the following lines



3, 3, 100
4, 2, 50
8, 5, 80
.
.
.


and I want the following output



line starts at 3 and ends at 3 with value 100
line starts at 4 and ends at 2 with value 50
line starts at 8 and ends at 5 with value 80
.
.
.


I tried the following: sed 's/^/line starts at /' then applying this command for the output: sed 's/, / and ends at /' then applying this command for the output sed 's/, / with value /'. Is there any way to do it in a single line?







share|improve this question














marked as duplicate by l0b0, Archemar, Kiwy, Stephen Kitt, Timothy Martin Apr 5 at 0:11


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • I tried the following: sed 's/^/line starts at /' then applying this command for the output: sed 's/, / and ends at /' then applying this command for the output sed 's/, / with value /'. Is there any way to do it in a single line?
    – Franky
    Apr 3 at 3:00














up vote
3
down vote

favorite













This question already has an answer here:



  • Formatted shell script output

    3 answers



I have the following lines



3, 3, 100
4, 2, 50
8, 5, 80
.
.
.


and I want the following output



line starts at 3 and ends at 3 with value 100
line starts at 4 and ends at 2 with value 50
line starts at 8 and ends at 5 with value 80
.
.
.


I tried the following: sed 's/^/line starts at /' then applying this command for the output: sed 's/, / and ends at /' then applying this command for the output sed 's/, / with value /'. Is there any way to do it in a single line?







share|improve this question














marked as duplicate by l0b0, Archemar, Kiwy, Stephen Kitt, Timothy Martin Apr 5 at 0:11


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • I tried the following: sed 's/^/line starts at /' then applying this command for the output: sed 's/, / and ends at /' then applying this command for the output sed 's/, / with value /'. Is there any way to do it in a single line?
    – Franky
    Apr 3 at 3:00












up vote
3
down vote

favorite









up vote
3
down vote

favorite












This question already has an answer here:



  • Formatted shell script output

    3 answers



I have the following lines



3, 3, 100
4, 2, 50
8, 5, 80
.
.
.


and I want the following output



line starts at 3 and ends at 3 with value 100
line starts at 4 and ends at 2 with value 50
line starts at 8 and ends at 5 with value 80
.
.
.


I tried the following: sed 's/^/line starts at /' then applying this command for the output: sed 's/, / and ends at /' then applying this command for the output sed 's/, / with value /'. Is there any way to do it in a single line?







share|improve this question















This question already has an answer here:



  • Formatted shell script output

    3 answers



I have the following lines



3, 3, 100
4, 2, 50
8, 5, 80
.
.
.


and I want the following output



line starts at 3 and ends at 3 with value 100
line starts at 4 and ends at 2 with value 50
line starts at 8 and ends at 5 with value 80
.
.
.


I tried the following: sed 's/^/line starts at /' then applying this command for the output: sed 's/, / and ends at /' then applying this command for the output sed 's/, / with value /'. Is there any way to do it in a single line?





This question already has an answer here:



  • Formatted shell script output

    3 answers









share|improve this question













share|improve this question




share|improve this question








edited Apr 3 at 6:21









Sundeep

6,9511826




6,9511826










asked Apr 3 at 2:46









Franky

385




385




marked as duplicate by l0b0, Archemar, Kiwy, Stephen Kitt, Timothy Martin Apr 5 at 0:11


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by l0b0, Archemar, Kiwy, Stephen Kitt, Timothy Martin Apr 5 at 0:11


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.













  • I tried the following: sed 's/^/line starts at /' then applying this command for the output: sed 's/, / and ends at /' then applying this command for the output sed 's/, / with value /'. Is there any way to do it in a single line?
    – Franky
    Apr 3 at 3:00
















  • I tried the following: sed 's/^/line starts at /' then applying this command for the output: sed 's/, / and ends at /' then applying this command for the output sed 's/, / with value /'. Is there any way to do it in a single line?
    – Franky
    Apr 3 at 3:00















I tried the following: sed 's/^/line starts at /' then applying this command for the output: sed 's/, / and ends at /' then applying this command for the output sed 's/, / with value /'. Is there any way to do it in a single line?
– Franky
Apr 3 at 3:00




I tried the following: sed 's/^/line starts at /' then applying this command for the output: sed 's/, / and ends at /' then applying this command for the output sed 's/, / with value /'. Is there any way to do it in a single line?
– Franky
Apr 3 at 3:00










4 Answers
4






active

oldest

votes

















up vote
12
down vote



accepted










awk is good for this kind of formatted input - formatted output:



awk -F, 'printf("line starts at %d and ends at %d with value %dn", $1, $2, $3)' file 
line starts at 3 and ends at 3 with value 100
line starts at 4 and ends at 2 with value 50
line starts at 8 and ends at 5 with value 80





share|improve this answer




















  • This is absolutely beautiful!
    – Franky
    Apr 4 at 19:27

















up vote
3
down vote













A shell while read loop with printf:



while IFS=', ' read c1 c2 c3; do
printf 'line starts at %s and ends at %s with value %sn'
"$c1" "$c2" "$c3"
done <file


By setting the IFS variable to a space and a comma, the read command will use those characters as field delimiters.



Output:



line starts at 3 and ends at 3 with value 100
line starts at 4 and ends at 2 with value 50
line starts at 8 and ends at 5 with value 80





share|improve this answer



























    up vote
    2
    down vote













    It turns out that there is -e option in sed



    sed -e 's/^/line starts at /g' -e 's/, / and ends at /' -e 's/, / with value at /'





    share|improve this answer




















    • see also: gnu.org/software/sed/manual/sed.html#Multiple-commands-syntax
      – Sundeep
      Apr 3 at 6:22






    • 2




      @Franky, steeldriver has better answer
      – WashichawbachaW
      Apr 3 at 7:21










    • @WashichawbachaW I do agree! It is a much better line
      – Franky
      Apr 4 at 19:27

















    up vote
    0
    down vote













    There is a easy and fast way to do this in shell itself:-





    # cat f
    3, 3, 100
    4, 2, 50
    8, 5, 80

    # cat f | while read line ; do IFS=", " array=($line) ; echo "line starts at $array[0] and ends at $array[1] with value $array[2]"; done

    line starts at 3 and ends at 3 with value 100
    line starts at 4 and ends at 2 with value 50
    line starts at 8 and ends at 5 with value 80



    Hope it helps






    share|improve this answer
















    • 2




      Why not split it up in the read itself instead of doing the extra step of creating an array?
      – Kusalananda
      Apr 3 at 9:02










    • I hope you're not actually running this as root…
      – Kevin
      Apr 3 at 17:29










    • Oh yes, that is much better
      – ananTgarg
      Apr 4 at 11:45

















    4 Answers
    4






    active

    oldest

    votes








    4 Answers
    4






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    12
    down vote



    accepted










    awk is good for this kind of formatted input - formatted output:



    awk -F, 'printf("line starts at %d and ends at %d with value %dn", $1, $2, $3)' file 
    line starts at 3 and ends at 3 with value 100
    line starts at 4 and ends at 2 with value 50
    line starts at 8 and ends at 5 with value 80





    share|improve this answer




















    • This is absolutely beautiful!
      – Franky
      Apr 4 at 19:27














    up vote
    12
    down vote



    accepted










    awk is good for this kind of formatted input - formatted output:



    awk -F, 'printf("line starts at %d and ends at %d with value %dn", $1, $2, $3)' file 
    line starts at 3 and ends at 3 with value 100
    line starts at 4 and ends at 2 with value 50
    line starts at 8 and ends at 5 with value 80





    share|improve this answer




















    • This is absolutely beautiful!
      – Franky
      Apr 4 at 19:27












    up vote
    12
    down vote



    accepted







    up vote
    12
    down vote



    accepted






    awk is good for this kind of formatted input - formatted output:



    awk -F, 'printf("line starts at %d and ends at %d with value %dn", $1, $2, $3)' file 
    line starts at 3 and ends at 3 with value 100
    line starts at 4 and ends at 2 with value 50
    line starts at 8 and ends at 5 with value 80





    share|improve this answer












    awk is good for this kind of formatted input - formatted output:



    awk -F, 'printf("line starts at %d and ends at %d with value %dn", $1, $2, $3)' file 
    line starts at 3 and ends at 3 with value 100
    line starts at 4 and ends at 2 with value 50
    line starts at 8 and ends at 5 with value 80






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Apr 3 at 3:33









    steeldriver

    31.5k34978




    31.5k34978











    • This is absolutely beautiful!
      – Franky
      Apr 4 at 19:27
















    • This is absolutely beautiful!
      – Franky
      Apr 4 at 19:27















    This is absolutely beautiful!
    – Franky
    Apr 4 at 19:27




    This is absolutely beautiful!
    – Franky
    Apr 4 at 19:27












    up vote
    3
    down vote













    A shell while read loop with printf:



    while IFS=', ' read c1 c2 c3; do
    printf 'line starts at %s and ends at %s with value %sn'
    "$c1" "$c2" "$c3"
    done <file


    By setting the IFS variable to a space and a comma, the read command will use those characters as field delimiters.



    Output:



    line starts at 3 and ends at 3 with value 100
    line starts at 4 and ends at 2 with value 50
    line starts at 8 and ends at 5 with value 80





    share|improve this answer
























      up vote
      3
      down vote













      A shell while read loop with printf:



      while IFS=', ' read c1 c2 c3; do
      printf 'line starts at %s and ends at %s with value %sn'
      "$c1" "$c2" "$c3"
      done <file


      By setting the IFS variable to a space and a comma, the read command will use those characters as field delimiters.



      Output:



      line starts at 3 and ends at 3 with value 100
      line starts at 4 and ends at 2 with value 50
      line starts at 8 and ends at 5 with value 80





      share|improve this answer






















        up vote
        3
        down vote










        up vote
        3
        down vote









        A shell while read loop with printf:



        while IFS=', ' read c1 c2 c3; do
        printf 'line starts at %s and ends at %s with value %sn'
        "$c1" "$c2" "$c3"
        done <file


        By setting the IFS variable to a space and a comma, the read command will use those characters as field delimiters.



        Output:



        line starts at 3 and ends at 3 with value 100
        line starts at 4 and ends at 2 with value 50
        line starts at 8 and ends at 5 with value 80





        share|improve this answer












        A shell while read loop with printf:



        while IFS=', ' read c1 c2 c3; do
        printf 'line starts at %s and ends at %s with value %sn'
        "$c1" "$c2" "$c3"
        done <file


        By setting the IFS variable to a space and a comma, the read command will use those characters as field delimiters.



        Output:



        line starts at 3 and ends at 3 with value 100
        line starts at 4 and ends at 2 with value 50
        line starts at 8 and ends at 5 with value 80






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Apr 3 at 10:26









        Kusalananda

        102k13201317




        102k13201317




















            up vote
            2
            down vote













            It turns out that there is -e option in sed



            sed -e 's/^/line starts at /g' -e 's/, / and ends at /' -e 's/, / with value at /'





            share|improve this answer




















            • see also: gnu.org/software/sed/manual/sed.html#Multiple-commands-syntax
              – Sundeep
              Apr 3 at 6:22






            • 2




              @Franky, steeldriver has better answer
              – WashichawbachaW
              Apr 3 at 7:21










            • @WashichawbachaW I do agree! It is a much better line
              – Franky
              Apr 4 at 19:27














            up vote
            2
            down vote













            It turns out that there is -e option in sed



            sed -e 's/^/line starts at /g' -e 's/, / and ends at /' -e 's/, / with value at /'





            share|improve this answer




















            • see also: gnu.org/software/sed/manual/sed.html#Multiple-commands-syntax
              – Sundeep
              Apr 3 at 6:22






            • 2




              @Franky, steeldriver has better answer
              – WashichawbachaW
              Apr 3 at 7:21










            • @WashichawbachaW I do agree! It is a much better line
              – Franky
              Apr 4 at 19:27












            up vote
            2
            down vote










            up vote
            2
            down vote









            It turns out that there is -e option in sed



            sed -e 's/^/line starts at /g' -e 's/, / and ends at /' -e 's/, / with value at /'





            share|improve this answer












            It turns out that there is -e option in sed



            sed -e 's/^/line starts at /g' -e 's/, / and ends at /' -e 's/, / with value at /'






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Apr 3 at 3:20









            Franky

            385




            385











            • see also: gnu.org/software/sed/manual/sed.html#Multiple-commands-syntax
              – Sundeep
              Apr 3 at 6:22






            • 2




              @Franky, steeldriver has better answer
              – WashichawbachaW
              Apr 3 at 7:21










            • @WashichawbachaW I do agree! It is a much better line
              – Franky
              Apr 4 at 19:27
















            • see also: gnu.org/software/sed/manual/sed.html#Multiple-commands-syntax
              – Sundeep
              Apr 3 at 6:22






            • 2




              @Franky, steeldriver has better answer
              – WashichawbachaW
              Apr 3 at 7:21










            • @WashichawbachaW I do agree! It is a much better line
              – Franky
              Apr 4 at 19:27















            see also: gnu.org/software/sed/manual/sed.html#Multiple-commands-syntax
            – Sundeep
            Apr 3 at 6:22




            see also: gnu.org/software/sed/manual/sed.html#Multiple-commands-syntax
            – Sundeep
            Apr 3 at 6:22




            2




            2




            @Franky, steeldriver has better answer
            – WashichawbachaW
            Apr 3 at 7:21




            @Franky, steeldriver has better answer
            – WashichawbachaW
            Apr 3 at 7:21












            @WashichawbachaW I do agree! It is a much better line
            – Franky
            Apr 4 at 19:27




            @WashichawbachaW I do agree! It is a much better line
            – Franky
            Apr 4 at 19:27










            up vote
            0
            down vote













            There is a easy and fast way to do this in shell itself:-





            # cat f
            3, 3, 100
            4, 2, 50
            8, 5, 80

            # cat f | while read line ; do IFS=", " array=($line) ; echo "line starts at $array[0] and ends at $array[1] with value $array[2]"; done

            line starts at 3 and ends at 3 with value 100
            line starts at 4 and ends at 2 with value 50
            line starts at 8 and ends at 5 with value 80



            Hope it helps






            share|improve this answer
















            • 2




              Why not split it up in the read itself instead of doing the extra step of creating an array?
              – Kusalananda
              Apr 3 at 9:02










            • I hope you're not actually running this as root…
              – Kevin
              Apr 3 at 17:29










            • Oh yes, that is much better
              – ananTgarg
              Apr 4 at 11:45














            up vote
            0
            down vote













            There is a easy and fast way to do this in shell itself:-





            # cat f
            3, 3, 100
            4, 2, 50
            8, 5, 80

            # cat f | while read line ; do IFS=", " array=($line) ; echo "line starts at $array[0] and ends at $array[1] with value $array[2]"; done

            line starts at 3 and ends at 3 with value 100
            line starts at 4 and ends at 2 with value 50
            line starts at 8 and ends at 5 with value 80



            Hope it helps






            share|improve this answer
















            • 2




              Why not split it up in the read itself instead of doing the extra step of creating an array?
              – Kusalananda
              Apr 3 at 9:02










            • I hope you're not actually running this as root…
              – Kevin
              Apr 3 at 17:29










            • Oh yes, that is much better
              – ananTgarg
              Apr 4 at 11:45












            up vote
            0
            down vote










            up vote
            0
            down vote









            There is a easy and fast way to do this in shell itself:-





            # cat f
            3, 3, 100
            4, 2, 50
            8, 5, 80

            # cat f | while read line ; do IFS=", " array=($line) ; echo "line starts at $array[0] and ends at $array[1] with value $array[2]"; done

            line starts at 3 and ends at 3 with value 100
            line starts at 4 and ends at 2 with value 50
            line starts at 8 and ends at 5 with value 80



            Hope it helps






            share|improve this answer












            There is a easy and fast way to do this in shell itself:-





            # cat f
            3, 3, 100
            4, 2, 50
            8, 5, 80

            # cat f | while read line ; do IFS=", " array=($line) ; echo "line starts at $array[0] and ends at $array[1] with value $array[2]"; done

            line starts at 3 and ends at 3 with value 100
            line starts at 4 and ends at 2 with value 50
            line starts at 8 and ends at 5 with value 80



            Hope it helps







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Apr 3 at 8:28









            ananTgarg

            304




            304







            • 2




              Why not split it up in the read itself instead of doing the extra step of creating an array?
              – Kusalananda
              Apr 3 at 9:02










            • I hope you're not actually running this as root…
              – Kevin
              Apr 3 at 17:29










            • Oh yes, that is much better
              – ananTgarg
              Apr 4 at 11:45












            • 2




              Why not split it up in the read itself instead of doing the extra step of creating an array?
              – Kusalananda
              Apr 3 at 9:02










            • I hope you're not actually running this as root…
              – Kevin
              Apr 3 at 17:29










            • Oh yes, that is much better
              – ananTgarg
              Apr 4 at 11:45







            2




            2




            Why not split it up in the read itself instead of doing the extra step of creating an array?
            – Kusalananda
            Apr 3 at 9:02




            Why not split it up in the read itself instead of doing the extra step of creating an array?
            – Kusalananda
            Apr 3 at 9:02












            I hope you're not actually running this as root…
            – Kevin
            Apr 3 at 17:29




            I hope you're not actually running this as root…
            – Kevin
            Apr 3 at 17:29












            Oh yes, that is much better
            – ananTgarg
            Apr 4 at 11:45




            Oh yes, that is much better
            – ananTgarg
            Apr 4 at 11:45


            Popular posts from this blog

            Peggy Mitchell

            Palaiologos

            The Forum (Inglewood, California)