For loop to iterate over directory tree extracting results from files of the same nametr a A < /etc/hosts...

How can people dance around bonfires on Lag Lo'Omer - it's darchei emori?

How does an ARM MCU run faster than the external crystal?

Does this degree 12 genus 1 curve have only one point over infinitely many finite fields?

Employer demanding to see degree after poor code review

Would the Geas spell work in a dead magic zone once you enter it?

Placing bypass capacitors after VCC reaches the IC

Why does the 'metric Lagrangian' approach appear to fail in Newtonian mechanics?

Array Stutter Implementation

Is using the CH340G on a commercial product a horrible idea?

Plot twist where the antagonist wins

When and what was the first 3D acceleration device ever released?

Graph with same number of edges and vertices is a loop?

Why does the 6502 have the BIT instruction?

Is healing by fire possible?

Is there a way to make it so the cursor is included when I prtscr key?

Riley Rebuses that Share a Common Theme

Why are C64 games inconsistent with which joystick port they use?

Employer asking for online access to bank account - Is this a scam?

Infinite Sequence based on Simple Rule

How to convert to standalone document a matrix table

How bitcoin nodes update UTXO set when their latests blocks are replaced?

Which noble houses were destroyed during the Game of Thrones?

I think I may have violated academic integrity last year - what should I do?

Tic-Tac-Toe for the terminal



For loop to iterate over directory tree extracting results from files of the same name


tr a A < /etc/hosts | sort -r |pr -d > /etc/hostsGetting data from matching listWhy won't the for loop execute on directoryFor loop in Unix : including files from sub directoriesfor loop to iterate through some file nth positionfor loop over a listfor loop over input linesIterate over multiple files in a folder and assign valuePrinting the number of files having the same name for every file from a directoryFor a large directory, create a variable of the filenames which include lines which include the text string stored in another variableThe $@ variable is not working in a for loop, trying to iterate through a users list






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







0















I have a series of directories, all with list.txt in the same format, and I wish to put the results into a single file. I am looking to write a script that will iteratively move through each directory tree, extract a specific column from the list.txt file without surrounding text using the grep/awk pipeline below and write the outputs of each to the same file.



    grep 'bar[0-9]' file.txt | awk '{print $1}'


I have attempted the following but I am not sure exactly where my loops in the script are going wrong.



#!/bin/bash
##Extract ligands from toplist and concatenate to file
for i in /home/ubuntu/Project/working/library_*/Results/list.txt
do
grep 'bar[0-9]' i | awk '{print $1}' | cat ../output.txt i
done


The directory tree is as follows:



.
├── library_1-200
│ ├── Results
│ │ ├── complex
│ │ ├── sorted.txt
│ │ └── list.txt
│ ├── files
│ │ ├── output
│ │ └── txt
│ └── summary.txt
├── library_201-400
│ ├── Results
│ │ ├── complex
│ │ ├── sorted.txt
│ │ └── list.txt
│ ├── files
│ │ ├── output
│ │ └── txt
│ └── summary.txt
├── library_401-600
│ ├── Results
│ │ ├── complex
│ │ ├── sorted.txt
│ │ └── list.txt
│ ├── files
│ │ ├── output
│ │ └── txt
│ └── summary.txt
└── library_601-800
├── Results
│ ├── complex
│ ├── sorted.txt
│ └── list.txt
├── files
│ ├── output
│ └── txt
└── summary.txt


Sample of list.txt, where I just want the Name values put into output.txt



Name    Score
bar65 -7.8
bar74 -7.5
bar14 -7.5
bar43 -7.4
bar94 -7.4
bar16 -7.4
bar12 -7.3
bar25 -7.3
bar65 -7.3
bar76 -7.3
bar24 -7.3
bar13 -7.3
bar58 -7.2
bar68 -7.2
bar28 -7.2


Solution was to put "$i" where I previously had just i and to modify to | cat >> ../output.txt










share|improve this question









New contributor



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




















  • Assuming you mean to do something like grep 'bar[0-9]' "$i" | awk '{print $1}' | cat > "$i", see unix.stackexchange.com/a/425801/70524

    – muru
    41 mins ago













  • Thanks @muru. That did the job of writing to an output file, however it printed the entirety of the list.txt files to the single output, seemingly ignoring the grep and awk commands.

    – proteinmodels
    30 mins ago











  • You probably meant ... | cat > ../output.txt, or without the unnecessary cat, just ` > ../output.txt`.

    – RalfFriedl
    20 mins ago











  • Yeah now tried removing the "$i" at the end, and it worked. Thanks!

    – proteinmodels
    20 mins ago


















0















I have a series of directories, all with list.txt in the same format, and I wish to put the results into a single file. I am looking to write a script that will iteratively move through each directory tree, extract a specific column from the list.txt file without surrounding text using the grep/awk pipeline below and write the outputs of each to the same file.



    grep 'bar[0-9]' file.txt | awk '{print $1}'


I have attempted the following but I am not sure exactly where my loops in the script are going wrong.



#!/bin/bash
##Extract ligands from toplist and concatenate to file
for i in /home/ubuntu/Project/working/library_*/Results/list.txt
do
grep 'bar[0-9]' i | awk '{print $1}' | cat ../output.txt i
done


The directory tree is as follows:



.
├── library_1-200
│ ├── Results
│ │ ├── complex
│ │ ├── sorted.txt
│ │ └── list.txt
│ ├── files
│ │ ├── output
│ │ └── txt
│ └── summary.txt
├── library_201-400
│ ├── Results
│ │ ├── complex
│ │ ├── sorted.txt
│ │ └── list.txt
│ ├── files
│ │ ├── output
│ │ └── txt
│ └── summary.txt
├── library_401-600
│ ├── Results
│ │ ├── complex
│ │ ├── sorted.txt
│ │ └── list.txt
│ ├── files
│ │ ├── output
│ │ └── txt
│ └── summary.txt
└── library_601-800
├── Results
│ ├── complex
│ ├── sorted.txt
│ └── list.txt
├── files
│ ├── output
│ └── txt
└── summary.txt


Sample of list.txt, where I just want the Name values put into output.txt



Name    Score
bar65 -7.8
bar74 -7.5
bar14 -7.5
bar43 -7.4
bar94 -7.4
bar16 -7.4
bar12 -7.3
bar25 -7.3
bar65 -7.3
bar76 -7.3
bar24 -7.3
bar13 -7.3
bar58 -7.2
bar68 -7.2
bar28 -7.2


Solution was to put "$i" where I previously had just i and to modify to | cat >> ../output.txt










share|improve this question









New contributor



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




















  • Assuming you mean to do something like grep 'bar[0-9]' "$i" | awk '{print $1}' | cat > "$i", see unix.stackexchange.com/a/425801/70524

    – muru
    41 mins ago













  • Thanks @muru. That did the job of writing to an output file, however it printed the entirety of the list.txt files to the single output, seemingly ignoring the grep and awk commands.

    – proteinmodels
    30 mins ago











  • You probably meant ... | cat > ../output.txt, or without the unnecessary cat, just ` > ../output.txt`.

    – RalfFriedl
    20 mins ago











  • Yeah now tried removing the "$i" at the end, and it worked. Thanks!

    – proteinmodels
    20 mins ago














0












0








0








I have a series of directories, all with list.txt in the same format, and I wish to put the results into a single file. I am looking to write a script that will iteratively move through each directory tree, extract a specific column from the list.txt file without surrounding text using the grep/awk pipeline below and write the outputs of each to the same file.



    grep 'bar[0-9]' file.txt | awk '{print $1}'


I have attempted the following but I am not sure exactly where my loops in the script are going wrong.



#!/bin/bash
##Extract ligands from toplist and concatenate to file
for i in /home/ubuntu/Project/working/library_*/Results/list.txt
do
grep 'bar[0-9]' i | awk '{print $1}' | cat ../output.txt i
done


The directory tree is as follows:



.
├── library_1-200
│ ├── Results
│ │ ├── complex
│ │ ├── sorted.txt
│ │ └── list.txt
│ ├── files
│ │ ├── output
│ │ └── txt
│ └── summary.txt
├── library_201-400
│ ├── Results
│ │ ├── complex
│ │ ├── sorted.txt
│ │ └── list.txt
│ ├── files
│ │ ├── output
│ │ └── txt
│ └── summary.txt
├── library_401-600
│ ├── Results
│ │ ├── complex
│ │ ├── sorted.txt
│ │ └── list.txt
│ ├── files
│ │ ├── output
│ │ └── txt
│ └── summary.txt
└── library_601-800
├── Results
│ ├── complex
│ ├── sorted.txt
│ └── list.txt
├── files
│ ├── output
│ └── txt
└── summary.txt


Sample of list.txt, where I just want the Name values put into output.txt



Name    Score
bar65 -7.8
bar74 -7.5
bar14 -7.5
bar43 -7.4
bar94 -7.4
bar16 -7.4
bar12 -7.3
bar25 -7.3
bar65 -7.3
bar76 -7.3
bar24 -7.3
bar13 -7.3
bar58 -7.2
bar68 -7.2
bar28 -7.2


Solution was to put "$i" where I previously had just i and to modify to | cat >> ../output.txt










share|improve this question









New contributor



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











I have a series of directories, all with list.txt in the same format, and I wish to put the results into a single file. I am looking to write a script that will iteratively move through each directory tree, extract a specific column from the list.txt file without surrounding text using the grep/awk pipeline below and write the outputs of each to the same file.



    grep 'bar[0-9]' file.txt | awk '{print $1}'


I have attempted the following but I am not sure exactly where my loops in the script are going wrong.



#!/bin/bash
##Extract ligands from toplist and concatenate to file
for i in /home/ubuntu/Project/working/library_*/Results/list.txt
do
grep 'bar[0-9]' i | awk '{print $1}' | cat ../output.txt i
done


The directory tree is as follows:



.
├── library_1-200
│ ├── Results
│ │ ├── complex
│ │ ├── sorted.txt
│ │ └── list.txt
│ ├── files
│ │ ├── output
│ │ └── txt
│ └── summary.txt
├── library_201-400
│ ├── Results
│ │ ├── complex
│ │ ├── sorted.txt
│ │ └── list.txt
│ ├── files
│ │ ├── output
│ │ └── txt
│ └── summary.txt
├── library_401-600
│ ├── Results
│ │ ├── complex
│ │ ├── sorted.txt
│ │ └── list.txt
│ ├── files
│ │ ├── output
│ │ └── txt
│ └── summary.txt
└── library_601-800
├── Results
│ ├── complex
│ ├── sorted.txt
│ └── list.txt
├── files
│ ├── output
│ └── txt
└── summary.txt


Sample of list.txt, where I just want the Name values put into output.txt



Name    Score
bar65 -7.8
bar74 -7.5
bar14 -7.5
bar43 -7.4
bar94 -7.4
bar16 -7.4
bar12 -7.3
bar25 -7.3
bar65 -7.3
bar76 -7.3
bar24 -7.3
bar13 -7.3
bar58 -7.2
bar68 -7.2
bar28 -7.2


Solution was to put "$i" where I previously had just i and to modify to | cat >> ../output.txt







bash shell-script awk grep for






share|improve this question









New contributor



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










share|improve this question









New contributor



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








share|improve this question




share|improve this question








edited 17 mins ago







proteinmodels













New contributor



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








asked 50 mins ago









proteinmodelsproteinmodels

34




34




New contributor



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




New contributor




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















  • Assuming you mean to do something like grep 'bar[0-9]' "$i" | awk '{print $1}' | cat > "$i", see unix.stackexchange.com/a/425801/70524

    – muru
    41 mins ago













  • Thanks @muru. That did the job of writing to an output file, however it printed the entirety of the list.txt files to the single output, seemingly ignoring the grep and awk commands.

    – proteinmodels
    30 mins ago











  • You probably meant ... | cat > ../output.txt, or without the unnecessary cat, just ` > ../output.txt`.

    – RalfFriedl
    20 mins ago











  • Yeah now tried removing the "$i" at the end, and it worked. Thanks!

    – proteinmodels
    20 mins ago



















  • Assuming you mean to do something like grep 'bar[0-9]' "$i" | awk '{print $1}' | cat > "$i", see unix.stackexchange.com/a/425801/70524

    – muru
    41 mins ago













  • Thanks @muru. That did the job of writing to an output file, however it printed the entirety of the list.txt files to the single output, seemingly ignoring the grep and awk commands.

    – proteinmodels
    30 mins ago











  • You probably meant ... | cat > ../output.txt, or without the unnecessary cat, just ` > ../output.txt`.

    – RalfFriedl
    20 mins ago











  • Yeah now tried removing the "$i" at the end, and it worked. Thanks!

    – proteinmodels
    20 mins ago

















Assuming you mean to do something like grep 'bar[0-9]' "$i" | awk '{print $1}' | cat > "$i", see unix.stackexchange.com/a/425801/70524

– muru
41 mins ago







Assuming you mean to do something like grep 'bar[0-9]' "$i" | awk '{print $1}' | cat > "$i", see unix.stackexchange.com/a/425801/70524

– muru
41 mins ago















Thanks @muru. That did the job of writing to an output file, however it printed the entirety of the list.txt files to the single output, seemingly ignoring the grep and awk commands.

– proteinmodels
30 mins ago





Thanks @muru. That did the job of writing to an output file, however it printed the entirety of the list.txt files to the single output, seemingly ignoring the grep and awk commands.

– proteinmodels
30 mins ago













You probably meant ... | cat > ../output.txt, or without the unnecessary cat, just ` > ../output.txt`.

– RalfFriedl
20 mins ago





You probably meant ... | cat > ../output.txt, or without the unnecessary cat, just ` > ../output.txt`.

– RalfFriedl
20 mins ago













Yeah now tried removing the "$i" at the end, and it worked. Thanks!

– proteinmodels
20 mins ago





Yeah now tried removing the "$i" at the end, and it worked. Thanks!

– proteinmodels
20 mins ago










2 Answers
2






active

oldest

votes


















0














You are using i, instead of this use $i in grep command.



And you said that you want to put all of them into single file then the last command should be:



cat >> /home/ubuntu/Project/working/output.txt


Or just:



>> /home/ubuntu/Project/working/output.txt





share|improve this answer


























  • Thanks for that tip, it is now actually running and spitting something out, however it tells me that output.txt does not exist. is there a command I should be using instead of cat in order to create the output and add to it? It is also just printing the entirety of each list.txt and not the specific sections column I am after. Does that have to do with the way I have written the for i in /*/list.txt?

    – proteinmodels
    38 mins ago











  • Please edit question and provide a sample example of your files. And also first manually create output.txt then it will work.

    – Prvt_Yadv
    35 mins ago













  • Added a sample of list.txt, and the comment by @muru taught me about redirection.

    – proteinmodels
    26 mins ago



















0














Apart from correcting some small typos in your original code (using "$i" in place of i and redirecting the output to the output file rather than trying to output its contents), if you don't have many thousands of these list.txt files:



awk '/^bar[0-9]/ { print $1 }' /home/ubuntu/Project/working/library_*/Results/list.txt >output.txt


This is using awk to extract the first column of all lines that start with the string bar followed by a digit. It does this for all files matching the patten /home/ubuntu/Project/working/library_*/Results/list.txt. The extracted data is redirected to output.txt.



The loop becomes necessary when the filename globbing pattern /home/ubuntu/Project/working/library_*/Results/list.txt expands to too many names:



for pathname in /home/ubuntu/Project/working/library_*/Results/list.txt; do
awk '/^bar/ { print $1 }' "$pathname"
done >output.txt


Note that it's more efficient to redirect the output of the loop than of each individual awk call. Also note that awk easily does the job of grep to detect the wanted lines and that cat is not needed.



If you need the first column from all lines except the first (as in your example data), you can change the condition in the awk code from /^bar[0-9]/ to FNR > 1.






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
    });


    }
    });






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










    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f521236%2ffor-loop-to-iterate-over-directory-tree-extracting-results-from-files-of-the-sam%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









    0














    You are using i, instead of this use $i in grep command.



    And you said that you want to put all of them into single file then the last command should be:



    cat >> /home/ubuntu/Project/working/output.txt


    Or just:



    >> /home/ubuntu/Project/working/output.txt





    share|improve this answer


























    • Thanks for that tip, it is now actually running and spitting something out, however it tells me that output.txt does not exist. is there a command I should be using instead of cat in order to create the output and add to it? It is also just printing the entirety of each list.txt and not the specific sections column I am after. Does that have to do with the way I have written the for i in /*/list.txt?

      – proteinmodels
      38 mins ago











    • Please edit question and provide a sample example of your files. And also first manually create output.txt then it will work.

      – Prvt_Yadv
      35 mins ago













    • Added a sample of list.txt, and the comment by @muru taught me about redirection.

      – proteinmodels
      26 mins ago
















    0














    You are using i, instead of this use $i in grep command.



    And you said that you want to put all of them into single file then the last command should be:



    cat >> /home/ubuntu/Project/working/output.txt


    Or just:



    >> /home/ubuntu/Project/working/output.txt





    share|improve this answer


























    • Thanks for that tip, it is now actually running and spitting something out, however it tells me that output.txt does not exist. is there a command I should be using instead of cat in order to create the output and add to it? It is also just printing the entirety of each list.txt and not the specific sections column I am after. Does that have to do with the way I have written the for i in /*/list.txt?

      – proteinmodels
      38 mins ago











    • Please edit question and provide a sample example of your files. And also first manually create output.txt then it will work.

      – Prvt_Yadv
      35 mins ago













    • Added a sample of list.txt, and the comment by @muru taught me about redirection.

      – proteinmodels
      26 mins ago














    0












    0








    0







    You are using i, instead of this use $i in grep command.



    And you said that you want to put all of them into single file then the last command should be:



    cat >> /home/ubuntu/Project/working/output.txt


    Or just:



    >> /home/ubuntu/Project/working/output.txt





    share|improve this answer















    You are using i, instead of this use $i in grep command.



    And you said that you want to put all of them into single file then the last command should be:



    cat >> /home/ubuntu/Project/working/output.txt


    Or just:



    >> /home/ubuntu/Project/working/output.txt






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited 13 mins ago

























    answered 46 mins ago









    Prvt_YadvPrvt_Yadv

    3,47831632




    3,47831632













    • Thanks for that tip, it is now actually running and spitting something out, however it tells me that output.txt does not exist. is there a command I should be using instead of cat in order to create the output and add to it? It is also just printing the entirety of each list.txt and not the specific sections column I am after. Does that have to do with the way I have written the for i in /*/list.txt?

      – proteinmodels
      38 mins ago











    • Please edit question and provide a sample example of your files. And also first manually create output.txt then it will work.

      – Prvt_Yadv
      35 mins ago













    • Added a sample of list.txt, and the comment by @muru taught me about redirection.

      – proteinmodels
      26 mins ago



















    • Thanks for that tip, it is now actually running and spitting something out, however it tells me that output.txt does not exist. is there a command I should be using instead of cat in order to create the output and add to it? It is also just printing the entirety of each list.txt and not the specific sections column I am after. Does that have to do with the way I have written the for i in /*/list.txt?

      – proteinmodels
      38 mins ago











    • Please edit question and provide a sample example of your files. And also first manually create output.txt then it will work.

      – Prvt_Yadv
      35 mins ago













    • Added a sample of list.txt, and the comment by @muru taught me about redirection.

      – proteinmodels
      26 mins ago

















    Thanks for that tip, it is now actually running and spitting something out, however it tells me that output.txt does not exist. is there a command I should be using instead of cat in order to create the output and add to it? It is also just printing the entirety of each list.txt and not the specific sections column I am after. Does that have to do with the way I have written the for i in /*/list.txt?

    – proteinmodels
    38 mins ago





    Thanks for that tip, it is now actually running and spitting something out, however it tells me that output.txt does not exist. is there a command I should be using instead of cat in order to create the output and add to it? It is also just printing the entirety of each list.txt and not the specific sections column I am after. Does that have to do with the way I have written the for i in /*/list.txt?

    – proteinmodels
    38 mins ago













    Please edit question and provide a sample example of your files. And also first manually create output.txt then it will work.

    – Prvt_Yadv
    35 mins ago







    Please edit question and provide a sample example of your files. And also first manually create output.txt then it will work.

    – Prvt_Yadv
    35 mins ago















    Added a sample of list.txt, and the comment by @muru taught me about redirection.

    – proteinmodels
    26 mins ago





    Added a sample of list.txt, and the comment by @muru taught me about redirection.

    – proteinmodels
    26 mins ago













    0














    Apart from correcting some small typos in your original code (using "$i" in place of i and redirecting the output to the output file rather than trying to output its contents), if you don't have many thousands of these list.txt files:



    awk '/^bar[0-9]/ { print $1 }' /home/ubuntu/Project/working/library_*/Results/list.txt >output.txt


    This is using awk to extract the first column of all lines that start with the string bar followed by a digit. It does this for all files matching the patten /home/ubuntu/Project/working/library_*/Results/list.txt. The extracted data is redirected to output.txt.



    The loop becomes necessary when the filename globbing pattern /home/ubuntu/Project/working/library_*/Results/list.txt expands to too many names:



    for pathname in /home/ubuntu/Project/working/library_*/Results/list.txt; do
    awk '/^bar/ { print $1 }' "$pathname"
    done >output.txt


    Note that it's more efficient to redirect the output of the loop than of each individual awk call. Also note that awk easily does the job of grep to detect the wanted lines and that cat is not needed.



    If you need the first column from all lines except the first (as in your example data), you can change the condition in the awk code from /^bar[0-9]/ to FNR > 1.






    share|improve this answer






























      0














      Apart from correcting some small typos in your original code (using "$i" in place of i and redirecting the output to the output file rather than trying to output its contents), if you don't have many thousands of these list.txt files:



      awk '/^bar[0-9]/ { print $1 }' /home/ubuntu/Project/working/library_*/Results/list.txt >output.txt


      This is using awk to extract the first column of all lines that start with the string bar followed by a digit. It does this for all files matching the patten /home/ubuntu/Project/working/library_*/Results/list.txt. The extracted data is redirected to output.txt.



      The loop becomes necessary when the filename globbing pattern /home/ubuntu/Project/working/library_*/Results/list.txt expands to too many names:



      for pathname in /home/ubuntu/Project/working/library_*/Results/list.txt; do
      awk '/^bar/ { print $1 }' "$pathname"
      done >output.txt


      Note that it's more efficient to redirect the output of the loop than of each individual awk call. Also note that awk easily does the job of grep to detect the wanted lines and that cat is not needed.



      If you need the first column from all lines except the first (as in your example data), you can change the condition in the awk code from /^bar[0-9]/ to FNR > 1.






      share|improve this answer




























        0












        0








        0







        Apart from correcting some small typos in your original code (using "$i" in place of i and redirecting the output to the output file rather than trying to output its contents), if you don't have many thousands of these list.txt files:



        awk '/^bar[0-9]/ { print $1 }' /home/ubuntu/Project/working/library_*/Results/list.txt >output.txt


        This is using awk to extract the first column of all lines that start with the string bar followed by a digit. It does this for all files matching the patten /home/ubuntu/Project/working/library_*/Results/list.txt. The extracted data is redirected to output.txt.



        The loop becomes necessary when the filename globbing pattern /home/ubuntu/Project/working/library_*/Results/list.txt expands to too many names:



        for pathname in /home/ubuntu/Project/working/library_*/Results/list.txt; do
        awk '/^bar/ { print $1 }' "$pathname"
        done >output.txt


        Note that it's more efficient to redirect the output of the loop than of each individual awk call. Also note that awk easily does the job of grep to detect the wanted lines and that cat is not needed.



        If you need the first column from all lines except the first (as in your example data), you can change the condition in the awk code from /^bar[0-9]/ to FNR > 1.






        share|improve this answer















        Apart from correcting some small typos in your original code (using "$i" in place of i and redirecting the output to the output file rather than trying to output its contents), if you don't have many thousands of these list.txt files:



        awk '/^bar[0-9]/ { print $1 }' /home/ubuntu/Project/working/library_*/Results/list.txt >output.txt


        This is using awk to extract the first column of all lines that start with the string bar followed by a digit. It does this for all files matching the patten /home/ubuntu/Project/working/library_*/Results/list.txt. The extracted data is redirected to output.txt.



        The loop becomes necessary when the filename globbing pattern /home/ubuntu/Project/working/library_*/Results/list.txt expands to too many names:



        for pathname in /home/ubuntu/Project/working/library_*/Results/list.txt; do
        awk '/^bar/ { print $1 }' "$pathname"
        done >output.txt


        Note that it's more efficient to redirect the output of the loop than of each individual awk call. Also note that awk easily does the job of grep to detect the wanted lines and that cat is not needed.



        If you need the first column from all lines except the first (as in your example data), you can change the condition in the awk code from /^bar[0-9]/ to FNR > 1.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited 4 mins ago

























        answered 13 mins ago









        KusalanandaKusalananda

        148k18279466




        148k18279466






















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










            draft saved

            draft discarded


















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













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












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
















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


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

            But avoid



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

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


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




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f521236%2ffor-loop-to-iterate-over-directory-tree-extracting-results-from-files-of-the-sam%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

            Taj Mahal Inhaltsverzeichnis Aufbau | Geschichte | 350-Jahr-Feier | Heutige Bedeutung | Siehe auch |...

            Baia Sprie Cuprins Etimologie | Istorie | Demografie | Politică și administrație | Arii naturale...

            Nicolae Petrescu-Găină Cuprins Biografie | Opera | In memoriam | Varia | Controverse, incertitudini...