Add Columns to .csv from Multiple Filescsv file with multiple columns to zenityPick columns from a variable length csv fileMerging contents of multiple .csv files into single .csv fileHow to swap certain columns in .csv fileRemove specific columns from csv using awkTransform existing .CSV by comparing previous row with current and minor calculationsMatching columns of different csv files, not working when column value is different lengthUsing bash to swap first and second columns in CSVSwitch columns in CSV using awk?Connecting files like columns

On studying Computer Science vs. Software Engineering to become a proficient coder

Ito`s Lemma problem

What are the components of a legend (in the sense of a tale, not a figure legend)?

Labeling matrices/rectangles and drawing Sigma inside rectangle

If current results hold, Man City would win PL title

Why are solar panels kept tilted?

correct spelling of "carruffel" (fuzz, hustle, all that jazz)

Automatically anti-predictably assemble an alliterative aria

Is there any good reason to write "it is easy to see"?

When a land becomes a creature, is it untapped?

Is there anything special about -1 (0xFFFFFFFF) regarding ADC?

What are the implications of the new alleged key recovery attack preprint on SIMON?

Why is it harder to turn a motor/generator with shorted terminals?

Jumping frame contents with beamer and pgfplots

How do I tell my supervisor that he is choosing poor replacements for me while I am on maternity leave?

Longest Text in Latin

Why is a set not a partition of itself?

As programers say: Strive to be lazy

What's tha name for when you write multiple voices on same staff? And are there any cons?

Smallest Guaranteed hash collision cycle length

Is 12 minutes connection in Bristol Temple Meads long enough?

declared variable inside void setup is forgotten in void loop

Does Lawful Interception of 4G / the proposed 5G provide a back door for hackers as well?

Would an 8% reduction in drag outweigh the weight addition from this custom CFD-tested winglet?



Add Columns to .csv from Multiple Files


csv file with multiple columns to zenityPick columns from a variable length csv fileMerging contents of multiple .csv files into single .csv fileHow to swap certain columns in .csv fileRemove specific columns from csv using awkTransform existing .CSV by comparing previous row with current and minor calculationsMatching columns of different csv files, not working when column value is different lengthUsing bash to swap first and second columns in CSVSwitch columns in CSV using awk?Connecting files like columns






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








4















I'm trying to add columns to a csv file from multiple files. This is what I have tried so far, but keep ending up with either an empty file or just the column from the last file.



 while read i; do
awk 'print $4' $i.txt > $i_temp
awk 'NR==FNRa[NR]=$0;nextprint a[FNR],$0' OFS=, test.csv $i_temp >> test.csv
done < file_list


The file list is just a list of accession numbers:



NA123
NA124
NA125
...


The text files that they correspond to have 4 columns in them, and I want to copy the last column and add it as the next column in a csv file. The contents of the files look like this:



NA123.txt:
April-18 10:00 18:00 8
April-19 09:00 19:00 10
April-20 11:00 16:00 5
...
NA124.txt:
April-18 14:00 18:00 4
April-19 09:00 15:00 6
April-20 07:00 16:00 9
...
NA125.txt:
April-18 10:00 22:00 12
April-19 09:00 12:00 3
April-20 06:00 16:00 10
...


test.csv:
0,
1,
2,
...


I would like the output to be:



0,8,4,12
1,10,6,3
2,5,9,10
...


What do I need to change about this or is there a more efficient way to do this?










share|improve this question









New contributor



thebriterican is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

























    4















    I'm trying to add columns to a csv file from multiple files. This is what I have tried so far, but keep ending up with either an empty file or just the column from the last file.



     while read i; do
    awk 'print $4' $i.txt > $i_temp
    awk 'NR==FNRa[NR]=$0;nextprint a[FNR],$0' OFS=, test.csv $i_temp >> test.csv
    done < file_list


    The file list is just a list of accession numbers:



    NA123
    NA124
    NA125
    ...


    The text files that they correspond to have 4 columns in them, and I want to copy the last column and add it as the next column in a csv file. The contents of the files look like this:



    NA123.txt:
    April-18 10:00 18:00 8
    April-19 09:00 19:00 10
    April-20 11:00 16:00 5
    ...
    NA124.txt:
    April-18 14:00 18:00 4
    April-19 09:00 15:00 6
    April-20 07:00 16:00 9
    ...
    NA125.txt:
    April-18 10:00 22:00 12
    April-19 09:00 12:00 3
    April-20 06:00 16:00 10
    ...


    test.csv:
    0,
    1,
    2,
    ...


    I would like the output to be:



    0,8,4,12
    1,10,6,3
    2,5,9,10
    ...


    What do I need to change about this or is there a more efficient way to do this?










    share|improve this question









    New contributor



    thebriterican is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.





















      4












      4








      4








      I'm trying to add columns to a csv file from multiple files. This is what I have tried so far, but keep ending up with either an empty file or just the column from the last file.



       while read i; do
      awk 'print $4' $i.txt > $i_temp
      awk 'NR==FNRa[NR]=$0;nextprint a[FNR],$0' OFS=, test.csv $i_temp >> test.csv
      done < file_list


      The file list is just a list of accession numbers:



      NA123
      NA124
      NA125
      ...


      The text files that they correspond to have 4 columns in them, and I want to copy the last column and add it as the next column in a csv file. The contents of the files look like this:



      NA123.txt:
      April-18 10:00 18:00 8
      April-19 09:00 19:00 10
      April-20 11:00 16:00 5
      ...
      NA124.txt:
      April-18 14:00 18:00 4
      April-19 09:00 15:00 6
      April-20 07:00 16:00 9
      ...
      NA125.txt:
      April-18 10:00 22:00 12
      April-19 09:00 12:00 3
      April-20 06:00 16:00 10
      ...


      test.csv:
      0,
      1,
      2,
      ...


      I would like the output to be:



      0,8,4,12
      1,10,6,3
      2,5,9,10
      ...


      What do I need to change about this or is there a more efficient way to do this?










      share|improve this question









      New contributor



      thebriterican is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      I'm trying to add columns to a csv file from multiple files. This is what I have tried so far, but keep ending up with either an empty file or just the column from the last file.



       while read i; do
      awk 'print $4' $i.txt > $i_temp
      awk 'NR==FNRa[NR]=$0;nextprint a[FNR],$0' OFS=, test.csv $i_temp >> test.csv
      done < file_list


      The file list is just a list of accession numbers:



      NA123
      NA124
      NA125
      ...


      The text files that they correspond to have 4 columns in them, and I want to copy the last column and add it as the next column in a csv file. The contents of the files look like this:



      NA123.txt:
      April-18 10:00 18:00 8
      April-19 09:00 19:00 10
      April-20 11:00 16:00 5
      ...
      NA124.txt:
      April-18 14:00 18:00 4
      April-19 09:00 15:00 6
      April-20 07:00 16:00 9
      ...
      NA125.txt:
      April-18 10:00 22:00 12
      April-19 09:00 12:00 3
      April-20 06:00 16:00 10
      ...


      test.csv:
      0,
      1,
      2,
      ...


      I would like the output to be:



      0,8,4,12
      1,10,6,3
      2,5,9,10
      ...


      What do I need to change about this or is there a more efficient way to do this?







      text-processing awk csv






      share|improve this question









      New contributor



      thebriterican is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.










      share|improve this question









      New contributor



      thebriterican is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.








      share|improve this question




      share|improve this question








      edited May 8 at 17:48







      thebriterican













      New contributor



      thebriterican is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.








      asked May 8 at 17:11









      thebritericanthebriterican

      234




      234




      New contributor



      thebriterican is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.




      New contributor




      thebriterican is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















          3 Answers
          3






          active

          oldest

          votes


















          1














          Maybe you could try to use paste and one additional temp file



          touch temp

          while read i; do
          awk 'print $4' $i.txt > $i_temp
          paste temp $i_temp > test.csv
          cp test.csv temp
          done < file_list

          rm temp





          share|improve this answer










          New contributor



          user352003 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.


























            2














            try:



             <file_list xargs -I % awk '
            system("sed -i ''' " NR "s/,\?$/," $4 "/''' test.csv" ) ' %.txt


            the xargs is reading filename from file_list file and feed to awk thought xarags's variable called % with .txt suffix as filename contains.



            with sed '#s/$/something/' test.csv" command that used to append something at the end $ of a specific line number # in a file like test.csv; so at above, $4 is the string that need to appended to specific line number taking from NR; ,?$ is saying might your line ends with comma , which I used because you have it in your test.csv. this sed command is calling by awk's system() function.



            note that sed is writing in-place any changes, so you will not notice terminal ouput, do:



            $ cat test.csv
            0,8,4,12
            1,10,6,3
            2,5,9,10





            share|improve this answer






























              1














              Why not just use paste?



              $ cat in1; cat in2
              row1,col2,col3
              row2,col2,col3
              row3,col2,col3
              row4,col2,col3
              row1
              row2
              row3
              row4
              $ paste -d, in1 in2
              row1,col2,col3,row1
              row2,col2,col3,row2
              row3,col2,col3,row3
              row4,col2,col3,row4





              share|improve this answer























              • OP needs merge column 4 from all files where their partial name is in file_list with test.csv file

                – αғsнιη
                May 9 at 1:42











              • That was not clear in the original presentation of the question which I addressed with this answer.

                – DopeGhoti
                May 9 at 15:40











              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',
              autoActivateHeartbeat: false,
              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
              );



              );






              thebriterican is a new contributor. Be nice, and check out our Code of Conduct.









              draft saved

              draft discarded


















              StackExchange.ready(
              function ()
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f517836%2fadd-columns-to-csv-from-multiple-files%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









              1














              Maybe you could try to use paste and one additional temp file



              touch temp

              while read i; do
              awk 'print $4' $i.txt > $i_temp
              paste temp $i_temp > test.csv
              cp test.csv temp
              done < file_list

              rm temp





              share|improve this answer










              New contributor



              user352003 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.























                1














                Maybe you could try to use paste and one additional temp file



                touch temp

                while read i; do
                awk 'print $4' $i.txt > $i_temp
                paste temp $i_temp > test.csv
                cp test.csv temp
                done < file_list

                rm temp





                share|improve this answer










                New contributor



                user352003 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.





















                  1












                  1








                  1







                  Maybe you could try to use paste and one additional temp file



                  touch temp

                  while read i; do
                  awk 'print $4' $i.txt > $i_temp
                  paste temp $i_temp > test.csv
                  cp test.csv temp
                  done < file_list

                  rm temp





                  share|improve this answer










                  New contributor



                  user352003 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.









                  Maybe you could try to use paste and one additional temp file



                  touch temp

                  while read i; do
                  awk 'print $4' $i.txt > $i_temp
                  paste temp $i_temp > test.csv
                  cp test.csv temp
                  done < file_list

                  rm temp






                  share|improve this answer










                  New contributor



                  user352003 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.








                  share|improve this answer



                  share|improve this answer








                  edited May 8 at 19:07









                  αғsнιη

                  18.1k113271




                  18.1k113271






                  New contributor



                  user352003 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.








                  answered May 8 at 18:41









                  user352003user352003

                  261




                  261




                  New contributor



                  user352003 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.




                  New contributor




                  user352003 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.

























                      2














                      try:



                       <file_list xargs -I % awk '
                      system("sed -i ''' " NR "s/,\?$/," $4 "/''' test.csv" ) ' %.txt


                      the xargs is reading filename from file_list file and feed to awk thought xarags's variable called % with .txt suffix as filename contains.



                      with sed '#s/$/something/' test.csv" command that used to append something at the end $ of a specific line number # in a file like test.csv; so at above, $4 is the string that need to appended to specific line number taking from NR; ,?$ is saying might your line ends with comma , which I used because you have it in your test.csv. this sed command is calling by awk's system() function.



                      note that sed is writing in-place any changes, so you will not notice terminal ouput, do:



                      $ cat test.csv
                      0,8,4,12
                      1,10,6,3
                      2,5,9,10





                      share|improve this answer



























                        2














                        try:



                         <file_list xargs -I % awk '
                        system("sed -i ''' " NR "s/,\?$/," $4 "/''' test.csv" ) ' %.txt


                        the xargs is reading filename from file_list file and feed to awk thought xarags's variable called % with .txt suffix as filename contains.



                        with sed '#s/$/something/' test.csv" command that used to append something at the end $ of a specific line number # in a file like test.csv; so at above, $4 is the string that need to appended to specific line number taking from NR; ,?$ is saying might your line ends with comma , which I used because you have it in your test.csv. this sed command is calling by awk's system() function.



                        note that sed is writing in-place any changes, so you will not notice terminal ouput, do:



                        $ cat test.csv
                        0,8,4,12
                        1,10,6,3
                        2,5,9,10





                        share|improve this answer

























                          2












                          2








                          2







                          try:



                           <file_list xargs -I % awk '
                          system("sed -i ''' " NR "s/,\?$/," $4 "/''' test.csv" ) ' %.txt


                          the xargs is reading filename from file_list file and feed to awk thought xarags's variable called % with .txt suffix as filename contains.



                          with sed '#s/$/something/' test.csv" command that used to append something at the end $ of a specific line number # in a file like test.csv; so at above, $4 is the string that need to appended to specific line number taking from NR; ,?$ is saying might your line ends with comma , which I used because you have it in your test.csv. this sed command is calling by awk's system() function.



                          note that sed is writing in-place any changes, so you will not notice terminal ouput, do:



                          $ cat test.csv
                          0,8,4,12
                          1,10,6,3
                          2,5,9,10





                          share|improve this answer













                          try:



                           <file_list xargs -I % awk '
                          system("sed -i ''' " NR "s/,\?$/," $4 "/''' test.csv" ) ' %.txt


                          the xargs is reading filename from file_list file and feed to awk thought xarags's variable called % with .txt suffix as filename contains.



                          with sed '#s/$/something/' test.csv" command that used to append something at the end $ of a specific line number # in a file like test.csv; so at above, $4 is the string that need to appended to specific line number taking from NR; ,?$ is saying might your line ends with comma , which I used because you have it in your test.csv. this sed command is calling by awk's system() function.



                          note that sed is writing in-place any changes, so you will not notice terminal ouput, do:



                          $ cat test.csv
                          0,8,4,12
                          1,10,6,3
                          2,5,9,10






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered May 8 at 18:53









                          αғsнιηαғsнιη

                          18.1k113271




                          18.1k113271





















                              1














                              Why not just use paste?



                              $ cat in1; cat in2
                              row1,col2,col3
                              row2,col2,col3
                              row3,col2,col3
                              row4,col2,col3
                              row1
                              row2
                              row3
                              row4
                              $ paste -d, in1 in2
                              row1,col2,col3,row1
                              row2,col2,col3,row2
                              row3,col2,col3,row3
                              row4,col2,col3,row4





                              share|improve this answer























                              • OP needs merge column 4 from all files where their partial name is in file_list with test.csv file

                                – αғsнιη
                                May 9 at 1:42











                              • That was not clear in the original presentation of the question which I addressed with this answer.

                                – DopeGhoti
                                May 9 at 15:40















                              1














                              Why not just use paste?



                              $ cat in1; cat in2
                              row1,col2,col3
                              row2,col2,col3
                              row3,col2,col3
                              row4,col2,col3
                              row1
                              row2
                              row3
                              row4
                              $ paste -d, in1 in2
                              row1,col2,col3,row1
                              row2,col2,col3,row2
                              row3,col2,col3,row3
                              row4,col2,col3,row4





                              share|improve this answer























                              • OP needs merge column 4 from all files where their partial name is in file_list with test.csv file

                                – αғsнιη
                                May 9 at 1:42











                              • That was not clear in the original presentation of the question which I addressed with this answer.

                                – DopeGhoti
                                May 9 at 15:40













                              1












                              1








                              1







                              Why not just use paste?



                              $ cat in1; cat in2
                              row1,col2,col3
                              row2,col2,col3
                              row3,col2,col3
                              row4,col2,col3
                              row1
                              row2
                              row3
                              row4
                              $ paste -d, in1 in2
                              row1,col2,col3,row1
                              row2,col2,col3,row2
                              row3,col2,col3,row3
                              row4,col2,col3,row4





                              share|improve this answer













                              Why not just use paste?



                              $ cat in1; cat in2
                              row1,col2,col3
                              row2,col2,col3
                              row3,col2,col3
                              row4,col2,col3
                              row1
                              row2
                              row3
                              row4
                              $ paste -d, in1 in2
                              row1,col2,col3,row1
                              row2,col2,col3,row2
                              row3,col2,col3,row3
                              row4,col2,col3,row4






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered May 8 at 17:20









                              DopeGhotiDopeGhoti

                              47.8k56195




                              47.8k56195












                              • OP needs merge column 4 from all files where their partial name is in file_list with test.csv file

                                – αғsнιη
                                May 9 at 1:42











                              • That was not clear in the original presentation of the question which I addressed with this answer.

                                – DopeGhoti
                                May 9 at 15:40

















                              • OP needs merge column 4 from all files where their partial name is in file_list with test.csv file

                                – αғsнιη
                                May 9 at 1:42











                              • That was not clear in the original presentation of the question which I addressed with this answer.

                                – DopeGhoti
                                May 9 at 15:40
















                              OP needs merge column 4 from all files where their partial name is in file_list with test.csv file

                              – αғsнιη
                              May 9 at 1:42





                              OP needs merge column 4 from all files where their partial name is in file_list with test.csv file

                              – αғsнιη
                              May 9 at 1:42













                              That was not clear in the original presentation of the question which I addressed with this answer.

                              – DopeGhoti
                              May 9 at 15:40





                              That was not clear in the original presentation of the question which I addressed with this answer.

                              – DopeGhoti
                              May 9 at 15:40










                              thebriterican is a new contributor. Be nice, and check out our Code of Conduct.









                              draft saved

                              draft discarded


















                              thebriterican is a new contributor. Be nice, and check out our Code of Conduct.












                              thebriterican is a new contributor. Be nice, and check out our Code of Conduct.











                              thebriterican is a new contributor. Be nice, and check out our Code of Conduct.














                              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.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f517836%2fadd-columns-to-csv-from-multiple-files%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

                              Grendel Contents Story Scholarship Depictions Notes References Navigation menu10.1093/notesj/gjn112Berserkeree

                              Area configuration aggregation error after install Porto themeMagento 2.1 CE Installed but front/backend not loading/workingCSS not loading on page within Magento 2 pageCannot install module in Magento 2no commands defined in the “setup” namespace. in Magento2Magento 2: Static files are present but shows 404Why do i have to always run the commands to clean cache in Magento 2.1.8?Failure reason: 'Unable to unserialize value.'Error 500 after magento migrationIn production mode the site does not loadMagento 2 : Error 500 after installing

                              Middle Expansion Olielle Resaix Definition: Uttering songs of triumph shouting with joy triumphant exulting Sejunction Journal 붙다 달 고급 품목 외출 The stretch trades the screeching tin. Definition: The act of speaking with a drawl a drawl Cough Sand Definition: An uproar a quarrel a noisy outbreak Shake Iron Publicize Horse House Baby 사과 Resaix Flaggy Jelly Temporary Unequaled Puppet A drop in the bucket Shrew 성격 회원 성질 미팅 The burn frames the tacky quality. Materialistic The smoke reduces the way. Yammoe Nondescript Cheek 얼굴 배 약하다 날리다 타다 The illegal country shows the iron. Help Rule Drearien Smoke Teaching Meaty Wasp Abraham Lincoln Jaws 진심 수리하다 Size Cork Idea Convert Think Lark John Lennon 거울 청소 군 추천하다 아이스크림