Shell - Print values in a comma separated string

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











up vote
0
down vote

favorite












I have a txt file with some comma separated values.



cat file.txt

abc,def,ghi
abc,ghi
def,abc,ghi
def,abc
abc,def
abc,def,ghi


I want to print these values with while do read line from file separated by comma.



Eg:



expecting output for Line no 1:
first col=abc
second col=def
third col=ghi
expecting output for Line no 2:
first col=abc
second col=ghi


If the line has three values then the read line should print



first col=value
second col=value
third col=value


else



first col=value
second col=value


How can I create this shell script?










share|improve this question



























    up vote
    0
    down vote

    favorite












    I have a txt file with some comma separated values.



    cat file.txt

    abc,def,ghi
    abc,ghi
    def,abc,ghi
    def,abc
    abc,def
    abc,def,ghi


    I want to print these values with while do read line from file separated by comma.



    Eg:



    expecting output for Line no 1:
    first col=abc
    second col=def
    third col=ghi
    expecting output for Line no 2:
    first col=abc
    second col=ghi


    If the line has three values then the read line should print



    first col=value
    second col=value
    third col=value


    else



    first col=value
    second col=value


    How can I create this shell script?










    share|improve this question

























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I have a txt file with some comma separated values.



      cat file.txt

      abc,def,ghi
      abc,ghi
      def,abc,ghi
      def,abc
      abc,def
      abc,def,ghi


      I want to print these values with while do read line from file separated by comma.



      Eg:



      expecting output for Line no 1:
      first col=abc
      second col=def
      third col=ghi
      expecting output for Line no 2:
      first col=abc
      second col=ghi


      If the line has three values then the read line should print



      first col=value
      second col=value
      third col=value


      else



      first col=value
      second col=value


      How can I create this shell script?










      share|improve this question















      I have a txt file with some comma separated values.



      cat file.txt

      abc,def,ghi
      abc,ghi
      def,abc,ghi
      def,abc
      abc,def
      abc,def,ghi


      I want to print these values with while do read line from file separated by comma.



      Eg:



      expecting output for Line no 1:
      first col=abc
      second col=def
      third col=ghi
      expecting output for Line no 2:
      first col=abc
      second col=ghi


      If the line has three values then the read line should print



      first col=value
      second col=value
      third col=value


      else



      first col=value
      second col=value


      How can I create this shell script?







      linux shell printing echo readline






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 26 at 19:03









      Rui F Ribeiro

      38.4k1477127




      38.4k1477127










      asked Nov 26 at 18:52









      SQLadmin

      1095




      1095




















          3 Answers
          3






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          $ awk -F, ' print "line " NR; for (i=1;i<=NF;i++) print "Col " i "="$i ' input
          line 1
          Col 1=abc
          Col 2=def
          Col 3=ghi
          line 2
          Col 1=abc
          Col 2=ghi
          line 3
          Col 1=def
          Col 2=abc
          Col 3=ghi
          line 4
          Col 1=def
          Col 2=abc
          line 5
          Col 1=abc
          Col 2=def
          line 6
          Col 1=abc
          Col 2=def
          Col 3=ghi


          If you really want to transliterate from numerical columns to "first", "second", et cetera, you can define an array and use i as the index to look up the word that matches the number.






          share|improve this answer




















          • Thanks for your answer. Its awesome, If I want to use the same case instead of a file, I want to do inline, like ` awk -F, ' for (i=1;i<=NF;i++) print "Col " i "="$i ' "abc,def,ghi" ` How can I use the same command for this?
            – SQLadmin
            Nov 26 at 19:04










          • echo "abc,def,ghi" | awk [...]
            – DopeGhoti
            Nov 26 at 19:05

















          up vote
          1
          down vote













          With bash you could do



          ordinals=( first second third fourth fifth sixth )
          n=0
          while IFS=, read -ra cols; do
          echo "line $((++n))"
          for i in "$!cols[@]"; do
          echo "$ordinals[i] col=$cols[i]"
          done
          done < file


          That reads the words in each line into an array named cols, then we interate over the indices of that array so we can correlate the value to the ordinal.



          For the first 3 lines, we get



          line 1
          first col=abc
          second col=def
          third col=ghi
          line 2
          first col=abc
          second col=ghi
          line 3
          first col=def
          second col=abc
          third col=ghi





          share|improve this answer





























            up vote
            0
            down vote













            Assuming that the input file only has at most three columns, the following uses a while-read loop to read the comma-separated values from standard input and outputs them in a format similar to what you showed:



            #!/bin/sh

            while IFS=, read -r first second third
            do
            printf 'Line %d:n' "$(( ++n ))"
            $first:+printf 'First:t%sn' "$first"
            $second:+printf 'Second:t%sn' "$second"
            $third:+printf 'Third:t%sn' "$third"
            done


            The parameter expansion $variable:+word expands to word if variable is set and non-empty. The code uses this to execute printf for output if the corresponding variable contains data to be printed.



            Testing on the provided data:



            $ ./script.sh <file
            Line 1:
            First: abc
            Second: def
            Third: ghi
            Line 2:
            First: abc
            Second: ghi
            Line 3:
            First: def
            Second: abc
            Third: ghi
            Line 4:
            First: def
            Second: abc
            Line 5:
            First: abc
            Second: def
            Line 6:
            First: abc
            Second: def
            Third: ghi





            share|improve this answer




















              Your Answer








              StackExchange.ready(function()
              var channelOptions =
              tags: "".split(" "),
              id: "106"
              ;
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function()
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled)
              StackExchange.using("snippets", function()
              createEditor();
              );

              else
              createEditor();

              );

              function createEditor()
              StackExchange.prepareEditor(
              heartbeatType: 'answer',
              convertImagesToLinks: false,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: null,
              bindNavPrevention: true,
              postfix: "",
              imageUploader:
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              ,
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              );



              );













              draft saved

              draft discarded


















              StackExchange.ready(
              function ()
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f484264%2fshell-print-values-in-a-comma-separated-string%23new-answer', 'question_page');

              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              0
              down vote



              accepted










              $ awk -F, ' print "line " NR; for (i=1;i<=NF;i++) print "Col " i "="$i ' input
              line 1
              Col 1=abc
              Col 2=def
              Col 3=ghi
              line 2
              Col 1=abc
              Col 2=ghi
              line 3
              Col 1=def
              Col 2=abc
              Col 3=ghi
              line 4
              Col 1=def
              Col 2=abc
              line 5
              Col 1=abc
              Col 2=def
              line 6
              Col 1=abc
              Col 2=def
              Col 3=ghi


              If you really want to transliterate from numerical columns to "first", "second", et cetera, you can define an array and use i as the index to look up the word that matches the number.






              share|improve this answer




















              • Thanks for your answer. Its awesome, If I want to use the same case instead of a file, I want to do inline, like ` awk -F, ' for (i=1;i<=NF;i++) print "Col " i "="$i ' "abc,def,ghi" ` How can I use the same command for this?
                – SQLadmin
                Nov 26 at 19:04










              • echo "abc,def,ghi" | awk [...]
                – DopeGhoti
                Nov 26 at 19:05














              up vote
              0
              down vote



              accepted










              $ awk -F, ' print "line " NR; for (i=1;i<=NF;i++) print "Col " i "="$i ' input
              line 1
              Col 1=abc
              Col 2=def
              Col 3=ghi
              line 2
              Col 1=abc
              Col 2=ghi
              line 3
              Col 1=def
              Col 2=abc
              Col 3=ghi
              line 4
              Col 1=def
              Col 2=abc
              line 5
              Col 1=abc
              Col 2=def
              line 6
              Col 1=abc
              Col 2=def
              Col 3=ghi


              If you really want to transliterate from numerical columns to "first", "second", et cetera, you can define an array and use i as the index to look up the word that matches the number.






              share|improve this answer




















              • Thanks for your answer. Its awesome, If I want to use the same case instead of a file, I want to do inline, like ` awk -F, ' for (i=1;i<=NF;i++) print "Col " i "="$i ' "abc,def,ghi" ` How can I use the same command for this?
                – SQLadmin
                Nov 26 at 19:04










              • echo "abc,def,ghi" | awk [...]
                – DopeGhoti
                Nov 26 at 19:05












              up vote
              0
              down vote



              accepted







              up vote
              0
              down vote



              accepted






              $ awk -F, ' print "line " NR; for (i=1;i<=NF;i++) print "Col " i "="$i ' input
              line 1
              Col 1=abc
              Col 2=def
              Col 3=ghi
              line 2
              Col 1=abc
              Col 2=ghi
              line 3
              Col 1=def
              Col 2=abc
              Col 3=ghi
              line 4
              Col 1=def
              Col 2=abc
              line 5
              Col 1=abc
              Col 2=def
              line 6
              Col 1=abc
              Col 2=def
              Col 3=ghi


              If you really want to transliterate from numerical columns to "first", "second", et cetera, you can define an array and use i as the index to look up the word that matches the number.






              share|improve this answer












              $ awk -F, ' print "line " NR; for (i=1;i<=NF;i++) print "Col " i "="$i ' input
              line 1
              Col 1=abc
              Col 2=def
              Col 3=ghi
              line 2
              Col 1=abc
              Col 2=ghi
              line 3
              Col 1=def
              Col 2=abc
              Col 3=ghi
              line 4
              Col 1=def
              Col 2=abc
              line 5
              Col 1=abc
              Col 2=def
              line 6
              Col 1=abc
              Col 2=def
              Col 3=ghi


              If you really want to transliterate from numerical columns to "first", "second", et cetera, you can define an array and use i as the index to look up the word that matches the number.







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 26 at 18:58









              DopeGhoti

              42.8k55181




              42.8k55181











              • Thanks for your answer. Its awesome, If I want to use the same case instead of a file, I want to do inline, like ` awk -F, ' for (i=1;i<=NF;i++) print "Col " i "="$i ' "abc,def,ghi" ` How can I use the same command for this?
                – SQLadmin
                Nov 26 at 19:04










              • echo "abc,def,ghi" | awk [...]
                – DopeGhoti
                Nov 26 at 19:05
















              • Thanks for your answer. Its awesome, If I want to use the same case instead of a file, I want to do inline, like ` awk -F, ' for (i=1;i<=NF;i++) print "Col " i "="$i ' "abc,def,ghi" ` How can I use the same command for this?
                – SQLadmin
                Nov 26 at 19:04










              • echo "abc,def,ghi" | awk [...]
                – DopeGhoti
                Nov 26 at 19:05















              Thanks for your answer. Its awesome, If I want to use the same case instead of a file, I want to do inline, like ` awk -F, ' for (i=1;i<=NF;i++) print "Col " i "="$i ' "abc,def,ghi" ` How can I use the same command for this?
              – SQLadmin
              Nov 26 at 19:04




              Thanks for your answer. Its awesome, If I want to use the same case instead of a file, I want to do inline, like ` awk -F, ' for (i=1;i<=NF;i++) print "Col " i "="$i ' "abc,def,ghi" ` How can I use the same command for this?
              – SQLadmin
              Nov 26 at 19:04












              echo "abc,def,ghi" | awk [...]
              – DopeGhoti
              Nov 26 at 19:05




              echo "abc,def,ghi" | awk [...]
              – DopeGhoti
              Nov 26 at 19:05












              up vote
              1
              down vote













              With bash you could do



              ordinals=( first second third fourth fifth sixth )
              n=0
              while IFS=, read -ra cols; do
              echo "line $((++n))"
              for i in "$!cols[@]"; do
              echo "$ordinals[i] col=$cols[i]"
              done
              done < file


              That reads the words in each line into an array named cols, then we interate over the indices of that array so we can correlate the value to the ordinal.



              For the first 3 lines, we get



              line 1
              first col=abc
              second col=def
              third col=ghi
              line 2
              first col=abc
              second col=ghi
              line 3
              first col=def
              second col=abc
              third col=ghi





              share|improve this answer


























                up vote
                1
                down vote













                With bash you could do



                ordinals=( first second third fourth fifth sixth )
                n=0
                while IFS=, read -ra cols; do
                echo "line $((++n))"
                for i in "$!cols[@]"; do
                echo "$ordinals[i] col=$cols[i]"
                done
                done < file


                That reads the words in each line into an array named cols, then we interate over the indices of that array so we can correlate the value to the ordinal.



                For the first 3 lines, we get



                line 1
                first col=abc
                second col=def
                third col=ghi
                line 2
                first col=abc
                second col=ghi
                line 3
                first col=def
                second col=abc
                third col=ghi





                share|improve this answer
























                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  With bash you could do



                  ordinals=( first second third fourth fifth sixth )
                  n=0
                  while IFS=, read -ra cols; do
                  echo "line $((++n))"
                  for i in "$!cols[@]"; do
                  echo "$ordinals[i] col=$cols[i]"
                  done
                  done < file


                  That reads the words in each line into an array named cols, then we interate over the indices of that array so we can correlate the value to the ordinal.



                  For the first 3 lines, we get



                  line 1
                  first col=abc
                  second col=def
                  third col=ghi
                  line 2
                  first col=abc
                  second col=ghi
                  line 3
                  first col=def
                  second col=abc
                  third col=ghi





                  share|improve this answer














                  With bash you could do



                  ordinals=( first second third fourth fifth sixth )
                  n=0
                  while IFS=, read -ra cols; do
                  echo "line $((++n))"
                  for i in "$!cols[@]"; do
                  echo "$ordinals[i] col=$cols[i]"
                  done
                  done < file


                  That reads the words in each line into an array named cols, then we interate over the indices of that array so we can correlate the value to the ordinal.



                  For the first 3 lines, we get



                  line 1
                  first col=abc
                  second col=def
                  third col=ghi
                  line 2
                  first col=abc
                  second col=ghi
                  line 3
                  first col=def
                  second col=abc
                  third col=ghi






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 26 at 19:28

























                  answered Nov 26 at 19:22









                  glenn jackman

                  49.6k569106




                  49.6k569106




















                      up vote
                      0
                      down vote













                      Assuming that the input file only has at most three columns, the following uses a while-read loop to read the comma-separated values from standard input and outputs them in a format similar to what you showed:



                      #!/bin/sh

                      while IFS=, read -r first second third
                      do
                      printf 'Line %d:n' "$(( ++n ))"
                      $first:+printf 'First:t%sn' "$first"
                      $second:+printf 'Second:t%sn' "$second"
                      $third:+printf 'Third:t%sn' "$third"
                      done


                      The parameter expansion $variable:+word expands to word if variable is set and non-empty. The code uses this to execute printf for output if the corresponding variable contains data to be printed.



                      Testing on the provided data:



                      $ ./script.sh <file
                      Line 1:
                      First: abc
                      Second: def
                      Third: ghi
                      Line 2:
                      First: abc
                      Second: ghi
                      Line 3:
                      First: def
                      Second: abc
                      Third: ghi
                      Line 4:
                      First: def
                      Second: abc
                      Line 5:
                      First: abc
                      Second: def
                      Line 6:
                      First: abc
                      Second: def
                      Third: ghi





                      share|improve this answer
























                        up vote
                        0
                        down vote













                        Assuming that the input file only has at most three columns, the following uses a while-read loop to read the comma-separated values from standard input and outputs them in a format similar to what you showed:



                        #!/bin/sh

                        while IFS=, read -r first second third
                        do
                        printf 'Line %d:n' "$(( ++n ))"
                        $first:+printf 'First:t%sn' "$first"
                        $second:+printf 'Second:t%sn' "$second"
                        $third:+printf 'Third:t%sn' "$third"
                        done


                        The parameter expansion $variable:+word expands to word if variable is set and non-empty. The code uses this to execute printf for output if the corresponding variable contains data to be printed.



                        Testing on the provided data:



                        $ ./script.sh <file
                        Line 1:
                        First: abc
                        Second: def
                        Third: ghi
                        Line 2:
                        First: abc
                        Second: ghi
                        Line 3:
                        First: def
                        Second: abc
                        Third: ghi
                        Line 4:
                        First: def
                        Second: abc
                        Line 5:
                        First: abc
                        Second: def
                        Line 6:
                        First: abc
                        Second: def
                        Third: ghi





                        share|improve this answer






















                          up vote
                          0
                          down vote










                          up vote
                          0
                          down vote









                          Assuming that the input file only has at most three columns, the following uses a while-read loop to read the comma-separated values from standard input and outputs them in a format similar to what you showed:



                          #!/bin/sh

                          while IFS=, read -r first second third
                          do
                          printf 'Line %d:n' "$(( ++n ))"
                          $first:+printf 'First:t%sn' "$first"
                          $second:+printf 'Second:t%sn' "$second"
                          $third:+printf 'Third:t%sn' "$third"
                          done


                          The parameter expansion $variable:+word expands to word if variable is set and non-empty. The code uses this to execute printf for output if the corresponding variable contains data to be printed.



                          Testing on the provided data:



                          $ ./script.sh <file
                          Line 1:
                          First: abc
                          Second: def
                          Third: ghi
                          Line 2:
                          First: abc
                          Second: ghi
                          Line 3:
                          First: def
                          Second: abc
                          Third: ghi
                          Line 4:
                          First: def
                          Second: abc
                          Line 5:
                          First: abc
                          Second: def
                          Line 6:
                          First: abc
                          Second: def
                          Third: ghi





                          share|improve this answer












                          Assuming that the input file only has at most three columns, the following uses a while-read loop to read the comma-separated values from standard input and outputs them in a format similar to what you showed:



                          #!/bin/sh

                          while IFS=, read -r first second third
                          do
                          printf 'Line %d:n' "$(( ++n ))"
                          $first:+printf 'First:t%sn' "$first"
                          $second:+printf 'Second:t%sn' "$second"
                          $third:+printf 'Third:t%sn' "$third"
                          done


                          The parameter expansion $variable:+word expands to word if variable is set and non-empty. The code uses this to execute printf for output if the corresponding variable contains data to be printed.



                          Testing on the provided data:



                          $ ./script.sh <file
                          Line 1:
                          First: abc
                          Second: def
                          Third: ghi
                          Line 2:
                          First: abc
                          Second: ghi
                          Line 3:
                          First: def
                          Second: abc
                          Third: ghi
                          Line 4:
                          First: def
                          Second: abc
                          Line 5:
                          First: abc
                          Second: def
                          Line 6:
                          First: abc
                          Second: def
                          Third: ghi






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 26 at 19:24









                          Kusalananda

                          118k16223364




                          118k16223364



























                              draft saved

                              draft discarded
















































                              Thanks for contributing an answer to Unix & Linux Stack Exchange!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid


                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.

                              To learn more, see our tips on writing great answers.





                              Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                              Please pay close attention to the following guidance:


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid


                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.

                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f484264%2fshell-print-values-in-a-comma-separated-string%23new-answer', 'question_page');

                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown






                              Popular posts from this blog

                              Peggy Mitchell

                              Palaiologos

                              The Forum (Inglewood, California)