grep can't get the word I need, but Linux continues to do the next job. How do I stop it?

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












1















I have quite a few txt files. They contain the word 'Minimum' (it only shows once in each file), and I want to get the line that contains this word, and show the line in a new file.

To do this, I use this command:



grep 'Minimum' file1.txt > new1.txt 


I did this to each file (file1, file2, etc.), and got lots of newn.txt. But it turns out that some files don't have the word 'Minimum', so their newn.txt are empty. Well, if grep can't get the word, why doesn't Linux give me an error message? Why does it continue and make the newn.txt? Giving me some empty files is really annoying.

So is there a way that I can stop it from creating the newn.txt if it can't find the word?



Also, I'm thinking about putting it into a bash script - the grep command is the first step, and then in the second step I'll do something to the newn.txt. So if grep can't find the word, or if there's an empty file created, I want the script to stop and not to do the second step. Is there a way to do this?










share|improve this question
























  • grep isn't going to give an error just because the pattern isn't found. It doesn't work in that way. If you have a script and you want it to stop after a certain condition then you need to add its contents to your question.

    – Nasir Riley
    Mar 3 at 23:33












  • @NasirRiley The content of the second step or further steps is not the point. The point is that I want to tell Linux to stop from doing the second step if the first step is like that.

    – LittleG
    Mar 3 at 23:52











  • You asked why there isn't an error message when the pattern isn't matched and I told you why. The fact that it doesn't stop with an error when you want it to is irrelevant. As I said before, if you want your script to stop when the pattern isn't found then it is up to you to alter the code. It is not up to the operating system or the shell or the binary that you are using to do something that it isn't coded to do or to give an error message when there isn't an error. Add your script to you question and we can help you alter it to get the output that you want.

    – Nasir Riley
    Mar 4 at 0:32












  • Actually, when you use the > redirection operator, creating (or opening, if it already exists) the file is the first step, done by the shell before executing the grep command

    – steeldriver
    Mar 4 at 0:36












  • @steeldriver Yeah that's right. I've just realised that.

    – LittleG
    Mar 4 at 0:43















1















I have quite a few txt files. They contain the word 'Minimum' (it only shows once in each file), and I want to get the line that contains this word, and show the line in a new file.

To do this, I use this command:



grep 'Minimum' file1.txt > new1.txt 


I did this to each file (file1, file2, etc.), and got lots of newn.txt. But it turns out that some files don't have the word 'Minimum', so their newn.txt are empty. Well, if grep can't get the word, why doesn't Linux give me an error message? Why does it continue and make the newn.txt? Giving me some empty files is really annoying.

So is there a way that I can stop it from creating the newn.txt if it can't find the word?



Also, I'm thinking about putting it into a bash script - the grep command is the first step, and then in the second step I'll do something to the newn.txt. So if grep can't find the word, or if there's an empty file created, I want the script to stop and not to do the second step. Is there a way to do this?










share|improve this question
























  • grep isn't going to give an error just because the pattern isn't found. It doesn't work in that way. If you have a script and you want it to stop after a certain condition then you need to add its contents to your question.

    – Nasir Riley
    Mar 3 at 23:33












  • @NasirRiley The content of the second step or further steps is not the point. The point is that I want to tell Linux to stop from doing the second step if the first step is like that.

    – LittleG
    Mar 3 at 23:52











  • You asked why there isn't an error message when the pattern isn't matched and I told you why. The fact that it doesn't stop with an error when you want it to is irrelevant. As I said before, if you want your script to stop when the pattern isn't found then it is up to you to alter the code. It is not up to the operating system or the shell or the binary that you are using to do something that it isn't coded to do or to give an error message when there isn't an error. Add your script to you question and we can help you alter it to get the output that you want.

    – Nasir Riley
    Mar 4 at 0:32












  • Actually, when you use the > redirection operator, creating (or opening, if it already exists) the file is the first step, done by the shell before executing the grep command

    – steeldriver
    Mar 4 at 0:36












  • @steeldriver Yeah that's right. I've just realised that.

    – LittleG
    Mar 4 at 0:43













1












1








1








I have quite a few txt files. They contain the word 'Minimum' (it only shows once in each file), and I want to get the line that contains this word, and show the line in a new file.

To do this, I use this command:



grep 'Minimum' file1.txt > new1.txt 


I did this to each file (file1, file2, etc.), and got lots of newn.txt. But it turns out that some files don't have the word 'Minimum', so their newn.txt are empty. Well, if grep can't get the word, why doesn't Linux give me an error message? Why does it continue and make the newn.txt? Giving me some empty files is really annoying.

So is there a way that I can stop it from creating the newn.txt if it can't find the word?



Also, I'm thinking about putting it into a bash script - the grep command is the first step, and then in the second step I'll do something to the newn.txt. So if grep can't find the word, or if there's an empty file created, I want the script to stop and not to do the second step. Is there a way to do this?










share|improve this question
















I have quite a few txt files. They contain the word 'Minimum' (it only shows once in each file), and I want to get the line that contains this word, and show the line in a new file.

To do this, I use this command:



grep 'Minimum' file1.txt > new1.txt 


I did this to each file (file1, file2, etc.), and got lots of newn.txt. But it turns out that some files don't have the word 'Minimum', so their newn.txt are empty. Well, if grep can't get the word, why doesn't Linux give me an error message? Why does it continue and make the newn.txt? Giving me some empty files is really annoying.

So is there a way that I can stop it from creating the newn.txt if it can't find the word?



Also, I'm thinking about putting it into a bash script - the grep command is the first step, and then in the second step I'll do something to the newn.txt. So if grep can't find the word, or if there's an empty file created, I want the script to stop and not to do the second step. Is there a way to do this?







linux bash shell-script text-processing grep






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 3 at 23:36









Tomasz

10.2k53168




10.2k53168










asked Mar 3 at 22:49









LittleGLittleG

206




206












  • grep isn't going to give an error just because the pattern isn't found. It doesn't work in that way. If you have a script and you want it to stop after a certain condition then you need to add its contents to your question.

    – Nasir Riley
    Mar 3 at 23:33












  • @NasirRiley The content of the second step or further steps is not the point. The point is that I want to tell Linux to stop from doing the second step if the first step is like that.

    – LittleG
    Mar 3 at 23:52











  • You asked why there isn't an error message when the pattern isn't matched and I told you why. The fact that it doesn't stop with an error when you want it to is irrelevant. As I said before, if you want your script to stop when the pattern isn't found then it is up to you to alter the code. It is not up to the operating system or the shell or the binary that you are using to do something that it isn't coded to do or to give an error message when there isn't an error. Add your script to you question and we can help you alter it to get the output that you want.

    – Nasir Riley
    Mar 4 at 0:32












  • Actually, when you use the > redirection operator, creating (or opening, if it already exists) the file is the first step, done by the shell before executing the grep command

    – steeldriver
    Mar 4 at 0:36












  • @steeldriver Yeah that's right. I've just realised that.

    – LittleG
    Mar 4 at 0:43

















  • grep isn't going to give an error just because the pattern isn't found. It doesn't work in that way. If you have a script and you want it to stop after a certain condition then you need to add its contents to your question.

    – Nasir Riley
    Mar 3 at 23:33












  • @NasirRiley The content of the second step or further steps is not the point. The point is that I want to tell Linux to stop from doing the second step if the first step is like that.

    – LittleG
    Mar 3 at 23:52











  • You asked why there isn't an error message when the pattern isn't matched and I told you why. The fact that it doesn't stop with an error when you want it to is irrelevant. As I said before, if you want your script to stop when the pattern isn't found then it is up to you to alter the code. It is not up to the operating system or the shell or the binary that you are using to do something that it isn't coded to do or to give an error message when there isn't an error. Add your script to you question and we can help you alter it to get the output that you want.

    – Nasir Riley
    Mar 4 at 0:32












  • Actually, when you use the > redirection operator, creating (or opening, if it already exists) the file is the first step, done by the shell before executing the grep command

    – steeldriver
    Mar 4 at 0:36












  • @steeldriver Yeah that's right. I've just realised that.

    – LittleG
    Mar 4 at 0:43
















grep isn't going to give an error just because the pattern isn't found. It doesn't work in that way. If you have a script and you want it to stop after a certain condition then you need to add its contents to your question.

– Nasir Riley
Mar 3 at 23:33






grep isn't going to give an error just because the pattern isn't found. It doesn't work in that way. If you have a script and you want it to stop after a certain condition then you need to add its contents to your question.

– Nasir Riley
Mar 3 at 23:33














@NasirRiley The content of the second step or further steps is not the point. The point is that I want to tell Linux to stop from doing the second step if the first step is like that.

– LittleG
Mar 3 at 23:52





@NasirRiley The content of the second step or further steps is not the point. The point is that I want to tell Linux to stop from doing the second step if the first step is like that.

– LittleG
Mar 3 at 23:52













You asked why there isn't an error message when the pattern isn't matched and I told you why. The fact that it doesn't stop with an error when you want it to is irrelevant. As I said before, if you want your script to stop when the pattern isn't found then it is up to you to alter the code. It is not up to the operating system or the shell or the binary that you are using to do something that it isn't coded to do or to give an error message when there isn't an error. Add your script to you question and we can help you alter it to get the output that you want.

– Nasir Riley
Mar 4 at 0:32






You asked why there isn't an error message when the pattern isn't matched and I told you why. The fact that it doesn't stop with an error when you want it to is irrelevant. As I said before, if you want your script to stop when the pattern isn't found then it is up to you to alter the code. It is not up to the operating system or the shell or the binary that you are using to do something that it isn't coded to do or to give an error message when there isn't an error. Add your script to you question and we can help you alter it to get the output that you want.

– Nasir Riley
Mar 4 at 0:32














Actually, when you use the > redirection operator, creating (or opening, if it already exists) the file is the first step, done by the shell before executing the grep command

– steeldriver
Mar 4 at 0:36






Actually, when you use the > redirection operator, creating (or opening, if it already exists) the file is the first step, done by the shell before executing the grep command

– steeldriver
Mar 4 at 0:36














@steeldriver Yeah that's right. I've just realised that.

– LittleG
Mar 4 at 0:43





@steeldriver Yeah that's right. I've just realised that.

– LittleG
Mar 4 at 0:43










3 Answers
3






active

oldest

votes


















1














The shell will create the output file before even running grep. The grep utility will then add contents to the file, if there is anything to add. If grep does not find anything, the file will be left empty.



Another thing that happens if grep does not find anything matching in the input file is that it will exit with a non-zero exit status, signalling "failure". This does not remove the output file, but you could act on the failure to remove the file like this:



grep 'PATTERN' infile >outfile || rm -f outfile


If grep fails to match PATTERN in infile, this would call rm -f to remove the empty outfile. Strictly speaking, this would also try to remove the file if grep failed for any reason (like e.g. not being able to read its input), or if the output file could not be created.



If you additionally want to terminate the script if no matches were found:



if ! grep 'PATTERN' infile >outfile; then
rm -f outfile
exit
fi





share|improve this answer

























  • Unfortunately it doesn't work here. I tried this command grep 'PATTERN' infile >outfile || rm -f outfile but it doesn't stop and still moves to the next step of my code. My next step is using awk to get a column from the outfile and then put it into a new file. Although grep didn't get the word in infile, awk still got an empty column and created an empty new file.

    – LittleG
    Mar 4 at 17:14











  • @LittleG You haven't shown us any part of your script other than the single grep command. We don't know what the rest of your code does or what it looks like. The only thing you said you had an issue with was the creation of the output file from the grep command. If you could edit your question to add a bit more code, then it would be easier for us to see what your code does and how it could be changed. Also, using awk and grep together is seldom needed as awk can do everything grep can do and more.

    – Kusalananda
    Mar 4 at 17:19












  • Hiya, I said that in my question - my last sentence reads 'So if grep can't find the word, or if there's an empty file created, I want the script to stop and not to do the second step. Is there a way to do this?' I want it to stop and not start the second step.

    – LittleG
    Mar 4 at 17:22











  • @LittleG See updated answer.

    – Kusalananda
    Mar 4 at 17:24











  • Thanks. What does the ! mean by the way? I've never seen any exclamation mark after the if

    – LittleG
    Mar 4 at 17:28


















0














Try this loop. Adjust it to your needs.



i=0
for file in *; do
line=""
line="$(grep -hm1 'Minimum' "$file")"
[ -z "$line" ] || echo "$line" > new"$i".txt
((i++))
done


Change * in this line: for file in *; do to your file set.



Take care to weed out any unwanted grep output that might crop up.






share|improve this answer

























  • The loop variable "file" appears to be unused within the loop's body, is that intended?

    – dhag
    Mar 4 at 0:02


















0














for i in file*; do grep -q Minimum "$i" && grep Minimum "$i" > "$i/file/new"; done



  • grep -q Minimum "$i" immediately exists after the first "Minimum" is found

  • the second grep does the real job and is not executed if the first one fails


  • "$i/file/new" renames file in each filename to new





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%2f504162%2fgrep-cant-get-the-word-i-need-but-linux-continues-to-do-the-next-job-how-do-i%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    The shell will create the output file before even running grep. The grep utility will then add contents to the file, if there is anything to add. If grep does not find anything, the file will be left empty.



    Another thing that happens if grep does not find anything matching in the input file is that it will exit with a non-zero exit status, signalling "failure". This does not remove the output file, but you could act on the failure to remove the file like this:



    grep 'PATTERN' infile >outfile || rm -f outfile


    If grep fails to match PATTERN in infile, this would call rm -f to remove the empty outfile. Strictly speaking, this would also try to remove the file if grep failed for any reason (like e.g. not being able to read its input), or if the output file could not be created.



    If you additionally want to terminate the script if no matches were found:



    if ! grep 'PATTERN' infile >outfile; then
    rm -f outfile
    exit
    fi





    share|improve this answer

























    • Unfortunately it doesn't work here. I tried this command grep 'PATTERN' infile >outfile || rm -f outfile but it doesn't stop and still moves to the next step of my code. My next step is using awk to get a column from the outfile and then put it into a new file. Although grep didn't get the word in infile, awk still got an empty column and created an empty new file.

      – LittleG
      Mar 4 at 17:14











    • @LittleG You haven't shown us any part of your script other than the single grep command. We don't know what the rest of your code does or what it looks like. The only thing you said you had an issue with was the creation of the output file from the grep command. If you could edit your question to add a bit more code, then it would be easier for us to see what your code does and how it could be changed. Also, using awk and grep together is seldom needed as awk can do everything grep can do and more.

      – Kusalananda
      Mar 4 at 17:19












    • Hiya, I said that in my question - my last sentence reads 'So if grep can't find the word, or if there's an empty file created, I want the script to stop and not to do the second step. Is there a way to do this?' I want it to stop and not start the second step.

      – LittleG
      Mar 4 at 17:22











    • @LittleG See updated answer.

      – Kusalananda
      Mar 4 at 17:24











    • Thanks. What does the ! mean by the way? I've never seen any exclamation mark after the if

      – LittleG
      Mar 4 at 17:28















    1














    The shell will create the output file before even running grep. The grep utility will then add contents to the file, if there is anything to add. If grep does not find anything, the file will be left empty.



    Another thing that happens if grep does not find anything matching in the input file is that it will exit with a non-zero exit status, signalling "failure". This does not remove the output file, but you could act on the failure to remove the file like this:



    grep 'PATTERN' infile >outfile || rm -f outfile


    If grep fails to match PATTERN in infile, this would call rm -f to remove the empty outfile. Strictly speaking, this would also try to remove the file if grep failed for any reason (like e.g. not being able to read its input), or if the output file could not be created.



    If you additionally want to terminate the script if no matches were found:



    if ! grep 'PATTERN' infile >outfile; then
    rm -f outfile
    exit
    fi





    share|improve this answer

























    • Unfortunately it doesn't work here. I tried this command grep 'PATTERN' infile >outfile || rm -f outfile but it doesn't stop and still moves to the next step of my code. My next step is using awk to get a column from the outfile and then put it into a new file. Although grep didn't get the word in infile, awk still got an empty column and created an empty new file.

      – LittleG
      Mar 4 at 17:14











    • @LittleG You haven't shown us any part of your script other than the single grep command. We don't know what the rest of your code does or what it looks like. The only thing you said you had an issue with was the creation of the output file from the grep command. If you could edit your question to add a bit more code, then it would be easier for us to see what your code does and how it could be changed. Also, using awk and grep together is seldom needed as awk can do everything grep can do and more.

      – Kusalananda
      Mar 4 at 17:19












    • Hiya, I said that in my question - my last sentence reads 'So if grep can't find the word, or if there's an empty file created, I want the script to stop and not to do the second step. Is there a way to do this?' I want it to stop and not start the second step.

      – LittleG
      Mar 4 at 17:22











    • @LittleG See updated answer.

      – Kusalananda
      Mar 4 at 17:24











    • Thanks. What does the ! mean by the way? I've never seen any exclamation mark after the if

      – LittleG
      Mar 4 at 17:28













    1












    1








    1







    The shell will create the output file before even running grep. The grep utility will then add contents to the file, if there is anything to add. If grep does not find anything, the file will be left empty.



    Another thing that happens if grep does not find anything matching in the input file is that it will exit with a non-zero exit status, signalling "failure". This does not remove the output file, but you could act on the failure to remove the file like this:



    grep 'PATTERN' infile >outfile || rm -f outfile


    If grep fails to match PATTERN in infile, this would call rm -f to remove the empty outfile. Strictly speaking, this would also try to remove the file if grep failed for any reason (like e.g. not being able to read its input), or if the output file could not be created.



    If you additionally want to terminate the script if no matches were found:



    if ! grep 'PATTERN' infile >outfile; then
    rm -f outfile
    exit
    fi





    share|improve this answer















    The shell will create the output file before even running grep. The grep utility will then add contents to the file, if there is anything to add. If grep does not find anything, the file will be left empty.



    Another thing that happens if grep does not find anything matching in the input file is that it will exit with a non-zero exit status, signalling "failure". This does not remove the output file, but you could act on the failure to remove the file like this:



    grep 'PATTERN' infile >outfile || rm -f outfile


    If grep fails to match PATTERN in infile, this would call rm -f to remove the empty outfile. Strictly speaking, this would also try to remove the file if grep failed for any reason (like e.g. not being able to read its input), or if the output file could not be created.



    If you additionally want to terminate the script if no matches were found:



    if ! grep 'PATTERN' infile >outfile; then
    rm -f outfile
    exit
    fi






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Mar 4 at 17:24

























    answered Mar 4 at 8:07









    KusalanandaKusalananda

    139k17259429




    139k17259429












    • Unfortunately it doesn't work here. I tried this command grep 'PATTERN' infile >outfile || rm -f outfile but it doesn't stop and still moves to the next step of my code. My next step is using awk to get a column from the outfile and then put it into a new file. Although grep didn't get the word in infile, awk still got an empty column and created an empty new file.

      – LittleG
      Mar 4 at 17:14











    • @LittleG You haven't shown us any part of your script other than the single grep command. We don't know what the rest of your code does or what it looks like. The only thing you said you had an issue with was the creation of the output file from the grep command. If you could edit your question to add a bit more code, then it would be easier for us to see what your code does and how it could be changed. Also, using awk and grep together is seldom needed as awk can do everything grep can do and more.

      – Kusalananda
      Mar 4 at 17:19












    • Hiya, I said that in my question - my last sentence reads 'So if grep can't find the word, or if there's an empty file created, I want the script to stop and not to do the second step. Is there a way to do this?' I want it to stop and not start the second step.

      – LittleG
      Mar 4 at 17:22











    • @LittleG See updated answer.

      – Kusalananda
      Mar 4 at 17:24











    • Thanks. What does the ! mean by the way? I've never seen any exclamation mark after the if

      – LittleG
      Mar 4 at 17:28

















    • Unfortunately it doesn't work here. I tried this command grep 'PATTERN' infile >outfile || rm -f outfile but it doesn't stop and still moves to the next step of my code. My next step is using awk to get a column from the outfile and then put it into a new file. Although grep didn't get the word in infile, awk still got an empty column and created an empty new file.

      – LittleG
      Mar 4 at 17:14











    • @LittleG You haven't shown us any part of your script other than the single grep command. We don't know what the rest of your code does or what it looks like. The only thing you said you had an issue with was the creation of the output file from the grep command. If you could edit your question to add a bit more code, then it would be easier for us to see what your code does and how it could be changed. Also, using awk and grep together is seldom needed as awk can do everything grep can do and more.

      – Kusalananda
      Mar 4 at 17:19












    • Hiya, I said that in my question - my last sentence reads 'So if grep can't find the word, or if there's an empty file created, I want the script to stop and not to do the second step. Is there a way to do this?' I want it to stop and not start the second step.

      – LittleG
      Mar 4 at 17:22











    • @LittleG See updated answer.

      – Kusalananda
      Mar 4 at 17:24











    • Thanks. What does the ! mean by the way? I've never seen any exclamation mark after the if

      – LittleG
      Mar 4 at 17:28
















    Unfortunately it doesn't work here. I tried this command grep 'PATTERN' infile >outfile || rm -f outfile but it doesn't stop and still moves to the next step of my code. My next step is using awk to get a column from the outfile and then put it into a new file. Although grep didn't get the word in infile, awk still got an empty column and created an empty new file.

    – LittleG
    Mar 4 at 17:14





    Unfortunately it doesn't work here. I tried this command grep 'PATTERN' infile >outfile || rm -f outfile but it doesn't stop and still moves to the next step of my code. My next step is using awk to get a column from the outfile and then put it into a new file. Although grep didn't get the word in infile, awk still got an empty column and created an empty new file.

    – LittleG
    Mar 4 at 17:14













    @LittleG You haven't shown us any part of your script other than the single grep command. We don't know what the rest of your code does or what it looks like. The only thing you said you had an issue with was the creation of the output file from the grep command. If you could edit your question to add a bit more code, then it would be easier for us to see what your code does and how it could be changed. Also, using awk and grep together is seldom needed as awk can do everything grep can do and more.

    – Kusalananda
    Mar 4 at 17:19






    @LittleG You haven't shown us any part of your script other than the single grep command. We don't know what the rest of your code does or what it looks like. The only thing you said you had an issue with was the creation of the output file from the grep command. If you could edit your question to add a bit more code, then it would be easier for us to see what your code does and how it could be changed. Also, using awk and grep together is seldom needed as awk can do everything grep can do and more.

    – Kusalananda
    Mar 4 at 17:19














    Hiya, I said that in my question - my last sentence reads 'So if grep can't find the word, or if there's an empty file created, I want the script to stop and not to do the second step. Is there a way to do this?' I want it to stop and not start the second step.

    – LittleG
    Mar 4 at 17:22





    Hiya, I said that in my question - my last sentence reads 'So if grep can't find the word, or if there's an empty file created, I want the script to stop and not to do the second step. Is there a way to do this?' I want it to stop and not start the second step.

    – LittleG
    Mar 4 at 17:22













    @LittleG See updated answer.

    – Kusalananda
    Mar 4 at 17:24





    @LittleG See updated answer.

    – Kusalananda
    Mar 4 at 17:24













    Thanks. What does the ! mean by the way? I've never seen any exclamation mark after the if

    – LittleG
    Mar 4 at 17:28





    Thanks. What does the ! mean by the way? I've never seen any exclamation mark after the if

    – LittleG
    Mar 4 at 17:28













    0














    Try this loop. Adjust it to your needs.



    i=0
    for file in *; do
    line=""
    line="$(grep -hm1 'Minimum' "$file")"
    [ -z "$line" ] || echo "$line" > new"$i".txt
    ((i++))
    done


    Change * in this line: for file in *; do to your file set.



    Take care to weed out any unwanted grep output that might crop up.






    share|improve this answer

























    • The loop variable "file" appears to be unused within the loop's body, is that intended?

      – dhag
      Mar 4 at 0:02















    0














    Try this loop. Adjust it to your needs.



    i=0
    for file in *; do
    line=""
    line="$(grep -hm1 'Minimum' "$file")"
    [ -z "$line" ] || echo "$line" > new"$i".txt
    ((i++))
    done


    Change * in this line: for file in *; do to your file set.



    Take care to weed out any unwanted grep output that might crop up.






    share|improve this answer

























    • The loop variable "file" appears to be unused within the loop's body, is that intended?

      – dhag
      Mar 4 at 0:02













    0












    0








    0







    Try this loop. Adjust it to your needs.



    i=0
    for file in *; do
    line=""
    line="$(grep -hm1 'Minimum' "$file")"
    [ -z "$line" ] || echo "$line" > new"$i".txt
    ((i++))
    done


    Change * in this line: for file in *; do to your file set.



    Take care to weed out any unwanted grep output that might crop up.






    share|improve this answer















    Try this loop. Adjust it to your needs.



    i=0
    for file in *; do
    line=""
    line="$(grep -hm1 'Minimum' "$file")"
    [ -z "$line" ] || echo "$line" > new"$i".txt
    ((i++))
    done


    Change * in this line: for file in *; do to your file set.



    Take care to weed out any unwanted grep output that might crop up.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Mar 4 at 0:04

























    answered Mar 3 at 23:53









    TomaszTomasz

    10.2k53168




    10.2k53168












    • The loop variable "file" appears to be unused within the loop's body, is that intended?

      – dhag
      Mar 4 at 0:02

















    • The loop variable "file" appears to be unused within the loop's body, is that intended?

      – dhag
      Mar 4 at 0:02
















    The loop variable "file" appears to be unused within the loop's body, is that intended?

    – dhag
    Mar 4 at 0:02





    The loop variable "file" appears to be unused within the loop's body, is that intended?

    – dhag
    Mar 4 at 0:02











    0














    for i in file*; do grep -q Minimum "$i" && grep Minimum "$i" > "$i/file/new"; done



    • grep -q Minimum "$i" immediately exists after the first "Minimum" is found

    • the second grep does the real job and is not executed if the first one fails


    • "$i/file/new" renames file in each filename to new





    share|improve this answer



























      0














      for i in file*; do grep -q Minimum "$i" && grep Minimum "$i" > "$i/file/new"; done



      • grep -q Minimum "$i" immediately exists after the first "Minimum" is found

      • the second grep does the real job and is not executed if the first one fails


      • "$i/file/new" renames file in each filename to new





      share|improve this answer

























        0












        0








        0







        for i in file*; do grep -q Minimum "$i" && grep Minimum "$i" > "$i/file/new"; done



        • grep -q Minimum "$i" immediately exists after the first "Minimum" is found

        • the second grep does the real job and is not executed if the first one fails


        • "$i/file/new" renames file in each filename to new





        share|improve this answer













        for i in file*; do grep -q Minimum "$i" && grep Minimum "$i" > "$i/file/new"; done



        • grep -q Minimum "$i" immediately exists after the first "Minimum" is found

        • the second grep does the real job and is not executed if the first one fails


        • "$i/file/new" renames file in each filename to new






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 4 at 0:15









        FreddyFreddy

        1,429210




        1,429210



























            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%2f504162%2fgrep-cant-get-the-word-i-need-but-linux-continues-to-do-the-next-job-how-do-i%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?

            Bahrain

            Postfix configuration issue with fips on centos 7; mailgun relay