Extracting words from .txt and creating one master .txt

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











up vote
1
down vote

favorite












I have multiple files .txt which contain names of people sorted alphabetically so for example in main directory I have directory a which contains one a.txt full with names which start with "a" like "Anna" "Andrew" etc. The same thing repeats in main directory I have directory b containing b.txt full with names etc. up until x,y,z. How can I extract the names and create a master.txt containing all people's names?







share|improve this question
























    up vote
    1
    down vote

    favorite












    I have multiple files .txt which contain names of people sorted alphabetically so for example in main directory I have directory a which contains one a.txt full with names which start with "a" like "Anna" "Andrew" etc. The same thing repeats in main directory I have directory b containing b.txt full with names etc. up until x,y,z. How can I extract the names and create a master.txt containing all people's names?







    share|improve this question






















      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I have multiple files .txt which contain names of people sorted alphabetically so for example in main directory I have directory a which contains one a.txt full with names which start with "a" like "Anna" "Andrew" etc. The same thing repeats in main directory I have directory b containing b.txt full with names etc. up until x,y,z. How can I extract the names and create a master.txt containing all people's names?







      share|improve this question












      I have multiple files .txt which contain names of people sorted alphabetically so for example in main directory I have directory a which contains one a.txt full with names which start with "a" like "Anna" "Andrew" etc. The same thing repeats in main directory I have directory b containing b.txt full with names etc. up until x,y,z. How can I extract the names and create a master.txt containing all people's names?









      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 19 '17 at 1:52









      Alex

      1104




      1104




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          2
          down vote













          You can accomplish this by using the cat command and filename expansion. If all of these files are in the source directory /path/to/directory, and there are no other files in this directory, then the most succinct command would be the following:



          cat /path/to/directory/*/*.txt > master.txt


          This will create a file called master.txt in your current directory which contains the concatenated contents of all of the files in the source directory. NOTE: This will include any files in any of the subdirectories of the source directory.



          If there are other files in the directory (or if you just want to be a little bit more precise) then you can use this command instead:



          cat /path/to/directory/[a-z]/[a-z].txt > master.txt


          This will only match the following files in the source directory:



          a/a.txt
          a/b.txt
          a/c.txt
          .
          .
          .
          z/x.txt
          z/y.txt
          z/z.txt


          If there are other files in the source directory, or if you have files of the similar to a/z.txt where the subdirectory name doesn't match the base-name of the file, and if you want to exclude those files, then you would have to use a more precise command to narrow down the list of matched files. In that case you could use brace-expansion and a for-loop:



          for letter in a..z; do 
          cat "/path/to/directory/$letter/$letter.txt";
          done >> master.txt


          This will match exactly the files you've specified in your question and no other files.






          share|improve this answer





























            up vote
            0
            down vote













            Replace main_dir with the path to your main directory, save this in a script and run from the terminal as sh ./script-name.sh



            #!/bin/bash
            for i in $( cd main_dir && ls ); do
            cat "main_dir/$i/$i.txt" >> "master.txt"
            done





            share|improve this answer




















            • There's no reason to parse the output of ls in this situation, since globbing/filename-expansion will work just as well. Also, parsing the output of ls is considered bad practice in general. See Why you shouldn't parse the output of ls for more on that topic.
              – igal
              Nov 19 '17 at 4:01










            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%2f405522%2fextracting-words-from-txt-and-creating-one-master-txt%23new-answer', 'question_page');

            );

            Post as a guest






























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            2
            down vote













            You can accomplish this by using the cat command and filename expansion. If all of these files are in the source directory /path/to/directory, and there are no other files in this directory, then the most succinct command would be the following:



            cat /path/to/directory/*/*.txt > master.txt


            This will create a file called master.txt in your current directory which contains the concatenated contents of all of the files in the source directory. NOTE: This will include any files in any of the subdirectories of the source directory.



            If there are other files in the directory (or if you just want to be a little bit more precise) then you can use this command instead:



            cat /path/to/directory/[a-z]/[a-z].txt > master.txt


            This will only match the following files in the source directory:



            a/a.txt
            a/b.txt
            a/c.txt
            .
            .
            .
            z/x.txt
            z/y.txt
            z/z.txt


            If there are other files in the source directory, or if you have files of the similar to a/z.txt where the subdirectory name doesn't match the base-name of the file, and if you want to exclude those files, then you would have to use a more precise command to narrow down the list of matched files. In that case you could use brace-expansion and a for-loop:



            for letter in a..z; do 
            cat "/path/to/directory/$letter/$letter.txt";
            done >> master.txt


            This will match exactly the files you've specified in your question and no other files.






            share|improve this answer


























              up vote
              2
              down vote













              You can accomplish this by using the cat command and filename expansion. If all of these files are in the source directory /path/to/directory, and there are no other files in this directory, then the most succinct command would be the following:



              cat /path/to/directory/*/*.txt > master.txt


              This will create a file called master.txt in your current directory which contains the concatenated contents of all of the files in the source directory. NOTE: This will include any files in any of the subdirectories of the source directory.



              If there are other files in the directory (or if you just want to be a little bit more precise) then you can use this command instead:



              cat /path/to/directory/[a-z]/[a-z].txt > master.txt


              This will only match the following files in the source directory:



              a/a.txt
              a/b.txt
              a/c.txt
              .
              .
              .
              z/x.txt
              z/y.txt
              z/z.txt


              If there are other files in the source directory, or if you have files of the similar to a/z.txt where the subdirectory name doesn't match the base-name of the file, and if you want to exclude those files, then you would have to use a more precise command to narrow down the list of matched files. In that case you could use brace-expansion and a for-loop:



              for letter in a..z; do 
              cat "/path/to/directory/$letter/$letter.txt";
              done >> master.txt


              This will match exactly the files you've specified in your question and no other files.






              share|improve this answer
























                up vote
                2
                down vote










                up vote
                2
                down vote









                You can accomplish this by using the cat command and filename expansion. If all of these files are in the source directory /path/to/directory, and there are no other files in this directory, then the most succinct command would be the following:



                cat /path/to/directory/*/*.txt > master.txt


                This will create a file called master.txt in your current directory which contains the concatenated contents of all of the files in the source directory. NOTE: This will include any files in any of the subdirectories of the source directory.



                If there are other files in the directory (or if you just want to be a little bit more precise) then you can use this command instead:



                cat /path/to/directory/[a-z]/[a-z].txt > master.txt


                This will only match the following files in the source directory:



                a/a.txt
                a/b.txt
                a/c.txt
                .
                .
                .
                z/x.txt
                z/y.txt
                z/z.txt


                If there are other files in the source directory, or if you have files of the similar to a/z.txt where the subdirectory name doesn't match the base-name of the file, and if you want to exclude those files, then you would have to use a more precise command to narrow down the list of matched files. In that case you could use brace-expansion and a for-loop:



                for letter in a..z; do 
                cat "/path/to/directory/$letter/$letter.txt";
                done >> master.txt


                This will match exactly the files you've specified in your question and no other files.






                share|improve this answer














                You can accomplish this by using the cat command and filename expansion. If all of these files are in the source directory /path/to/directory, and there are no other files in this directory, then the most succinct command would be the following:



                cat /path/to/directory/*/*.txt > master.txt


                This will create a file called master.txt in your current directory which contains the concatenated contents of all of the files in the source directory. NOTE: This will include any files in any of the subdirectories of the source directory.



                If there are other files in the directory (or if you just want to be a little bit more precise) then you can use this command instead:



                cat /path/to/directory/[a-z]/[a-z].txt > master.txt


                This will only match the following files in the source directory:



                a/a.txt
                a/b.txt
                a/c.txt
                .
                .
                .
                z/x.txt
                z/y.txt
                z/z.txt


                If there are other files in the source directory, or if you have files of the similar to a/z.txt where the subdirectory name doesn't match the base-name of the file, and if you want to exclude those files, then you would have to use a more precise command to narrow down the list of matched files. In that case you could use brace-expansion and a for-loop:



                for letter in a..z; do 
                cat "/path/to/directory/$letter/$letter.txt";
                done >> master.txt


                This will match exactly the files you've specified in your question and no other files.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 19 '17 at 3:26

























                answered Nov 19 '17 at 2:18









                igal

                4,830930




                4,830930






















                    up vote
                    0
                    down vote













                    Replace main_dir with the path to your main directory, save this in a script and run from the terminal as sh ./script-name.sh



                    #!/bin/bash
                    for i in $( cd main_dir && ls ); do
                    cat "main_dir/$i/$i.txt" >> "master.txt"
                    done





                    share|improve this answer




















                    • There's no reason to parse the output of ls in this situation, since globbing/filename-expansion will work just as well. Also, parsing the output of ls is considered bad practice in general. See Why you shouldn't parse the output of ls for more on that topic.
                      – igal
                      Nov 19 '17 at 4:01














                    up vote
                    0
                    down vote













                    Replace main_dir with the path to your main directory, save this in a script and run from the terminal as sh ./script-name.sh



                    #!/bin/bash
                    for i in $( cd main_dir && ls ); do
                    cat "main_dir/$i/$i.txt" >> "master.txt"
                    done





                    share|improve this answer




















                    • There's no reason to parse the output of ls in this situation, since globbing/filename-expansion will work just as well. Also, parsing the output of ls is considered bad practice in general. See Why you shouldn't parse the output of ls for more on that topic.
                      – igal
                      Nov 19 '17 at 4:01












                    up vote
                    0
                    down vote










                    up vote
                    0
                    down vote









                    Replace main_dir with the path to your main directory, save this in a script and run from the terminal as sh ./script-name.sh



                    #!/bin/bash
                    for i in $( cd main_dir && ls ); do
                    cat "main_dir/$i/$i.txt" >> "master.txt"
                    done





                    share|improve this answer












                    Replace main_dir with the path to your main directory, save this in a script and run from the terminal as sh ./script-name.sh



                    #!/bin/bash
                    for i in $( cd main_dir && ls ); do
                    cat "main_dir/$i/$i.txt" >> "master.txt"
                    done






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 19 '17 at 2:23









                    shivench

                    215




                    215











                    • There's no reason to parse the output of ls in this situation, since globbing/filename-expansion will work just as well. Also, parsing the output of ls is considered bad practice in general. See Why you shouldn't parse the output of ls for more on that topic.
                      – igal
                      Nov 19 '17 at 4:01
















                    • There's no reason to parse the output of ls in this situation, since globbing/filename-expansion will work just as well. Also, parsing the output of ls is considered bad practice in general. See Why you shouldn't parse the output of ls for more on that topic.
                      – igal
                      Nov 19 '17 at 4:01















                    There's no reason to parse the output of ls in this situation, since globbing/filename-expansion will work just as well. Also, parsing the output of ls is considered bad practice in general. See Why you shouldn't parse the output of ls for more on that topic.
                    – igal
                    Nov 19 '17 at 4:01




                    There's no reason to parse the output of ls in this situation, since globbing/filename-expansion will work just as well. Also, parsing the output of ls is considered bad practice in general. See Why you shouldn't parse the output of ls for more on that topic.
                    – igal
                    Nov 19 '17 at 4:01

















                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f405522%2fextracting-words-from-txt-and-creating-one-master-txt%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?