extracting string from files and merge (python)

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





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








0















I have a directory with multiple subfolders that all contain several text files which are formatted in the following way.



data01:data02


I need to extract just the data02 after the : and export this to a single file in the root directory. I got the extraction in place, but how can I run this on multiple directories and files?










share|improve this question
























  • Hi Gromit, this looks like a perfect task for command line tools "find" and "awk". Can you please explain, why you'd require python for this task?

    – Marvin
    Mar 15 at 11:20











  • If you have a suggestion resolve this just by using cmd, it's also a option I'am just not familiar with using awk etc.

    – Gromit
    Mar 15 at 11:24

















0















I have a directory with multiple subfolders that all contain several text files which are formatted in the following way.



data01:data02


I need to extract just the data02 after the : and export this to a single file in the root directory. I got the extraction in place, but how can I run this on multiple directories and files?










share|improve this question
























  • Hi Gromit, this looks like a perfect task for command line tools "find" and "awk". Can you please explain, why you'd require python for this task?

    – Marvin
    Mar 15 at 11:20











  • If you have a suggestion resolve this just by using cmd, it's also a option I'am just not familiar with using awk etc.

    – Gromit
    Mar 15 at 11:24













0












0








0








I have a directory with multiple subfolders that all contain several text files which are formatted in the following way.



data01:data02


I need to extract just the data02 after the : and export this to a single file in the root directory. I got the extraction in place, but how can I run this on multiple directories and files?










share|improve this question
















I have a directory with multiple subfolders that all contain several text files which are formatted in the following way.



data01:data02


I need to extract just the data02 after the : and export this to a single file in the root directory. I got the extraction in place, but how can I run this on multiple directories and files?







python






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 15 at 10:57









ilkkachu

63.4k10104181




63.4k10104181










asked Mar 15 at 10:50









GromitGromit

1




1












  • Hi Gromit, this looks like a perfect task for command line tools "find" and "awk". Can you please explain, why you'd require python for this task?

    – Marvin
    Mar 15 at 11:20











  • If you have a suggestion resolve this just by using cmd, it's also a option I'am just not familiar with using awk etc.

    – Gromit
    Mar 15 at 11:24

















  • Hi Gromit, this looks like a perfect task for command line tools "find" and "awk". Can you please explain, why you'd require python for this task?

    – Marvin
    Mar 15 at 11:20











  • If you have a suggestion resolve this just by using cmd, it's also a option I'am just not familiar with using awk etc.

    – Gromit
    Mar 15 at 11:24
















Hi Gromit, this looks like a perfect task for command line tools "find" and "awk". Can you please explain, why you'd require python for this task?

– Marvin
Mar 15 at 11:20





Hi Gromit, this looks like a perfect task for command line tools "find" and "awk". Can you please explain, why you'd require python for this task?

– Marvin
Mar 15 at 11:20













If you have a suggestion resolve this just by using cmd, it's also a option I'am just not familiar with using awk etc.

– Gromit
Mar 15 at 11:24





If you have a suggestion resolve this just by using cmd, it's also a option I'am just not familiar with using awk etc.

– Gromit
Mar 15 at 11:24










2 Answers
2






active

oldest

votes


















1














You can customize the following command line with "find" and "awk"



find FOLDERLIST -type f -iname "PATTERN" 
-exec awk -F":" 'NF>1 print $2' "" ; > /PATH/TO/RESULTFILE


where



  • FOLDERLIST is a space separated list of top folders you want to recursively search, whereby the "current folder" would be a dot: find . -type f ...

  • "-type f" for searching files only

  • PATTERN is the common pattern of the files you are interested in, e.g. an asterix "*" will find all files, "*.csv" will find CSV files, ...

  • /PATH/TO/RESULTFILE is the name of your result file in root directory

  • the "awk" part splits all found files at the ":" and skips empty results

EDIT: adjusted empty results check to NF>1 as suggested by steeldriver.






share|improve this answer

























  • NF>1 print $2 might be safer (in case $2 evaluates numerically as 0)

    – steeldriver
    Mar 15 at 12:00











  • You are right steeldriver, thank you :)

    – Marvin
    Mar 15 at 12:19











  • No problem - upvoted ;)

    – steeldriver
    Mar 15 at 12:20


















0














You have not provided the format of the output file, so I am writing assuming that you want second field in separate line. You can use(assuming all files are in the format a:b as you said in your question):



find directory -type f 
-exec awk -F: 'print $2' "" >> /output.txt ;


It will find files in directory and in it's sub-directory, and will execute command awk -F: 'print $2' "" >> /output.txt, which will write value after :, to a file /output.txt.






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



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f506476%2fextracting-string-from-files-and-merge-python%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    You can customize the following command line with "find" and "awk"



    find FOLDERLIST -type f -iname "PATTERN" 
    -exec awk -F":" 'NF>1 print $2' "" ; > /PATH/TO/RESULTFILE


    where



    • FOLDERLIST is a space separated list of top folders you want to recursively search, whereby the "current folder" would be a dot: find . -type f ...

    • "-type f" for searching files only

    • PATTERN is the common pattern of the files you are interested in, e.g. an asterix "*" will find all files, "*.csv" will find CSV files, ...

    • /PATH/TO/RESULTFILE is the name of your result file in root directory

    • the "awk" part splits all found files at the ":" and skips empty results

    EDIT: adjusted empty results check to NF>1 as suggested by steeldriver.






    share|improve this answer

























    • NF>1 print $2 might be safer (in case $2 evaluates numerically as 0)

      – steeldriver
      Mar 15 at 12:00











    • You are right steeldriver, thank you :)

      – Marvin
      Mar 15 at 12:19











    • No problem - upvoted ;)

      – steeldriver
      Mar 15 at 12:20















    1














    You can customize the following command line with "find" and "awk"



    find FOLDERLIST -type f -iname "PATTERN" 
    -exec awk -F":" 'NF>1 print $2' "" ; > /PATH/TO/RESULTFILE


    where



    • FOLDERLIST is a space separated list of top folders you want to recursively search, whereby the "current folder" would be a dot: find . -type f ...

    • "-type f" for searching files only

    • PATTERN is the common pattern of the files you are interested in, e.g. an asterix "*" will find all files, "*.csv" will find CSV files, ...

    • /PATH/TO/RESULTFILE is the name of your result file in root directory

    • the "awk" part splits all found files at the ":" and skips empty results

    EDIT: adjusted empty results check to NF>1 as suggested by steeldriver.






    share|improve this answer

























    • NF>1 print $2 might be safer (in case $2 evaluates numerically as 0)

      – steeldriver
      Mar 15 at 12:00











    • You are right steeldriver, thank you :)

      – Marvin
      Mar 15 at 12:19











    • No problem - upvoted ;)

      – steeldriver
      Mar 15 at 12:20













    1












    1








    1







    You can customize the following command line with "find" and "awk"



    find FOLDERLIST -type f -iname "PATTERN" 
    -exec awk -F":" 'NF>1 print $2' "" ; > /PATH/TO/RESULTFILE


    where



    • FOLDERLIST is a space separated list of top folders you want to recursively search, whereby the "current folder" would be a dot: find . -type f ...

    • "-type f" for searching files only

    • PATTERN is the common pattern of the files you are interested in, e.g. an asterix "*" will find all files, "*.csv" will find CSV files, ...

    • /PATH/TO/RESULTFILE is the name of your result file in root directory

    • the "awk" part splits all found files at the ":" and skips empty results

    EDIT: adjusted empty results check to NF>1 as suggested by steeldriver.






    share|improve this answer















    You can customize the following command line with "find" and "awk"



    find FOLDERLIST -type f -iname "PATTERN" 
    -exec awk -F":" 'NF>1 print $2' "" ; > /PATH/TO/RESULTFILE


    where



    • FOLDERLIST is a space separated list of top folders you want to recursively search, whereby the "current folder" would be a dot: find . -type f ...

    • "-type f" for searching files only

    • PATTERN is the common pattern of the files you are interested in, e.g. an asterix "*" will find all files, "*.csv" will find CSV files, ...

    • /PATH/TO/RESULTFILE is the name of your result file in root directory

    • the "awk" part splits all found files at the ":" and skips empty results

    EDIT: adjusted empty results check to NF>1 as suggested by steeldriver.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Mar 15 at 12:19

























    answered Mar 15 at 11:47









    MarvinMarvin

    19116




    19116












    • NF>1 print $2 might be safer (in case $2 evaluates numerically as 0)

      – steeldriver
      Mar 15 at 12:00











    • You are right steeldriver, thank you :)

      – Marvin
      Mar 15 at 12:19











    • No problem - upvoted ;)

      – steeldriver
      Mar 15 at 12:20

















    • NF>1 print $2 might be safer (in case $2 evaluates numerically as 0)

      – steeldriver
      Mar 15 at 12:00











    • You are right steeldriver, thank you :)

      – Marvin
      Mar 15 at 12:19











    • No problem - upvoted ;)

      – steeldriver
      Mar 15 at 12:20
















    NF>1 print $2 might be safer (in case $2 evaluates numerically as 0)

    – steeldriver
    Mar 15 at 12:00





    NF>1 print $2 might be safer (in case $2 evaluates numerically as 0)

    – steeldriver
    Mar 15 at 12:00













    You are right steeldriver, thank you :)

    – Marvin
    Mar 15 at 12:19





    You are right steeldriver, thank you :)

    – Marvin
    Mar 15 at 12:19













    No problem - upvoted ;)

    – steeldriver
    Mar 15 at 12:20





    No problem - upvoted ;)

    – steeldriver
    Mar 15 at 12:20













    0














    You have not provided the format of the output file, so I am writing assuming that you want second field in separate line. You can use(assuming all files are in the format a:b as you said in your question):



    find directory -type f 
    -exec awk -F: 'print $2' "" >> /output.txt ;


    It will find files in directory and in it's sub-directory, and will execute command awk -F: 'print $2' "" >> /output.txt, which will write value after :, to a file /output.txt.






    share|improve this answer





























      0














      You have not provided the format of the output file, so I am writing assuming that you want second field in separate line. You can use(assuming all files are in the format a:b as you said in your question):



      find directory -type f 
      -exec awk -F: 'print $2' "" >> /output.txt ;


      It will find files in directory and in it's sub-directory, and will execute command awk -F: 'print $2' "" >> /output.txt, which will write value after :, to a file /output.txt.






      share|improve this answer



























        0












        0








        0







        You have not provided the format of the output file, so I am writing assuming that you want second field in separate line. You can use(assuming all files are in the format a:b as you said in your question):



        find directory -type f 
        -exec awk -F: 'print $2' "" >> /output.txt ;


        It will find files in directory and in it's sub-directory, and will execute command awk -F: 'print $2' "" >> /output.txt, which will write value after :, to a file /output.txt.






        share|improve this answer















        You have not provided the format of the output file, so I am writing assuming that you want second field in separate line. You can use(assuming all files are in the format a:b as you said in your question):



        find directory -type f 
        -exec awk -F: 'print $2' "" >> /output.txt ;


        It will find files in directory and in it's sub-directory, and will execute command awk -F: 'print $2' "" >> /output.txt, which will write value after :, to a file /output.txt.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Mar 15 at 11:54

























        answered Mar 15 at 11:46









        Prvt_YadvPrvt_Yadv

        3,30631430




        3,30631430



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Unix & Linux Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f506476%2fextracting-string-from-files-and-merge-python%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

            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?