how to copy data from multiple txt to a single txt file according to the order [closed]

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












1















I have used the code cat *.txt >> output.txt . This code is doing is its work but here unix is deciding the order in which the files are being copied to my output file which is undesirable as i want the files to be copied according to their occurence or time of generation .
I have also used the below command so i can control the copy format



for tablename in 'cat $current_path/table.txt(have defined the order)

do

for data in 'cat path/$tablename.txt

do
echo "$data" >> final/output.txt

done

done


here it is doing its work but generating multiple copies of the single txt as for loop is working .










share|improve this question















closed as unclear what you're asking by G-Man, Mr Shunz, Thomas, X Tian, Luciano Andress Martini Jan 17 at 14:58


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.






















    1















    I have used the code cat *.txt >> output.txt . This code is doing is its work but here unix is deciding the order in which the files are being copied to my output file which is undesirable as i want the files to be copied according to their occurence or time of generation .
    I have also used the below command so i can control the copy format



    for tablename in 'cat $current_path/table.txt(have defined the order)

    do

    for data in 'cat path/$tablename.txt

    do
    echo "$data" >> final/output.txt

    done

    done


    here it is doing its work but generating multiple copies of the single txt as for loop is working .










    share|improve this question















    closed as unclear what you're asking by G-Man, Mr Shunz, Thomas, X Tian, Luciano Andress Martini Jan 17 at 14:58


    Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.




















      1












      1








      1








      I have used the code cat *.txt >> output.txt . This code is doing is its work but here unix is deciding the order in which the files are being copied to my output file which is undesirable as i want the files to be copied according to their occurence or time of generation .
      I have also used the below command so i can control the copy format



      for tablename in 'cat $current_path/table.txt(have defined the order)

      do

      for data in 'cat path/$tablename.txt

      do
      echo "$data" >> final/output.txt

      done

      done


      here it is doing its work but generating multiple copies of the single txt as for loop is working .










      share|improve this question
















      I have used the code cat *.txt >> output.txt . This code is doing is its work but here unix is deciding the order in which the files are being copied to my output file which is undesirable as i want the files to be copied according to their occurence or time of generation .
      I have also used the below command so i can control the copy format



      for tablename in 'cat $current_path/table.txt(have defined the order)

      do

      for data in 'cat path/$tablename.txt

      do
      echo "$data" >> final/output.txt

      done

      done


      here it is doing its work but generating multiple copies of the single txt as for loop is working .







      linux shell-script






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 16 at 8:34









      Rui F Ribeiro

      39.8k1479133




      39.8k1479133










      asked Jan 16 at 3:22









      Rahul RawatRahul Rawat

      111




      111




      closed as unclear what you're asking by G-Man, Mr Shunz, Thomas, X Tian, Luciano Andress Martini Jan 17 at 14:58


      Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.









      closed as unclear what you're asking by G-Man, Mr Shunz, Thomas, X Tian, Luciano Andress Martini Jan 17 at 14:58


      Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.






















          2 Answers
          2






          active

          oldest

          votes


















          2














          Assuming I understand the question then



           xargs < "$current_path/table.txt" cat > final/output.txt


          is all you need. xargs will read the "table.txt" file and run cat with the files in the specified order, so will output them to stdout, and you redirect stdout to the desired place. Note that this works even if there are so many files that xargs executes cat more than once, as the redirection is done only once before any cat (or even the xargs) is executed.



          If you want it sorted according to time, then



           ls -t | xargs cat > final/output


          will work if there are no awkward filenames (e.g. embedded spaces or newlines).






          share|improve this answer























          • Hi icarus the below command does work. Ls - t | xargs cat > final/output but there is one problem that is it is copying the last generated txt file on the top but i want the file which is generated first should be copies to the output file first then the following ones. For example if there are abc.txt (first files) abd.txt then the output file should have data firat of abc..txt and the the following files. Can this be done

            – Rahul Rawat
            Jan 16 at 8:37












          • Does ls -rt | grep -v output |xargs cat > final/output do what you want? Can you edit your question to put in some more example data

            – icarus
            Jan 16 at 12:53











          • I mean i want to append the data in the output file according to the generatiin of the file by using above code the. The output file generated show the data of the last file and then the first it is reversing the order of appending data that is last showing first and first files data showing at the end

            – Rahul Rawat
            Jan 16 at 13:53











          • Does the r in the ls -rt not do what you want? It appears that both I and @joolin are having difficulty in understanding what you are asking for. Please edit your question to show some example data. For example create 3 files cat, dog and zebra each of which just has a line saying this is cat, this is dog and this is zebra. Run touch dog so dog is the newest file so the ages of the files do not match the alphabetical order. show us the output of ls -l and also your desired output.

            – icarus
            Jan 16 at 18:35


















          2














          for tablename in $(cat $current_path/table.txt);do
          for word in $(cat path/$tablename.txt);do
          echo $word
          done
          done > final/output.txt


          This should work. I'm not sure what you mean by




          here it is doing its work but generating multiple copies of the single txt as for loop is working




          Can you be more specific ?




          -- Edited -- following the suggestion below.






          share|improve this answer

























          • This is a correct answer, but it can be improved. One of the things that you can do in the shell is redirect control structures. In this case you can redirect the for loop to the file. Doing so is fractionally more efficient (the shell doesn't need to do the redirection for each cat) but also removes the need to "start with a clean file". So the code is just for tablename in $(cat $current_path/table.txt); do cat "$tablename" ; done > final/output.txt

            – icarus
            Jan 16 at 13:00











          • True, it avoids opening the output.txt file on each iteration.

            – jayooin
            Jan 16 at 13:25











          • Hi have tried this as in my code using the table.txt i am creating seperate $tablename.txt as i have code above also which created diffrent txt. So if you will cat $tablename.txt it will throw error as each $tablename.txt has date row by row and not in a single row ex asdfff gsss. Gsss so here cat wont do.

            – Rahul Rawat
            Jan 16 at 14:02












          • Hi, I edited the answer following your comment. Basically you need the words in each file to be displayed as lines. I'm not sure about the order

            – jayooin
            Jan 16 at 14:25

















          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          Assuming I understand the question then



           xargs < "$current_path/table.txt" cat > final/output.txt


          is all you need. xargs will read the "table.txt" file and run cat with the files in the specified order, so will output them to stdout, and you redirect stdout to the desired place. Note that this works even if there are so many files that xargs executes cat more than once, as the redirection is done only once before any cat (or even the xargs) is executed.



          If you want it sorted according to time, then



           ls -t | xargs cat > final/output


          will work if there are no awkward filenames (e.g. embedded spaces or newlines).






          share|improve this answer























          • Hi icarus the below command does work. Ls - t | xargs cat > final/output but there is one problem that is it is copying the last generated txt file on the top but i want the file which is generated first should be copies to the output file first then the following ones. For example if there are abc.txt (first files) abd.txt then the output file should have data firat of abc..txt and the the following files. Can this be done

            – Rahul Rawat
            Jan 16 at 8:37












          • Does ls -rt | grep -v output |xargs cat > final/output do what you want? Can you edit your question to put in some more example data

            – icarus
            Jan 16 at 12:53











          • I mean i want to append the data in the output file according to the generatiin of the file by using above code the. The output file generated show the data of the last file and then the first it is reversing the order of appending data that is last showing first and first files data showing at the end

            – Rahul Rawat
            Jan 16 at 13:53











          • Does the r in the ls -rt not do what you want? It appears that both I and @joolin are having difficulty in understanding what you are asking for. Please edit your question to show some example data. For example create 3 files cat, dog and zebra each of which just has a line saying this is cat, this is dog and this is zebra. Run touch dog so dog is the newest file so the ages of the files do not match the alphabetical order. show us the output of ls -l and also your desired output.

            – icarus
            Jan 16 at 18:35















          2














          Assuming I understand the question then



           xargs < "$current_path/table.txt" cat > final/output.txt


          is all you need. xargs will read the "table.txt" file and run cat with the files in the specified order, so will output them to stdout, and you redirect stdout to the desired place. Note that this works even if there are so many files that xargs executes cat more than once, as the redirection is done only once before any cat (or even the xargs) is executed.



          If you want it sorted according to time, then



           ls -t | xargs cat > final/output


          will work if there are no awkward filenames (e.g. embedded spaces or newlines).






          share|improve this answer























          • Hi icarus the below command does work. Ls - t | xargs cat > final/output but there is one problem that is it is copying the last generated txt file on the top but i want the file which is generated first should be copies to the output file first then the following ones. For example if there are abc.txt (first files) abd.txt then the output file should have data firat of abc..txt and the the following files. Can this be done

            – Rahul Rawat
            Jan 16 at 8:37












          • Does ls -rt | grep -v output |xargs cat > final/output do what you want? Can you edit your question to put in some more example data

            – icarus
            Jan 16 at 12:53











          • I mean i want to append the data in the output file according to the generatiin of the file by using above code the. The output file generated show the data of the last file and then the first it is reversing the order of appending data that is last showing first and first files data showing at the end

            – Rahul Rawat
            Jan 16 at 13:53











          • Does the r in the ls -rt not do what you want? It appears that both I and @joolin are having difficulty in understanding what you are asking for. Please edit your question to show some example data. For example create 3 files cat, dog and zebra each of which just has a line saying this is cat, this is dog and this is zebra. Run touch dog so dog is the newest file so the ages of the files do not match the alphabetical order. show us the output of ls -l and also your desired output.

            – icarus
            Jan 16 at 18:35













          2












          2








          2







          Assuming I understand the question then



           xargs < "$current_path/table.txt" cat > final/output.txt


          is all you need. xargs will read the "table.txt" file and run cat with the files in the specified order, so will output them to stdout, and you redirect stdout to the desired place. Note that this works even if there are so many files that xargs executes cat more than once, as the redirection is done only once before any cat (or even the xargs) is executed.



          If you want it sorted according to time, then



           ls -t | xargs cat > final/output


          will work if there are no awkward filenames (e.g. embedded spaces or newlines).






          share|improve this answer













          Assuming I understand the question then



           xargs < "$current_path/table.txt" cat > final/output.txt


          is all you need. xargs will read the "table.txt" file and run cat with the files in the specified order, so will output them to stdout, and you redirect stdout to the desired place. Note that this works even if there are so many files that xargs executes cat more than once, as the redirection is done only once before any cat (or even the xargs) is executed.



          If you want it sorted according to time, then



           ls -t | xargs cat > final/output


          will work if there are no awkward filenames (e.g. embedded spaces or newlines).







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 16 at 4:11









          icarusicarus

          6,02111030




          6,02111030












          • Hi icarus the below command does work. Ls - t | xargs cat > final/output but there is one problem that is it is copying the last generated txt file on the top but i want the file which is generated first should be copies to the output file first then the following ones. For example if there are abc.txt (first files) abd.txt then the output file should have data firat of abc..txt and the the following files. Can this be done

            – Rahul Rawat
            Jan 16 at 8:37












          • Does ls -rt | grep -v output |xargs cat > final/output do what you want? Can you edit your question to put in some more example data

            – icarus
            Jan 16 at 12:53











          • I mean i want to append the data in the output file according to the generatiin of the file by using above code the. The output file generated show the data of the last file and then the first it is reversing the order of appending data that is last showing first and first files data showing at the end

            – Rahul Rawat
            Jan 16 at 13:53











          • Does the r in the ls -rt not do what you want? It appears that both I and @joolin are having difficulty in understanding what you are asking for. Please edit your question to show some example data. For example create 3 files cat, dog and zebra each of which just has a line saying this is cat, this is dog and this is zebra. Run touch dog so dog is the newest file so the ages of the files do not match the alphabetical order. show us the output of ls -l and also your desired output.

            – icarus
            Jan 16 at 18:35

















          • Hi icarus the below command does work. Ls - t | xargs cat > final/output but there is one problem that is it is copying the last generated txt file on the top but i want the file which is generated first should be copies to the output file first then the following ones. For example if there are abc.txt (first files) abd.txt then the output file should have data firat of abc..txt and the the following files. Can this be done

            – Rahul Rawat
            Jan 16 at 8:37












          • Does ls -rt | grep -v output |xargs cat > final/output do what you want? Can you edit your question to put in some more example data

            – icarus
            Jan 16 at 12:53











          • I mean i want to append the data in the output file according to the generatiin of the file by using above code the. The output file generated show the data of the last file and then the first it is reversing the order of appending data that is last showing first and first files data showing at the end

            – Rahul Rawat
            Jan 16 at 13:53











          • Does the r in the ls -rt not do what you want? It appears that both I and @joolin are having difficulty in understanding what you are asking for. Please edit your question to show some example data. For example create 3 files cat, dog and zebra each of which just has a line saying this is cat, this is dog and this is zebra. Run touch dog so dog is the newest file so the ages of the files do not match the alphabetical order. show us the output of ls -l and also your desired output.

            – icarus
            Jan 16 at 18:35
















          Hi icarus the below command does work. Ls - t | xargs cat > final/output but there is one problem that is it is copying the last generated txt file on the top but i want the file which is generated first should be copies to the output file first then the following ones. For example if there are abc.txt (first files) abd.txt then the output file should have data firat of abc..txt and the the following files. Can this be done

          – Rahul Rawat
          Jan 16 at 8:37






          Hi icarus the below command does work. Ls - t | xargs cat > final/output but there is one problem that is it is copying the last generated txt file on the top but i want the file which is generated first should be copies to the output file first then the following ones. For example if there are abc.txt (first files) abd.txt then the output file should have data firat of abc..txt and the the following files. Can this be done

          – Rahul Rawat
          Jan 16 at 8:37














          Does ls -rt | grep -v output |xargs cat > final/output do what you want? Can you edit your question to put in some more example data

          – icarus
          Jan 16 at 12:53





          Does ls -rt | grep -v output |xargs cat > final/output do what you want? Can you edit your question to put in some more example data

          – icarus
          Jan 16 at 12:53













          I mean i want to append the data in the output file according to the generatiin of the file by using above code the. The output file generated show the data of the last file and then the first it is reversing the order of appending data that is last showing first and first files data showing at the end

          – Rahul Rawat
          Jan 16 at 13:53





          I mean i want to append the data in the output file according to the generatiin of the file by using above code the. The output file generated show the data of the last file and then the first it is reversing the order of appending data that is last showing first and first files data showing at the end

          – Rahul Rawat
          Jan 16 at 13:53













          Does the r in the ls -rt not do what you want? It appears that both I and @joolin are having difficulty in understanding what you are asking for. Please edit your question to show some example data. For example create 3 files cat, dog and zebra each of which just has a line saying this is cat, this is dog and this is zebra. Run touch dog so dog is the newest file so the ages of the files do not match the alphabetical order. show us the output of ls -l and also your desired output.

          – icarus
          Jan 16 at 18:35





          Does the r in the ls -rt not do what you want? It appears that both I and @joolin are having difficulty in understanding what you are asking for. Please edit your question to show some example data. For example create 3 files cat, dog and zebra each of which just has a line saying this is cat, this is dog and this is zebra. Run touch dog so dog is the newest file so the ages of the files do not match the alphabetical order. show us the output of ls -l and also your desired output.

          – icarus
          Jan 16 at 18:35













          2














          for tablename in $(cat $current_path/table.txt);do
          for word in $(cat path/$tablename.txt);do
          echo $word
          done
          done > final/output.txt


          This should work. I'm not sure what you mean by




          here it is doing its work but generating multiple copies of the single txt as for loop is working




          Can you be more specific ?




          -- Edited -- following the suggestion below.






          share|improve this answer

























          • This is a correct answer, but it can be improved. One of the things that you can do in the shell is redirect control structures. In this case you can redirect the for loop to the file. Doing so is fractionally more efficient (the shell doesn't need to do the redirection for each cat) but also removes the need to "start with a clean file". So the code is just for tablename in $(cat $current_path/table.txt); do cat "$tablename" ; done > final/output.txt

            – icarus
            Jan 16 at 13:00











          • True, it avoids opening the output.txt file on each iteration.

            – jayooin
            Jan 16 at 13:25











          • Hi have tried this as in my code using the table.txt i am creating seperate $tablename.txt as i have code above also which created diffrent txt. So if you will cat $tablename.txt it will throw error as each $tablename.txt has date row by row and not in a single row ex asdfff gsss. Gsss so here cat wont do.

            – Rahul Rawat
            Jan 16 at 14:02












          • Hi, I edited the answer following your comment. Basically you need the words in each file to be displayed as lines. I'm not sure about the order

            – jayooin
            Jan 16 at 14:25















          2














          for tablename in $(cat $current_path/table.txt);do
          for word in $(cat path/$tablename.txt);do
          echo $word
          done
          done > final/output.txt


          This should work. I'm not sure what you mean by




          here it is doing its work but generating multiple copies of the single txt as for loop is working




          Can you be more specific ?




          -- Edited -- following the suggestion below.






          share|improve this answer

























          • This is a correct answer, but it can be improved. One of the things that you can do in the shell is redirect control structures. In this case you can redirect the for loop to the file. Doing so is fractionally more efficient (the shell doesn't need to do the redirection for each cat) but also removes the need to "start with a clean file". So the code is just for tablename in $(cat $current_path/table.txt); do cat "$tablename" ; done > final/output.txt

            – icarus
            Jan 16 at 13:00











          • True, it avoids opening the output.txt file on each iteration.

            – jayooin
            Jan 16 at 13:25











          • Hi have tried this as in my code using the table.txt i am creating seperate $tablename.txt as i have code above also which created diffrent txt. So if you will cat $tablename.txt it will throw error as each $tablename.txt has date row by row and not in a single row ex asdfff gsss. Gsss so here cat wont do.

            – Rahul Rawat
            Jan 16 at 14:02












          • Hi, I edited the answer following your comment. Basically you need the words in each file to be displayed as lines. I'm not sure about the order

            – jayooin
            Jan 16 at 14:25













          2












          2








          2







          for tablename in $(cat $current_path/table.txt);do
          for word in $(cat path/$tablename.txt);do
          echo $word
          done
          done > final/output.txt


          This should work. I'm not sure what you mean by




          here it is doing its work but generating multiple copies of the single txt as for loop is working




          Can you be more specific ?




          -- Edited -- following the suggestion below.






          share|improve this answer















          for tablename in $(cat $current_path/table.txt);do
          for word in $(cat path/$tablename.txt);do
          echo $word
          done
          done > final/output.txt


          This should work. I'm not sure what you mean by




          here it is doing its work but generating multiple copies of the single txt as for loop is working




          Can you be more specific ?




          -- Edited -- following the suggestion below.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 16 at 14:23

























          answered Jan 16 at 10:30









          jayooinjayooin

          3347




          3347












          • This is a correct answer, but it can be improved. One of the things that you can do in the shell is redirect control structures. In this case you can redirect the for loop to the file. Doing so is fractionally more efficient (the shell doesn't need to do the redirection for each cat) but also removes the need to "start with a clean file". So the code is just for tablename in $(cat $current_path/table.txt); do cat "$tablename" ; done > final/output.txt

            – icarus
            Jan 16 at 13:00











          • True, it avoids opening the output.txt file on each iteration.

            – jayooin
            Jan 16 at 13:25











          • Hi have tried this as in my code using the table.txt i am creating seperate $tablename.txt as i have code above also which created diffrent txt. So if you will cat $tablename.txt it will throw error as each $tablename.txt has date row by row and not in a single row ex asdfff gsss. Gsss so here cat wont do.

            – Rahul Rawat
            Jan 16 at 14:02












          • Hi, I edited the answer following your comment. Basically you need the words in each file to be displayed as lines. I'm not sure about the order

            – jayooin
            Jan 16 at 14:25

















          • This is a correct answer, but it can be improved. One of the things that you can do in the shell is redirect control structures. In this case you can redirect the for loop to the file. Doing so is fractionally more efficient (the shell doesn't need to do the redirection for each cat) but also removes the need to "start with a clean file". So the code is just for tablename in $(cat $current_path/table.txt); do cat "$tablename" ; done > final/output.txt

            – icarus
            Jan 16 at 13:00











          • True, it avoids opening the output.txt file on each iteration.

            – jayooin
            Jan 16 at 13:25











          • Hi have tried this as in my code using the table.txt i am creating seperate $tablename.txt as i have code above also which created diffrent txt. So if you will cat $tablename.txt it will throw error as each $tablename.txt has date row by row and not in a single row ex asdfff gsss. Gsss so here cat wont do.

            – Rahul Rawat
            Jan 16 at 14:02












          • Hi, I edited the answer following your comment. Basically you need the words in each file to be displayed as lines. I'm not sure about the order

            – jayooin
            Jan 16 at 14:25
















          This is a correct answer, but it can be improved. One of the things that you can do in the shell is redirect control structures. In this case you can redirect the for loop to the file. Doing so is fractionally more efficient (the shell doesn't need to do the redirection for each cat) but also removes the need to "start with a clean file". So the code is just for tablename in $(cat $current_path/table.txt); do cat "$tablename" ; done > final/output.txt

          – icarus
          Jan 16 at 13:00





          This is a correct answer, but it can be improved. One of the things that you can do in the shell is redirect control structures. In this case you can redirect the for loop to the file. Doing so is fractionally more efficient (the shell doesn't need to do the redirection for each cat) but also removes the need to "start with a clean file". So the code is just for tablename in $(cat $current_path/table.txt); do cat "$tablename" ; done > final/output.txt

          – icarus
          Jan 16 at 13:00













          True, it avoids opening the output.txt file on each iteration.

          – jayooin
          Jan 16 at 13:25





          True, it avoids opening the output.txt file on each iteration.

          – jayooin
          Jan 16 at 13:25













          Hi have tried this as in my code using the table.txt i am creating seperate $tablename.txt as i have code above also which created diffrent txt. So if you will cat $tablename.txt it will throw error as each $tablename.txt has date row by row and not in a single row ex asdfff gsss. Gsss so here cat wont do.

          – Rahul Rawat
          Jan 16 at 14:02






          Hi have tried this as in my code using the table.txt i am creating seperate $tablename.txt as i have code above also which created diffrent txt. So if you will cat $tablename.txt it will throw error as each $tablename.txt has date row by row and not in a single row ex asdfff gsss. Gsss so here cat wont do.

          – Rahul Rawat
          Jan 16 at 14:02














          Hi, I edited the answer following your comment. Basically you need the words in each file to be displayed as lines. I'm not sure about the order

          – jayooin
          Jan 16 at 14:25





          Hi, I edited the answer following your comment. Basically you need the words in each file to be displayed as lines. I'm not sure about the order

          – jayooin
          Jan 16 at 14:25


          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?