Copy first line from one file to other files using xargs and sed

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 list of split CSV files. How can I copy the first CSV header to the rest of CSV files?




profiles00.csv
profiles01.csv
profiles02.csv
profiles03.csv
...



This is what I have now but how do I send the output to new files or replace existing files?




find . -name *.csv -print0 | xargs -0 -I -P 100 sed -e '1r ' -e 'q' profiles00.csv







share|improve this question























    up vote
    0
    down vote

    favorite












    I have a list of split CSV files. How can I copy the first CSV header to the rest of CSV files?




    profiles00.csv
    profiles01.csv
    profiles02.csv
    profiles03.csv
    ...



    This is what I have now but how do I send the output to new files or replace existing files?




    find . -name *.csv -print0 | xargs -0 -I -P 100 sed -e '1r ' -e 'q' profiles00.csv







    share|improve this question





















      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I have a list of split CSV files. How can I copy the first CSV header to the rest of CSV files?




      profiles00.csv
      profiles01.csv
      profiles02.csv
      profiles03.csv
      ...



      This is what I have now but how do I send the output to new files or replace existing files?




      find . -name *.csv -print0 | xargs -0 -I -P 100 sed -e '1r ' -e 'q' profiles00.csv







      share|improve this question











      I have a list of split CSV files. How can I copy the first CSV header to the rest of CSV files?




      profiles00.csv
      profiles01.csv
      profiles02.csv
      profiles03.csv
      ...



      This is what I have now but how do I send the output to new files or replace existing files?




      find . -name *.csv -print0 | xargs -0 -I -P 100 sed -e '1r ' -e 'q' profiles00.csv









      share|improve this question










      share|improve this question




      share|improve this question









      asked May 17 at 1:55









      angelokh

      1033




      1033




















          3 Answers
          3






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          Here's one way to handle your particular problem:



          find . -maxdepth 1 -type f -name "profiles*.csv" ! -name "profiles00.csv" -exec sed -i -e '1!b' -e 'R profiles00.csv' -e 'N' +


          Written layed out in a multi-line manner as:



          find . -maxdepth 1 
          -type f
          -name "profiles*.csv" ! -name "profiles00.csv"
          -exec sed -i -e '1!b'
          -e 'R profiles00.csv'
          -e 'N'
          +


          to be read as: find utility shall



          • -maxdepth 1 => walk only in the current directory , normally it recurses down to all the way the lowest hierarchy.


          • -type f => while walking select the regular files only.


          • -name "profiles*.csv" ! -name "profiles00.csv" => out of all the regular files, select only thos who have profiles*.csv as their basenames AND filter out the "profiles00.csv" file since this is the header file.


          • -exec sed -i -e '1!b' -e 'R profiles00.csv' -e 'N' +


          • -i option => edit files in place GNU sed assumed



          • -e option => sed code follows



            • 1!b => don't touch the the non-first lines.


            • R profiles00.csv => read one line (since this is the first time, it'll be the first line) from the profiles00.csv file. So essentially the header is fished out of the profiles00.csv file. But not printed yet.


            • N => Read in the next line into the pattern space. this flushes out the read buffer and after that the actual pattern space is flushed out (lines 1,2) from the current file being operated upon by sed.



          • + will make find supply the multiple files it has selected in one fell swoop to the sed utility and GNU sed is capable of handling multiple files with the -i option. Specifically, the line numbers are reset for a new file read in.


          ≠================ Posix way ======



           LC_ALL=C 
          find . ! -name . -prune -type f
          -name 'profiles??*.csv'
          ! -name 'profiles*[!0-9]*.csv'
          ! -name 'profiles00.csv'
          -exec sh -c '
          shift "$1"
          head -n 1 < profiles00.csv > header
          for arg do
          printf "0r header\nw\nq\n" | ed -s "$arg"
          done
          ' 2 1 +


          Here we dispense with sed and take recourse to ed.




          • 0r => will insert the contents of file mentioned, to the beginning of file being edited by ed.


          • w => will write to the file, thus freezing all the changes done.


          • q => will quit the ed editor.





          share|improve this answer



















          • 1




            This will handle arbitrary input (no need to sanitize the line to be inserted) but is gnu sed specific. +1 nonetheless.
            – don_crissti
            May 17 at 12:16

















          up vote
          1
          down vote













          You don't need xargs



          Command :



          find . -maxdepth 1 ! -name "profiles00.csv" -a -name "*csv" -exec sed -i "1s/^/$(head -1 profiles00.csv)n/" ;


          Here I am using find to select files and sed for in place editing



          Explanation



          find




          • maxdepth 1 - to only select files in the immediate directory and not in sub directory, if you want to edit files in sub directory too, you can remove this option


          • ! -name "profiles00.csv" -a -name "*csv" - to select all csv files but not profiles00.csv , here -a means and and ! means not

          sed




          • -i - edit files in place


          • 1 - line addressing


          • s/^/$(head -1 profiles00.csv)n/ - to replace beginning of file with the given text

          head




          • -1 - to select first line





          share|improve this answer




























            up vote
            0
            down vote













            This is an untested pseudo-code sketch so it may not work, but I'd go about it something like (first backup your files so you don't overwrite them):



            sed -i '1s/^/'`head -n1 profiles00.csv`'/' *.csv


            explained



            For more information on inserting into files:



            https://stackoverflow.com/questions/9533679/how-to-insert-a-text-at-the-beginning-of-a-file






            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: false,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: null,
              bindNavPrevention: true,
              postfix: "",
              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%2f444274%2fcopy-first-line-from-one-file-to-other-files-using-xargs-and-sed%23new-answer', 'question_page');

              );

              Post as a guest






























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              2
              down vote



              accepted










              Here's one way to handle your particular problem:



              find . -maxdepth 1 -type f -name "profiles*.csv" ! -name "profiles00.csv" -exec sed -i -e '1!b' -e 'R profiles00.csv' -e 'N' +


              Written layed out in a multi-line manner as:



              find . -maxdepth 1 
              -type f
              -name "profiles*.csv" ! -name "profiles00.csv"
              -exec sed -i -e '1!b'
              -e 'R profiles00.csv'
              -e 'N'
              +


              to be read as: find utility shall



              • -maxdepth 1 => walk only in the current directory , normally it recurses down to all the way the lowest hierarchy.


              • -type f => while walking select the regular files only.


              • -name "profiles*.csv" ! -name "profiles00.csv" => out of all the regular files, select only thos who have profiles*.csv as their basenames AND filter out the "profiles00.csv" file since this is the header file.


              • -exec sed -i -e '1!b' -e 'R profiles00.csv' -e 'N' +


              • -i option => edit files in place GNU sed assumed



              • -e option => sed code follows



                • 1!b => don't touch the the non-first lines.


                • R profiles00.csv => read one line (since this is the first time, it'll be the first line) from the profiles00.csv file. So essentially the header is fished out of the profiles00.csv file. But not printed yet.


                • N => Read in the next line into the pattern space. this flushes out the read buffer and after that the actual pattern space is flushed out (lines 1,2) from the current file being operated upon by sed.



              • + will make find supply the multiple files it has selected in one fell swoop to the sed utility and GNU sed is capable of handling multiple files with the -i option. Specifically, the line numbers are reset for a new file read in.


              ≠================ Posix way ======



               LC_ALL=C 
              find . ! -name . -prune -type f
              -name 'profiles??*.csv'
              ! -name 'profiles*[!0-9]*.csv'
              ! -name 'profiles00.csv'
              -exec sh -c '
              shift "$1"
              head -n 1 < profiles00.csv > header
              for arg do
              printf "0r header\nw\nq\n" | ed -s "$arg"
              done
              ' 2 1 +


              Here we dispense with sed and take recourse to ed.




              • 0r => will insert the contents of file mentioned, to the beginning of file being edited by ed.


              • w => will write to the file, thus freezing all the changes done.


              • q => will quit the ed editor.





              share|improve this answer



















              • 1




                This will handle arbitrary input (no need to sanitize the line to be inserted) but is gnu sed specific. +1 nonetheless.
                – don_crissti
                May 17 at 12:16














              up vote
              2
              down vote



              accepted










              Here's one way to handle your particular problem:



              find . -maxdepth 1 -type f -name "profiles*.csv" ! -name "profiles00.csv" -exec sed -i -e '1!b' -e 'R profiles00.csv' -e 'N' +


              Written layed out in a multi-line manner as:



              find . -maxdepth 1 
              -type f
              -name "profiles*.csv" ! -name "profiles00.csv"
              -exec sed -i -e '1!b'
              -e 'R profiles00.csv'
              -e 'N'
              +


              to be read as: find utility shall



              • -maxdepth 1 => walk only in the current directory , normally it recurses down to all the way the lowest hierarchy.


              • -type f => while walking select the regular files only.


              • -name "profiles*.csv" ! -name "profiles00.csv" => out of all the regular files, select only thos who have profiles*.csv as their basenames AND filter out the "profiles00.csv" file since this is the header file.


              • -exec sed -i -e '1!b' -e 'R profiles00.csv' -e 'N' +


              • -i option => edit files in place GNU sed assumed



              • -e option => sed code follows



                • 1!b => don't touch the the non-first lines.


                • R profiles00.csv => read one line (since this is the first time, it'll be the first line) from the profiles00.csv file. So essentially the header is fished out of the profiles00.csv file. But not printed yet.


                • N => Read in the next line into the pattern space. this flushes out the read buffer and after that the actual pattern space is flushed out (lines 1,2) from the current file being operated upon by sed.



              • + will make find supply the multiple files it has selected in one fell swoop to the sed utility and GNU sed is capable of handling multiple files with the -i option. Specifically, the line numbers are reset for a new file read in.


              ≠================ Posix way ======



               LC_ALL=C 
              find . ! -name . -prune -type f
              -name 'profiles??*.csv'
              ! -name 'profiles*[!0-9]*.csv'
              ! -name 'profiles00.csv'
              -exec sh -c '
              shift "$1"
              head -n 1 < profiles00.csv > header
              for arg do
              printf "0r header\nw\nq\n" | ed -s "$arg"
              done
              ' 2 1 +


              Here we dispense with sed and take recourse to ed.




              • 0r => will insert the contents of file mentioned, to the beginning of file being edited by ed.


              • w => will write to the file, thus freezing all the changes done.


              • q => will quit the ed editor.





              share|improve this answer



















              • 1




                This will handle arbitrary input (no need to sanitize the line to be inserted) but is gnu sed specific. +1 nonetheless.
                – don_crissti
                May 17 at 12:16












              up vote
              2
              down vote



              accepted







              up vote
              2
              down vote



              accepted






              Here's one way to handle your particular problem:



              find . -maxdepth 1 -type f -name "profiles*.csv" ! -name "profiles00.csv" -exec sed -i -e '1!b' -e 'R profiles00.csv' -e 'N' +


              Written layed out in a multi-line manner as:



              find . -maxdepth 1 
              -type f
              -name "profiles*.csv" ! -name "profiles00.csv"
              -exec sed -i -e '1!b'
              -e 'R profiles00.csv'
              -e 'N'
              +


              to be read as: find utility shall



              • -maxdepth 1 => walk only in the current directory , normally it recurses down to all the way the lowest hierarchy.


              • -type f => while walking select the regular files only.


              • -name "profiles*.csv" ! -name "profiles00.csv" => out of all the regular files, select only thos who have profiles*.csv as their basenames AND filter out the "profiles00.csv" file since this is the header file.


              • -exec sed -i -e '1!b' -e 'R profiles00.csv' -e 'N' +


              • -i option => edit files in place GNU sed assumed



              • -e option => sed code follows



                • 1!b => don't touch the the non-first lines.


                • R profiles00.csv => read one line (since this is the first time, it'll be the first line) from the profiles00.csv file. So essentially the header is fished out of the profiles00.csv file. But not printed yet.


                • N => Read in the next line into the pattern space. this flushes out the read buffer and after that the actual pattern space is flushed out (lines 1,2) from the current file being operated upon by sed.



              • + will make find supply the multiple files it has selected in one fell swoop to the sed utility and GNU sed is capable of handling multiple files with the -i option. Specifically, the line numbers are reset for a new file read in.


              ≠================ Posix way ======



               LC_ALL=C 
              find . ! -name . -prune -type f
              -name 'profiles??*.csv'
              ! -name 'profiles*[!0-9]*.csv'
              ! -name 'profiles00.csv'
              -exec sh -c '
              shift "$1"
              head -n 1 < profiles00.csv > header
              for arg do
              printf "0r header\nw\nq\n" | ed -s "$arg"
              done
              ' 2 1 +


              Here we dispense with sed and take recourse to ed.




              • 0r => will insert the contents of file mentioned, to the beginning of file being edited by ed.


              • w => will write to the file, thus freezing all the changes done.


              • q => will quit the ed editor.





              share|improve this answer















              Here's one way to handle your particular problem:



              find . -maxdepth 1 -type f -name "profiles*.csv" ! -name "profiles00.csv" -exec sed -i -e '1!b' -e 'R profiles00.csv' -e 'N' +


              Written layed out in a multi-line manner as:



              find . -maxdepth 1 
              -type f
              -name "profiles*.csv" ! -name "profiles00.csv"
              -exec sed -i -e '1!b'
              -e 'R profiles00.csv'
              -e 'N'
              +


              to be read as: find utility shall



              • -maxdepth 1 => walk only in the current directory , normally it recurses down to all the way the lowest hierarchy.


              • -type f => while walking select the regular files only.


              • -name "profiles*.csv" ! -name "profiles00.csv" => out of all the regular files, select only thos who have profiles*.csv as their basenames AND filter out the "profiles00.csv" file since this is the header file.


              • -exec sed -i -e '1!b' -e 'R profiles00.csv' -e 'N' +


              • -i option => edit files in place GNU sed assumed



              • -e option => sed code follows



                • 1!b => don't touch the the non-first lines.


                • R profiles00.csv => read one line (since this is the first time, it'll be the first line) from the profiles00.csv file. So essentially the header is fished out of the profiles00.csv file. But not printed yet.


                • N => Read in the next line into the pattern space. this flushes out the read buffer and after that the actual pattern space is flushed out (lines 1,2) from the current file being operated upon by sed.



              • + will make find supply the multiple files it has selected in one fell swoop to the sed utility and GNU sed is capable of handling multiple files with the -i option. Specifically, the line numbers are reset for a new file read in.


              ≠================ Posix way ======



               LC_ALL=C 
              find . ! -name . -prune -type f
              -name 'profiles??*.csv'
              ! -name 'profiles*[!0-9]*.csv'
              ! -name 'profiles00.csv'
              -exec sh -c '
              shift "$1"
              head -n 1 < profiles00.csv > header
              for arg do
              printf "0r header\nw\nq\n" | ed -s "$arg"
              done
              ' 2 1 +


              Here we dispense with sed and take recourse to ed.




              • 0r => will insert the contents of file mentioned, to the beginning of file being edited by ed.


              • w => will write to the file, thus freezing all the changes done.


              • q => will quit the ed editor.






              share|improve this answer















              share|improve this answer



              share|improve this answer








              edited May 18 at 8:27


























              answered May 17 at 11:46









              Rakesh Sharma

              37813




              37813







              • 1




                This will handle arbitrary input (no need to sanitize the line to be inserted) but is gnu sed specific. +1 nonetheless.
                – don_crissti
                May 17 at 12:16












              • 1




                This will handle arbitrary input (no need to sanitize the line to be inserted) but is gnu sed specific. +1 nonetheless.
                – don_crissti
                May 17 at 12:16







              1




              1




              This will handle arbitrary input (no need to sanitize the line to be inserted) but is gnu sed specific. +1 nonetheless.
              – don_crissti
              May 17 at 12:16




              This will handle arbitrary input (no need to sanitize the line to be inserted) but is gnu sed specific. +1 nonetheless.
              – don_crissti
              May 17 at 12:16












              up vote
              1
              down vote













              You don't need xargs



              Command :



              find . -maxdepth 1 ! -name "profiles00.csv" -a -name "*csv" -exec sed -i "1s/^/$(head -1 profiles00.csv)n/" ;


              Here I am using find to select files and sed for in place editing



              Explanation



              find




              • maxdepth 1 - to only select files in the immediate directory and not in sub directory, if you want to edit files in sub directory too, you can remove this option


              • ! -name "profiles00.csv" -a -name "*csv" - to select all csv files but not profiles00.csv , here -a means and and ! means not

              sed




              • -i - edit files in place


              • 1 - line addressing


              • s/^/$(head -1 profiles00.csv)n/ - to replace beginning of file with the given text

              head




              • -1 - to select first line





              share|improve this answer

























                up vote
                1
                down vote













                You don't need xargs



                Command :



                find . -maxdepth 1 ! -name "profiles00.csv" -a -name "*csv" -exec sed -i "1s/^/$(head -1 profiles00.csv)n/" ;


                Here I am using find to select files and sed for in place editing



                Explanation



                find




                • maxdepth 1 - to only select files in the immediate directory and not in sub directory, if you want to edit files in sub directory too, you can remove this option


                • ! -name "profiles00.csv" -a -name "*csv" - to select all csv files but not profiles00.csv , here -a means and and ! means not

                sed




                • -i - edit files in place


                • 1 - line addressing


                • s/^/$(head -1 profiles00.csv)n/ - to replace beginning of file with the given text

                head




                • -1 - to select first line





                share|improve this answer























                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  You don't need xargs



                  Command :



                  find . -maxdepth 1 ! -name "profiles00.csv" -a -name "*csv" -exec sed -i "1s/^/$(head -1 profiles00.csv)n/" ;


                  Here I am using find to select files and sed for in place editing



                  Explanation



                  find




                  • maxdepth 1 - to only select files in the immediate directory and not in sub directory, if you want to edit files in sub directory too, you can remove this option


                  • ! -name "profiles00.csv" -a -name "*csv" - to select all csv files but not profiles00.csv , here -a means and and ! means not

                  sed




                  • -i - edit files in place


                  • 1 - line addressing


                  • s/^/$(head -1 profiles00.csv)n/ - to replace beginning of file with the given text

                  head




                  • -1 - to select first line





                  share|improve this answer













                  You don't need xargs



                  Command :



                  find . -maxdepth 1 ! -name "profiles00.csv" -a -name "*csv" -exec sed -i "1s/^/$(head -1 profiles00.csv)n/" ;


                  Here I am using find to select files and sed for in place editing



                  Explanation



                  find




                  • maxdepth 1 - to only select files in the immediate directory and not in sub directory, if you want to edit files in sub directory too, you can remove this option


                  • ! -name "profiles00.csv" -a -name "*csv" - to select all csv files but not profiles00.csv , here -a means and and ! means not

                  sed




                  • -i - edit files in place


                  • 1 - line addressing


                  • s/^/$(head -1 profiles00.csv)n/ - to replace beginning of file with the given text

                  head




                  • -1 - to select first line






                  share|improve this answer













                  share|improve this answer



                  share|improve this answer











                  answered May 17 at 5:32









                  mkmayank

                  36310




                  36310




















                      up vote
                      0
                      down vote













                      This is an untested pseudo-code sketch so it may not work, but I'd go about it something like (first backup your files so you don't overwrite them):



                      sed -i '1s/^/'`head -n1 profiles00.csv`'/' *.csv


                      explained



                      For more information on inserting into files:



                      https://stackoverflow.com/questions/9533679/how-to-insert-a-text-at-the-beginning-of-a-file






                      share|improve this answer

























                        up vote
                        0
                        down vote













                        This is an untested pseudo-code sketch so it may not work, but I'd go about it something like (first backup your files so you don't overwrite them):



                        sed -i '1s/^/'`head -n1 profiles00.csv`'/' *.csv


                        explained



                        For more information on inserting into files:



                        https://stackoverflow.com/questions/9533679/how-to-insert-a-text-at-the-beginning-of-a-file






                        share|improve this answer























                          up vote
                          0
                          down vote










                          up vote
                          0
                          down vote









                          This is an untested pseudo-code sketch so it may not work, but I'd go about it something like (first backup your files so you don't overwrite them):



                          sed -i '1s/^/'`head -n1 profiles00.csv`'/' *.csv


                          explained



                          For more information on inserting into files:



                          https://stackoverflow.com/questions/9533679/how-to-insert-a-text-at-the-beginning-of-a-file






                          share|improve this answer













                          This is an untested pseudo-code sketch so it may not work, but I'd go about it something like (first backup your files so you don't overwrite them):



                          sed -i '1s/^/'`head -n1 profiles00.csv`'/' *.csv


                          explained



                          For more information on inserting into files:



                          https://stackoverflow.com/questions/9533679/how-to-insert-a-text-at-the-beginning-of-a-file







                          share|improve this answer













                          share|improve this answer



                          share|improve this answer











                          answered May 17 at 3:11









                          Jonathan

                          7081612




                          7081612






















                               

                              draft saved


                              draft discarded


























                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f444274%2fcopy-first-line-from-one-file-to-other-files-using-xargs-and-sed%23new-answer', 'question_page');

                              );

                              Post as a guest













































































                              Popular posts from this blog

                              How to check contact read email or not when send email to Individual?

                              Displaying single band from multi-band raster using QGIS

                              How many registers does an x86_64 CPU actually have?