grep string from alphabetically last files in subdirectories

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












1















I need to look for the string "Total CPU time used" in a set of files that are generated in an iterative calculation within a great number of subfolders called folder_A, folder_B, folder_C and so on.



So in folder_A i would have



file_1_1
file_1_2
file_1_3
file_1_4
file_1_5
file_2_1
file_2_2
file_2_3



next to some other files with different names. In folder_B there would be



file_1_1
file_1_2
file_1_3
file_1_4
file_1_5
file_2_1
file_2_2
file_2_3
file_2_4
file_2_5
file_3_1



and so on, so every subfolder would contain a different amount of iterative steps and thus a different number appending the last file. I think the way to go is using recursive grep sorting out the alphabetically last file, the code I've tried is:



grep -r "Total CPU time used" */file_* | tail -1



However this only gives me an output of the last file in the last directory folder_Z. How do I grep the string from all subdirectories so that folder_A/file_2_3, folder_B/file_3_1 and so on are not skipped?










share|improve this question






















  • I think your question hints at a for loop... if it works, then yes that's what I need.

    – Andreas
    Jan 7 at 13:30











  • There are other files present that are named differently. I'm only interested in the files specified in the post.

    – Andreas
    Jan 7 at 13:41











  • OK but then, do you know that the files are sorted by default in lexicographical order so file_1_10 will sort before file_1_2 ? Do you want the last one in lexicographical order (e.g. file_1_2)or do you want the last one as in "sorted by version" (file_1_10) ?

    – don_crissti
    Jan 7 at 13:47












  • Well the counting always stops at file_1_5 and resumes with file_2_1, so this should be no problem. But what I need would be file_1_10.

    – Andreas
    Jan 7 at 13:54











  • In that case (file names contain only numbers between 1 and 9) you already have an answer that works.

    – don_crissti
    Jan 7 at 13:57















1















I need to look for the string "Total CPU time used" in a set of files that are generated in an iterative calculation within a great number of subfolders called folder_A, folder_B, folder_C and so on.



So in folder_A i would have



file_1_1
file_1_2
file_1_3
file_1_4
file_1_5
file_2_1
file_2_2
file_2_3



next to some other files with different names. In folder_B there would be



file_1_1
file_1_2
file_1_3
file_1_4
file_1_5
file_2_1
file_2_2
file_2_3
file_2_4
file_2_5
file_3_1



and so on, so every subfolder would contain a different amount of iterative steps and thus a different number appending the last file. I think the way to go is using recursive grep sorting out the alphabetically last file, the code I've tried is:



grep -r "Total CPU time used" */file_* | tail -1



However this only gives me an output of the last file in the last directory folder_Z. How do I grep the string from all subdirectories so that folder_A/file_2_3, folder_B/file_3_1 and so on are not skipped?










share|improve this question






















  • I think your question hints at a for loop... if it works, then yes that's what I need.

    – Andreas
    Jan 7 at 13:30











  • There are other files present that are named differently. I'm only interested in the files specified in the post.

    – Andreas
    Jan 7 at 13:41











  • OK but then, do you know that the files are sorted by default in lexicographical order so file_1_10 will sort before file_1_2 ? Do you want the last one in lexicographical order (e.g. file_1_2)or do you want the last one as in "sorted by version" (file_1_10) ?

    – don_crissti
    Jan 7 at 13:47












  • Well the counting always stops at file_1_5 and resumes with file_2_1, so this should be no problem. But what I need would be file_1_10.

    – Andreas
    Jan 7 at 13:54











  • In that case (file names contain only numbers between 1 and 9) you already have an answer that works.

    – don_crissti
    Jan 7 at 13:57













1












1








1








I need to look for the string "Total CPU time used" in a set of files that are generated in an iterative calculation within a great number of subfolders called folder_A, folder_B, folder_C and so on.



So in folder_A i would have



file_1_1
file_1_2
file_1_3
file_1_4
file_1_5
file_2_1
file_2_2
file_2_3



next to some other files with different names. In folder_B there would be



file_1_1
file_1_2
file_1_3
file_1_4
file_1_5
file_2_1
file_2_2
file_2_3
file_2_4
file_2_5
file_3_1



and so on, so every subfolder would contain a different amount of iterative steps and thus a different number appending the last file. I think the way to go is using recursive grep sorting out the alphabetically last file, the code I've tried is:



grep -r "Total CPU time used" */file_* | tail -1



However this only gives me an output of the last file in the last directory folder_Z. How do I grep the string from all subdirectories so that folder_A/file_2_3, folder_B/file_3_1 and so on are not skipped?










share|improve this question














I need to look for the string "Total CPU time used" in a set of files that are generated in an iterative calculation within a great number of subfolders called folder_A, folder_B, folder_C and so on.



So in folder_A i would have



file_1_1
file_1_2
file_1_3
file_1_4
file_1_5
file_2_1
file_2_2
file_2_3



next to some other files with different names. In folder_B there would be



file_1_1
file_1_2
file_1_3
file_1_4
file_1_5
file_2_1
file_2_2
file_2_3
file_2_4
file_2_5
file_3_1



and so on, so every subfolder would contain a different amount of iterative steps and thus a different number appending the last file. I think the way to go is using recursive grep sorting out the alphabetically last file, the code I've tried is:



grep -r "Total CPU time used" */file_* | tail -1



However this only gives me an output of the last file in the last directory folder_Z. How do I grep the string from all subdirectories so that folder_A/file_2_3, folder_B/file_3_1 and so on are not skipped?







grep






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 7 at 13:19









AndreasAndreas

223




223












  • I think your question hints at a for loop... if it works, then yes that's what I need.

    – Andreas
    Jan 7 at 13:30











  • There are other files present that are named differently. I'm only interested in the files specified in the post.

    – Andreas
    Jan 7 at 13:41











  • OK but then, do you know that the files are sorted by default in lexicographical order so file_1_10 will sort before file_1_2 ? Do you want the last one in lexicographical order (e.g. file_1_2)or do you want the last one as in "sorted by version" (file_1_10) ?

    – don_crissti
    Jan 7 at 13:47












  • Well the counting always stops at file_1_5 and resumes with file_2_1, so this should be no problem. But what I need would be file_1_10.

    – Andreas
    Jan 7 at 13:54











  • In that case (file names contain only numbers between 1 and 9) you already have an answer that works.

    – don_crissti
    Jan 7 at 13:57

















  • I think your question hints at a for loop... if it works, then yes that's what I need.

    – Andreas
    Jan 7 at 13:30











  • There are other files present that are named differently. I'm only interested in the files specified in the post.

    – Andreas
    Jan 7 at 13:41











  • OK but then, do you know that the files are sorted by default in lexicographical order so file_1_10 will sort before file_1_2 ? Do you want the last one in lexicographical order (e.g. file_1_2)or do you want the last one as in "sorted by version" (file_1_10) ?

    – don_crissti
    Jan 7 at 13:47












  • Well the counting always stops at file_1_5 and resumes with file_2_1, so this should be no problem. But what I need would be file_1_10.

    – Andreas
    Jan 7 at 13:54











  • In that case (file names contain only numbers between 1 and 9) you already have an answer that works.

    – don_crissti
    Jan 7 at 13:57
















I think your question hints at a for loop... if it works, then yes that's what I need.

– Andreas
Jan 7 at 13:30





I think your question hints at a for loop... if it works, then yes that's what I need.

– Andreas
Jan 7 at 13:30













There are other files present that are named differently. I'm only interested in the files specified in the post.

– Andreas
Jan 7 at 13:41





There are other files present that are named differently. I'm only interested in the files specified in the post.

– Andreas
Jan 7 at 13:41













OK but then, do you know that the files are sorted by default in lexicographical order so file_1_10 will sort before file_1_2 ? Do you want the last one in lexicographical order (e.g. file_1_2)or do you want the last one as in "sorted by version" (file_1_10) ?

– don_crissti
Jan 7 at 13:47






OK but then, do you know that the files are sorted by default in lexicographical order so file_1_10 will sort before file_1_2 ? Do you want the last one in lexicographical order (e.g. file_1_2)or do you want the last one as in "sorted by version" (file_1_10) ?

– don_crissti
Jan 7 at 13:47














Well the counting always stops at file_1_5 and resumes with file_2_1, so this should be no problem. But what I need would be file_1_10.

– Andreas
Jan 7 at 13:54





Well the counting always stops at file_1_5 and resumes with file_2_1, so this should be no problem. But what I need would be file_1_10.

– Andreas
Jan 7 at 13:54













In that case (file names contain only numbers between 1 and 9) you already have an answer that works.

– don_crissti
Jan 7 at 13:57





In that case (file names contain only numbers between 1 and 9) you already have an answer that works.

– don_crissti
Jan 7 at 13:57










1 Answer
1






active

oldest

votes


















3














It's the tail -1 that gives you the last line of the result of the grep call. This likely comes from a match in the last file in the last directory.



Instead, you will need to loop over the directories. This is using bash:



for dir in folder_*/; do
files=( "$dir"/file_* )
grep -F 'Total CPU time used' "$files[-1]"
done


This would iterate over the directories. For each directory, the last file (in the dictionary order sense) is grepped for the string that you are searching for.



I'm using -F with grep as I'm looking for a fixed string and not a regular expression.



If you want to additionally get the filename of the file in the grep output, then either tag on /dev/null as a last argument to grep (grep will include the filename when matching across more than one file operand), or use grep with -H, if your grep supports it.






share|improve this answer























  • The last paragraph is especially helpful!

    – Andreas
    Jan 7 at 14:02










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%2f492993%2fgrep-string-from-alphabetically-last-files-in-subdirectories%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














It's the tail -1 that gives you the last line of the result of the grep call. This likely comes from a match in the last file in the last directory.



Instead, you will need to loop over the directories. This is using bash:



for dir in folder_*/; do
files=( "$dir"/file_* )
grep -F 'Total CPU time used' "$files[-1]"
done


This would iterate over the directories. For each directory, the last file (in the dictionary order sense) is grepped for the string that you are searching for.



I'm using -F with grep as I'm looking for a fixed string and not a regular expression.



If you want to additionally get the filename of the file in the grep output, then either tag on /dev/null as a last argument to grep (grep will include the filename when matching across more than one file operand), or use grep with -H, if your grep supports it.






share|improve this answer























  • The last paragraph is especially helpful!

    – Andreas
    Jan 7 at 14:02















3














It's the tail -1 that gives you the last line of the result of the grep call. This likely comes from a match in the last file in the last directory.



Instead, you will need to loop over the directories. This is using bash:



for dir in folder_*/; do
files=( "$dir"/file_* )
grep -F 'Total CPU time used' "$files[-1]"
done


This would iterate over the directories. For each directory, the last file (in the dictionary order sense) is grepped for the string that you are searching for.



I'm using -F with grep as I'm looking for a fixed string and not a regular expression.



If you want to additionally get the filename of the file in the grep output, then either tag on /dev/null as a last argument to grep (grep will include the filename when matching across more than one file operand), or use grep with -H, if your grep supports it.






share|improve this answer























  • The last paragraph is especially helpful!

    – Andreas
    Jan 7 at 14:02













3












3








3







It's the tail -1 that gives you the last line of the result of the grep call. This likely comes from a match in the last file in the last directory.



Instead, you will need to loop over the directories. This is using bash:



for dir in folder_*/; do
files=( "$dir"/file_* )
grep -F 'Total CPU time used' "$files[-1]"
done


This would iterate over the directories. For each directory, the last file (in the dictionary order sense) is grepped for the string that you are searching for.



I'm using -F with grep as I'm looking for a fixed string and not a regular expression.



If you want to additionally get the filename of the file in the grep output, then either tag on /dev/null as a last argument to grep (grep will include the filename when matching across more than one file operand), or use grep with -H, if your grep supports it.






share|improve this answer













It's the tail -1 that gives you the last line of the result of the grep call. This likely comes from a match in the last file in the last directory.



Instead, you will need to loop over the directories. This is using bash:



for dir in folder_*/; do
files=( "$dir"/file_* )
grep -F 'Total CPU time used' "$files[-1]"
done


This would iterate over the directories. For each directory, the last file (in the dictionary order sense) is grepped for the string that you are searching for.



I'm using -F with grep as I'm looking for a fixed string and not a regular expression.



If you want to additionally get the filename of the file in the grep output, then either tag on /dev/null as a last argument to grep (grep will include the filename when matching across more than one file operand), or use grep with -H, if your grep supports it.







share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 7 at 13:33









KusalanandaKusalananda

125k16236389




125k16236389












  • The last paragraph is especially helpful!

    – Andreas
    Jan 7 at 14:02

















  • The last paragraph is especially helpful!

    – Andreas
    Jan 7 at 14:02
















The last paragraph is especially helpful!

– Andreas
Jan 7 at 14:02





The last paragraph is especially helpful!

– Andreas
Jan 7 at 14:02

















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%2f492993%2fgrep-string-from-alphabetically-last-files-in-subdirectories%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?