Transpose a single column of a CSV file











up vote
0
down vote

favorite












I have a CSV file with two columns namely
column 1: file name
column 2: access status



The following are some sample recotds



FileA, CREATE
FileA, MODIFY
FileA, DELETE
FileB, CREATE
FileB, MODIFY


I need to TRANSPOSE values of the second column into a single row based on the distinct values of the first column.



FileA, CREATE|MODIFY|DELETE
FileB, CREATE|MODIFY









share|improve this question
























  • Do you need to keep the order? I mean, is FileA, CREATE|MODIFY|DELETE the same as FileA, DELETE|CREATE|MODIFY?
    – terdon
    2 days ago










  • stackoverflow.com/questions/32940758/…
    – Emilio Galarraga
    2 days ago










  • No, ordering based on second column is required
    – Kumarjit Ghosh
    yesterday















up vote
0
down vote

favorite












I have a CSV file with two columns namely
column 1: file name
column 2: access status



The following are some sample recotds



FileA, CREATE
FileA, MODIFY
FileA, DELETE
FileB, CREATE
FileB, MODIFY


I need to TRANSPOSE values of the second column into a single row based on the distinct values of the first column.



FileA, CREATE|MODIFY|DELETE
FileB, CREATE|MODIFY









share|improve this question
























  • Do you need to keep the order? I mean, is FileA, CREATE|MODIFY|DELETE the same as FileA, DELETE|CREATE|MODIFY?
    – terdon
    2 days ago










  • stackoverflow.com/questions/32940758/…
    – Emilio Galarraga
    2 days ago










  • No, ordering based on second column is required
    – Kumarjit Ghosh
    yesterday













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have a CSV file with two columns namely
column 1: file name
column 2: access status



The following are some sample recotds



FileA, CREATE
FileA, MODIFY
FileA, DELETE
FileB, CREATE
FileB, MODIFY


I need to TRANSPOSE values of the second column into a single row based on the distinct values of the first column.



FileA, CREATE|MODIFY|DELETE
FileB, CREATE|MODIFY









share|improve this question















I have a CSV file with two columns namely
column 1: file name
column 2: access status



The following are some sample recotds



FileA, CREATE
FileA, MODIFY
FileA, DELETE
FileB, CREATE
FileB, MODIFY


I need to TRANSPOSE values of the second column into a single row based on the distinct values of the first column.



FileA, CREATE|MODIFY|DELETE
FileB, CREATE|MODIFY






shell-script shell






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 2 days ago









Rui F Ribeiro

38.5k1479128




38.5k1479128










asked 2 days ago









Kumarjit Ghosh

102




102












  • Do you need to keep the order? I mean, is FileA, CREATE|MODIFY|DELETE the same as FileA, DELETE|CREATE|MODIFY?
    – terdon
    2 days ago










  • stackoverflow.com/questions/32940758/…
    – Emilio Galarraga
    2 days ago










  • No, ordering based on second column is required
    – Kumarjit Ghosh
    yesterday


















  • Do you need to keep the order? I mean, is FileA, CREATE|MODIFY|DELETE the same as FileA, DELETE|CREATE|MODIFY?
    – terdon
    2 days ago










  • stackoverflow.com/questions/32940758/…
    – Emilio Galarraga
    2 days ago










  • No, ordering based on second column is required
    – Kumarjit Ghosh
    yesterday
















Do you need to keep the order? I mean, is FileA, CREATE|MODIFY|DELETE the same as FileA, DELETE|CREATE|MODIFY?
– terdon
2 days ago




Do you need to keep the order? I mean, is FileA, CREATE|MODIFY|DELETE the same as FileA, DELETE|CREATE|MODIFY?
– terdon
2 days ago












stackoverflow.com/questions/32940758/…
– Emilio Galarraga
2 days ago




stackoverflow.com/questions/32940758/…
– Emilio Galarraga
2 days ago












No, ordering based on second column is required
– Kumarjit Ghosh
yesterday




No, ordering based on second column is required
– Kumarjit Ghosh
yesterday










5 Answers
5






active

oldest

votes

















up vote
1
down vote













If you don't care about the order the commands are in, you can use:



$ awk -F"[, ]" '{
a[$1][$2]++
}
END{
for(i in a){
printf "%s,",i;
for(k in a[i]){
printf "%s|", k
}
print ""
}
}' file | sed 's/|$//'
FileA, DELETE|CREATE|MODIFY
FileB, CREATE|MODIFY


If you need the order, you can apply some perl magic:



$ sed 's/ //' file | 
perl -F, -lne 'push @{$k{$F[0]}},$F[1]; }{
print "$_, ",join "|", @{$k{$_}} for keys(%k);'
FileB, CREATE|MODIFY
FileA, CREATE|MODIFY|DELETE





share|improve this answer




























    up vote
    1
    down vote













    awk '1 {if (a[$1]) {a[$1] = a[$1]" "$2"|"} else {a[$1] = $2"|"}} END {for (i in a) { print i,a[i]}}' file |sed 's/.$//'





    share|improve this answer




























      up vote
      1
      down vote













      Try also



      awk '
      $1 != LAST {printf "%s%s ", LD, $1 # print every new COL1 value
      LAST = $1 # and remeber it
      LD = RS # set the line delimiter (empty at program start)
      FD = "" # unset field delimiter
      }
      {printf "%s%s", FD, $2 # print successive second fields, after field delim
      FD = "|" # set the field delimiter
      }
      END {printf RS # last action: new line
      }
      ' file
      FileA, CREATE|MODIFY|DELETE
      FileB, CREATE|MODIFY





      share|improve this answer




























        up vote
        1
        down vote













        And a fourth one :D



        awk '1 {if (a[$1]) {a[$1] = a[$1]$2"|"} else {a[$1] = $2"|"}} END {for (i in a) { print i,gensub( /|$/,"","1",a[i])}}' kumarjit
        FileA, CREATE|MODIFY|DELETE
        FileB, CREATE|MODIFY





        share|improve this answer





















        • What does that first 1 do? I'd expect it to print the unmodified line.
          – terdon
          2 days ago


















        up vote
        1
        down vote













        To output in sorted order, with GNU awk



        gawk -F', ' '
        { a[$1] = a[$1] "|" $2 }
        END {
        PROCINFO["sorted_in"] = "@ind_str_asc"
        for (b in a) print b ", " substr(a[b], 2)
        }
        '


        To output in the original order of the keys:



        awk -F', ' '
        !($1 in a) { keys[++count] = $1 }
        { a[$1] = a[$1] "|" $2 }
        END {
        for (i = 1; i <= count; i++)
        print keys[i] ", " substr(a[keys[i]], 2)
        }
        '





        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%2f487150%2ftranspose-a-single-column-of-a-csv-file%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          5 Answers
          5






          active

          oldest

          votes








          5 Answers
          5






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          1
          down vote













          If you don't care about the order the commands are in, you can use:



          $ awk -F"[, ]" '{
          a[$1][$2]++
          }
          END{
          for(i in a){
          printf "%s,",i;
          for(k in a[i]){
          printf "%s|", k
          }
          print ""
          }
          }' file | sed 's/|$//'
          FileA, DELETE|CREATE|MODIFY
          FileB, CREATE|MODIFY


          If you need the order, you can apply some perl magic:



          $ sed 's/ //' file | 
          perl -F, -lne 'push @{$k{$F[0]}},$F[1]; }{
          print "$_, ",join "|", @{$k{$_}} for keys(%k);'
          FileB, CREATE|MODIFY
          FileA, CREATE|MODIFY|DELETE





          share|improve this answer

























            up vote
            1
            down vote













            If you don't care about the order the commands are in, you can use:



            $ awk -F"[, ]" '{
            a[$1][$2]++
            }
            END{
            for(i in a){
            printf "%s,",i;
            for(k in a[i]){
            printf "%s|", k
            }
            print ""
            }
            }' file | sed 's/|$//'
            FileA, DELETE|CREATE|MODIFY
            FileB, CREATE|MODIFY


            If you need the order, you can apply some perl magic:



            $ sed 's/ //' file | 
            perl -F, -lne 'push @{$k{$F[0]}},$F[1]; }{
            print "$_, ",join "|", @{$k{$_}} for keys(%k);'
            FileB, CREATE|MODIFY
            FileA, CREATE|MODIFY|DELETE





            share|improve this answer























              up vote
              1
              down vote










              up vote
              1
              down vote









              If you don't care about the order the commands are in, you can use:



              $ awk -F"[, ]" '{
              a[$1][$2]++
              }
              END{
              for(i in a){
              printf "%s,",i;
              for(k in a[i]){
              printf "%s|", k
              }
              print ""
              }
              }' file | sed 's/|$//'
              FileA, DELETE|CREATE|MODIFY
              FileB, CREATE|MODIFY


              If you need the order, you can apply some perl magic:



              $ sed 's/ //' file | 
              perl -F, -lne 'push @{$k{$F[0]}},$F[1]; }{
              print "$_, ",join "|", @{$k{$_}} for keys(%k);'
              FileB, CREATE|MODIFY
              FileA, CREATE|MODIFY|DELETE





              share|improve this answer












              If you don't care about the order the commands are in, you can use:



              $ awk -F"[, ]" '{
              a[$1][$2]++
              }
              END{
              for(i in a){
              printf "%s,",i;
              for(k in a[i]){
              printf "%s|", k
              }
              print ""
              }
              }' file | sed 's/|$//'
              FileA, DELETE|CREATE|MODIFY
              FileB, CREATE|MODIFY


              If you need the order, you can apply some perl magic:



              $ sed 's/ //' file | 
              perl -F, -lne 'push @{$k{$F[0]}},$F[1]; }{
              print "$_, ",join "|", @{$k{$_}} for keys(%k);'
              FileB, CREATE|MODIFY
              FileA, CREATE|MODIFY|DELETE






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered 2 days ago









              terdon

              127k31245422




              127k31245422
























                  up vote
                  1
                  down vote













                  awk '1 {if (a[$1]) {a[$1] = a[$1]" "$2"|"} else {a[$1] = $2"|"}} END {for (i in a) { print i,a[i]}}' file |sed 's/.$//'





                  share|improve this answer

























                    up vote
                    1
                    down vote













                    awk '1 {if (a[$1]) {a[$1] = a[$1]" "$2"|"} else {a[$1] = $2"|"}} END {for (i in a) { print i,a[i]}}' file |sed 's/.$//'





                    share|improve this answer























                      up vote
                      1
                      down vote










                      up vote
                      1
                      down vote









                      awk '1 {if (a[$1]) {a[$1] = a[$1]" "$2"|"} else {a[$1] = $2"|"}} END {for (i in a) { print i,a[i]}}' file |sed 's/.$//'





                      share|improve this answer












                      awk '1 {if (a[$1]) {a[$1] = a[$1]" "$2"|"} else {a[$1] = $2"|"}} END {for (i in a) { print i,a[i]}}' file |sed 's/.$//'






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered 2 days ago









                      Emilio Galarraga

                      34829




                      34829






















                          up vote
                          1
                          down vote













                          Try also



                          awk '
                          $1 != LAST {printf "%s%s ", LD, $1 # print every new COL1 value
                          LAST = $1 # and remeber it
                          LD = RS # set the line delimiter (empty at program start)
                          FD = "" # unset field delimiter
                          }
                          {printf "%s%s", FD, $2 # print successive second fields, after field delim
                          FD = "|" # set the field delimiter
                          }
                          END {printf RS # last action: new line
                          }
                          ' file
                          FileA, CREATE|MODIFY|DELETE
                          FileB, CREATE|MODIFY





                          share|improve this answer

























                            up vote
                            1
                            down vote













                            Try also



                            awk '
                            $1 != LAST {printf "%s%s ", LD, $1 # print every new COL1 value
                            LAST = $1 # and remeber it
                            LD = RS # set the line delimiter (empty at program start)
                            FD = "" # unset field delimiter
                            }
                            {printf "%s%s", FD, $2 # print successive second fields, after field delim
                            FD = "|" # set the field delimiter
                            }
                            END {printf RS # last action: new line
                            }
                            ' file
                            FileA, CREATE|MODIFY|DELETE
                            FileB, CREATE|MODIFY





                            share|improve this answer























                              up vote
                              1
                              down vote










                              up vote
                              1
                              down vote









                              Try also



                              awk '
                              $1 != LAST {printf "%s%s ", LD, $1 # print every new COL1 value
                              LAST = $1 # and remeber it
                              LD = RS # set the line delimiter (empty at program start)
                              FD = "" # unset field delimiter
                              }
                              {printf "%s%s", FD, $2 # print successive second fields, after field delim
                              FD = "|" # set the field delimiter
                              }
                              END {printf RS # last action: new line
                              }
                              ' file
                              FileA, CREATE|MODIFY|DELETE
                              FileB, CREATE|MODIFY





                              share|improve this answer












                              Try also



                              awk '
                              $1 != LAST {printf "%s%s ", LD, $1 # print every new COL1 value
                              LAST = $1 # and remeber it
                              LD = RS # set the line delimiter (empty at program start)
                              FD = "" # unset field delimiter
                              }
                              {printf "%s%s", FD, $2 # print successive second fields, after field delim
                              FD = "|" # set the field delimiter
                              }
                              END {printf RS # last action: new line
                              }
                              ' file
                              FileA, CREATE|MODIFY|DELETE
                              FileB, CREATE|MODIFY






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered 2 days ago









                              RudiC

                              3,8381312




                              3,8381312






















                                  up vote
                                  1
                                  down vote













                                  And a fourth one :D



                                  awk '1 {if (a[$1]) {a[$1] = a[$1]$2"|"} else {a[$1] = $2"|"}} END {for (i in a) { print i,gensub( /|$/,"","1",a[i])}}' kumarjit
                                  FileA, CREATE|MODIFY|DELETE
                                  FileB, CREATE|MODIFY





                                  share|improve this answer





















                                  • What does that first 1 do? I'd expect it to print the unmodified line.
                                    – terdon
                                    2 days ago















                                  up vote
                                  1
                                  down vote













                                  And a fourth one :D



                                  awk '1 {if (a[$1]) {a[$1] = a[$1]$2"|"} else {a[$1] = $2"|"}} END {for (i in a) { print i,gensub( /|$/,"","1",a[i])}}' kumarjit
                                  FileA, CREATE|MODIFY|DELETE
                                  FileB, CREATE|MODIFY





                                  share|improve this answer





















                                  • What does that first 1 do? I'd expect it to print the unmodified line.
                                    – terdon
                                    2 days ago













                                  up vote
                                  1
                                  down vote










                                  up vote
                                  1
                                  down vote









                                  And a fourth one :D



                                  awk '1 {if (a[$1]) {a[$1] = a[$1]$2"|"} else {a[$1] = $2"|"}} END {for (i in a) { print i,gensub( /|$/,"","1",a[i])}}' kumarjit
                                  FileA, CREATE|MODIFY|DELETE
                                  FileB, CREATE|MODIFY





                                  share|improve this answer












                                  And a fourth one :D



                                  awk '1 {if (a[$1]) {a[$1] = a[$1]$2"|"} else {a[$1] = $2"|"}} END {for (i in a) { print i,gensub( /|$/,"","1",a[i])}}' kumarjit
                                  FileA, CREATE|MODIFY|DELETE
                                  FileB, CREATE|MODIFY






                                  share|improve this answer












                                  share|improve this answer



                                  share|improve this answer










                                  answered 2 days ago









                                  tink

                                  4,08411218




                                  4,08411218












                                  • What does that first 1 do? I'd expect it to print the unmodified line.
                                    – terdon
                                    2 days ago


















                                  • What does that first 1 do? I'd expect it to print the unmodified line.
                                    – terdon
                                    2 days ago
















                                  What does that first 1 do? I'd expect it to print the unmodified line.
                                  – terdon
                                  2 days ago




                                  What does that first 1 do? I'd expect it to print the unmodified line.
                                  – terdon
                                  2 days ago










                                  up vote
                                  1
                                  down vote













                                  To output in sorted order, with GNU awk



                                  gawk -F', ' '
                                  { a[$1] = a[$1] "|" $2 }
                                  END {
                                  PROCINFO["sorted_in"] = "@ind_str_asc"
                                  for (b in a) print b ", " substr(a[b], 2)
                                  }
                                  '


                                  To output in the original order of the keys:



                                  awk -F', ' '
                                  !($1 in a) { keys[++count] = $1 }
                                  { a[$1] = a[$1] "|" $2 }
                                  END {
                                  for (i = 1; i <= count; i++)
                                  print keys[i] ", " substr(a[keys[i]], 2)
                                  }
                                  '





                                  share|improve this answer

























                                    up vote
                                    1
                                    down vote













                                    To output in sorted order, with GNU awk



                                    gawk -F', ' '
                                    { a[$1] = a[$1] "|" $2 }
                                    END {
                                    PROCINFO["sorted_in"] = "@ind_str_asc"
                                    for (b in a) print b ", " substr(a[b], 2)
                                    }
                                    '


                                    To output in the original order of the keys:



                                    awk -F', ' '
                                    !($1 in a) { keys[++count] = $1 }
                                    { a[$1] = a[$1] "|" $2 }
                                    END {
                                    for (i = 1; i <= count; i++)
                                    print keys[i] ", " substr(a[keys[i]], 2)
                                    }
                                    '





                                    share|improve this answer























                                      up vote
                                      1
                                      down vote










                                      up vote
                                      1
                                      down vote









                                      To output in sorted order, with GNU awk



                                      gawk -F', ' '
                                      { a[$1] = a[$1] "|" $2 }
                                      END {
                                      PROCINFO["sorted_in"] = "@ind_str_asc"
                                      for (b in a) print b ", " substr(a[b], 2)
                                      }
                                      '


                                      To output in the original order of the keys:



                                      awk -F', ' '
                                      !($1 in a) { keys[++count] = $1 }
                                      { a[$1] = a[$1] "|" $2 }
                                      END {
                                      for (i = 1; i <= count; i++)
                                      print keys[i] ", " substr(a[keys[i]], 2)
                                      }
                                      '





                                      share|improve this answer












                                      To output in sorted order, with GNU awk



                                      gawk -F', ' '
                                      { a[$1] = a[$1] "|" $2 }
                                      END {
                                      PROCINFO["sorted_in"] = "@ind_str_asc"
                                      for (b in a) print b ", " substr(a[b], 2)
                                      }
                                      '


                                      To output in the original order of the keys:



                                      awk -F', ' '
                                      !($1 in a) { keys[++count] = $1 }
                                      { a[$1] = a[$1] "|" $2 }
                                      END {
                                      for (i = 1; i <= count; i++)
                                      print keys[i] ", " substr(a[keys[i]], 2)
                                      }
                                      '






                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered 2 days ago









                                      glenn jackman

                                      50k569106




                                      50k569106






























                                          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%2f487150%2ftranspose-a-single-column-of-a-csv-file%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

                                          Entries order in /etc/network/interfaces

                                          新発田市

                                          Grub takes very long (several minutes) to open Menu (in Multi-Boot-System)