tail and grep + print and exit first match












3















I'm looking for a 1 liner to tail a file and grep a "string", print the first match (new line) and exit.



I came up with:



tail -f /var/log/logfile.log -n 0 | grep -m 1 -i string_to_match


actual result is that the command prints the first match but exits only after the second match.
any help would be appreciated










share|improve this question

























  • Please take a look at How to create a Minimal, Complete, and Verifiable example. Please add sample input and your desired output for that sample input to your question.

    – Cyrus
    Jan 1 at 8:52











  • See this: stackoverflow.com/questions/5024357/… which also has a link to Superuser with more solutions.

    – James Brown
    Jan 1 at 9:42








  • 1





    Heh. So, grep exits immediately, but tail doesn't know that grep exited until it tries to write a second line and gets a SIGPIPE.

    – Charles Duffy
    Jan 1 at 16:17
















3















I'm looking for a 1 liner to tail a file and grep a "string", print the first match (new line) and exit.



I came up with:



tail -f /var/log/logfile.log -n 0 | grep -m 1 -i string_to_match


actual result is that the command prints the first match but exits only after the second match.
any help would be appreciated










share|improve this question

























  • Please take a look at How to create a Minimal, Complete, and Verifiable example. Please add sample input and your desired output for that sample input to your question.

    – Cyrus
    Jan 1 at 8:52











  • See this: stackoverflow.com/questions/5024357/… which also has a link to Superuser with more solutions.

    – James Brown
    Jan 1 at 9:42








  • 1





    Heh. So, grep exits immediately, but tail doesn't know that grep exited until it tries to write a second line and gets a SIGPIPE.

    – Charles Duffy
    Jan 1 at 16:17














3












3








3


1






I'm looking for a 1 liner to tail a file and grep a "string", print the first match (new line) and exit.



I came up with:



tail -f /var/log/logfile.log -n 0 | grep -m 1 -i string_to_match


actual result is that the command prints the first match but exits only after the second match.
any help would be appreciated










share|improve this question
















I'm looking for a 1 liner to tail a file and grep a "string", print the first match (new line) and exit.



I came up with:



tail -f /var/log/logfile.log -n 0 | grep -m 1 -i string_to_match


actual result is that the command prints the first match but exits only after the second match.
any help would be appreciated







linux bash grep tail






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 2 at 8:11









tripleee

93.7k13130185




93.7k13130185










asked Jan 1 at 8:47









Isaac AIsaac A

186




186













  • Please take a look at How to create a Minimal, Complete, and Verifiable example. Please add sample input and your desired output for that sample input to your question.

    – Cyrus
    Jan 1 at 8:52











  • See this: stackoverflow.com/questions/5024357/… which also has a link to Superuser with more solutions.

    – James Brown
    Jan 1 at 9:42








  • 1





    Heh. So, grep exits immediately, but tail doesn't know that grep exited until it tries to write a second line and gets a SIGPIPE.

    – Charles Duffy
    Jan 1 at 16:17



















  • Please take a look at How to create a Minimal, Complete, and Verifiable example. Please add sample input and your desired output for that sample input to your question.

    – Cyrus
    Jan 1 at 8:52











  • See this: stackoverflow.com/questions/5024357/… which also has a link to Superuser with more solutions.

    – James Brown
    Jan 1 at 9:42








  • 1





    Heh. So, grep exits immediately, but tail doesn't know that grep exited until it tries to write a second line and gets a SIGPIPE.

    – Charles Duffy
    Jan 1 at 16:17

















Please take a look at How to create a Minimal, Complete, and Verifiable example. Please add sample input and your desired output for that sample input to your question.

– Cyrus
Jan 1 at 8:52





Please take a look at How to create a Minimal, Complete, and Verifiable example. Please add sample input and your desired output for that sample input to your question.

– Cyrus
Jan 1 at 8:52













See this: stackoverflow.com/questions/5024357/… which also has a link to Superuser with more solutions.

– James Brown
Jan 1 at 9:42







See this: stackoverflow.com/questions/5024357/… which also has a link to Superuser with more solutions.

– James Brown
Jan 1 at 9:42






1




1





Heh. So, grep exits immediately, but tail doesn't know that grep exited until it tries to write a second line and gets a SIGPIPE.

– Charles Duffy
Jan 1 at 16:17





Heh. So, grep exits immediately, but tail doesn't know that grep exited until it tries to write a second line and gets a SIGPIPE.

– Charles Duffy
Jan 1 at 16:17












1 Answer
1






active

oldest

votes


















3














In Bash you could use:



$ grep -m 1 string_to_match <(tail -n 0 -f file)


This could work for testing (NOTICE: IT APPENDS TO A FILE NAMED file):



$ grep -m 1 foo <(tail -n 0 -f file) & sleep 2 ; echo -e bar\nfoo >> file
[1] 5390
foo
[1]+ Done grep --color -m 1 foo <(tail -n 0 -f file)


Edit: Further testing revealed that tail stays running in the background but exits after the next line in the file.






share|improve this answer


























  • Humm, pretty hard to make a one-liner for testing.

    – James Brown
    Jan 1 at 9:11











  • thanks, this one gives syntax error near unexpected token `('

    – Isaac A
    Jan 1 at 9:13






  • 1





    @IsaacA: You don't use bash.

    – Cyrus
    Jan 1 at 9:15






  • 1





    @IsaacA: I can't reproduce the error you mentioned with bash-3.2.57. It occurs when using sh.

    – Cyrus
    Jan 1 at 9:52








  • 1





    @JamesBrown Eventually what you suggested was the right answer for me! Thanks and thank you all! grep -m 1 string_to_match <(tail -n 0 -f /var/log/syslog.log)

    – Isaac A
    Jan 2 at 8:03













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
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: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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%2fstackoverflow.com%2fquestions%2f53994136%2ftail-and-grep-print-and-exit-first-match%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














In Bash you could use:



$ grep -m 1 string_to_match <(tail -n 0 -f file)


This could work for testing (NOTICE: IT APPENDS TO A FILE NAMED file):



$ grep -m 1 foo <(tail -n 0 -f file) & sleep 2 ; echo -e bar\nfoo >> file
[1] 5390
foo
[1]+ Done grep --color -m 1 foo <(tail -n 0 -f file)


Edit: Further testing revealed that tail stays running in the background but exits after the next line in the file.






share|improve this answer


























  • Humm, pretty hard to make a one-liner for testing.

    – James Brown
    Jan 1 at 9:11











  • thanks, this one gives syntax error near unexpected token `('

    – Isaac A
    Jan 1 at 9:13






  • 1





    @IsaacA: You don't use bash.

    – Cyrus
    Jan 1 at 9:15






  • 1





    @IsaacA: I can't reproduce the error you mentioned with bash-3.2.57. It occurs when using sh.

    – Cyrus
    Jan 1 at 9:52








  • 1





    @JamesBrown Eventually what you suggested was the right answer for me! Thanks and thank you all! grep -m 1 string_to_match <(tail -n 0 -f /var/log/syslog.log)

    – Isaac A
    Jan 2 at 8:03


















3














In Bash you could use:



$ grep -m 1 string_to_match <(tail -n 0 -f file)


This could work for testing (NOTICE: IT APPENDS TO A FILE NAMED file):



$ grep -m 1 foo <(tail -n 0 -f file) & sleep 2 ; echo -e bar\nfoo >> file
[1] 5390
foo
[1]+ Done grep --color -m 1 foo <(tail -n 0 -f file)


Edit: Further testing revealed that tail stays running in the background but exits after the next line in the file.






share|improve this answer


























  • Humm, pretty hard to make a one-liner for testing.

    – James Brown
    Jan 1 at 9:11











  • thanks, this one gives syntax error near unexpected token `('

    – Isaac A
    Jan 1 at 9:13






  • 1





    @IsaacA: You don't use bash.

    – Cyrus
    Jan 1 at 9:15






  • 1





    @IsaacA: I can't reproduce the error you mentioned with bash-3.2.57. It occurs when using sh.

    – Cyrus
    Jan 1 at 9:52








  • 1





    @JamesBrown Eventually what you suggested was the right answer for me! Thanks and thank you all! grep -m 1 string_to_match <(tail -n 0 -f /var/log/syslog.log)

    – Isaac A
    Jan 2 at 8:03
















3












3








3







In Bash you could use:



$ grep -m 1 string_to_match <(tail -n 0 -f file)


This could work for testing (NOTICE: IT APPENDS TO A FILE NAMED file):



$ grep -m 1 foo <(tail -n 0 -f file) & sleep 2 ; echo -e bar\nfoo >> file
[1] 5390
foo
[1]+ Done grep --color -m 1 foo <(tail -n 0 -f file)


Edit: Further testing revealed that tail stays running in the background but exits after the next line in the file.






share|improve this answer















In Bash you could use:



$ grep -m 1 string_to_match <(tail -n 0 -f file)


This could work for testing (NOTICE: IT APPENDS TO A FILE NAMED file):



$ grep -m 1 foo <(tail -n 0 -f file) & sleep 2 ; echo -e bar\nfoo >> file
[1] 5390
foo
[1]+ Done grep --color -m 1 foo <(tail -n 0 -f file)


Edit: Further testing revealed that tail stays running in the background but exits after the next line in the file.







share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 1 at 11:46

























answered Jan 1 at 9:08









James BrownJames Brown

19.4k31736




19.4k31736













  • Humm, pretty hard to make a one-liner for testing.

    – James Brown
    Jan 1 at 9:11











  • thanks, this one gives syntax error near unexpected token `('

    – Isaac A
    Jan 1 at 9:13






  • 1





    @IsaacA: You don't use bash.

    – Cyrus
    Jan 1 at 9:15






  • 1





    @IsaacA: I can't reproduce the error you mentioned with bash-3.2.57. It occurs when using sh.

    – Cyrus
    Jan 1 at 9:52








  • 1





    @JamesBrown Eventually what you suggested was the right answer for me! Thanks and thank you all! grep -m 1 string_to_match <(tail -n 0 -f /var/log/syslog.log)

    – Isaac A
    Jan 2 at 8:03





















  • Humm, pretty hard to make a one-liner for testing.

    – James Brown
    Jan 1 at 9:11











  • thanks, this one gives syntax error near unexpected token `('

    – Isaac A
    Jan 1 at 9:13






  • 1





    @IsaacA: You don't use bash.

    – Cyrus
    Jan 1 at 9:15






  • 1





    @IsaacA: I can't reproduce the error you mentioned with bash-3.2.57. It occurs when using sh.

    – Cyrus
    Jan 1 at 9:52








  • 1





    @JamesBrown Eventually what you suggested was the right answer for me! Thanks and thank you all! grep -m 1 string_to_match <(tail -n 0 -f /var/log/syslog.log)

    – Isaac A
    Jan 2 at 8:03



















Humm, pretty hard to make a one-liner for testing.

– James Brown
Jan 1 at 9:11





Humm, pretty hard to make a one-liner for testing.

– James Brown
Jan 1 at 9:11













thanks, this one gives syntax error near unexpected token `('

– Isaac A
Jan 1 at 9:13





thanks, this one gives syntax error near unexpected token `('

– Isaac A
Jan 1 at 9:13




1




1





@IsaacA: You don't use bash.

– Cyrus
Jan 1 at 9:15





@IsaacA: You don't use bash.

– Cyrus
Jan 1 at 9:15




1




1





@IsaacA: I can't reproduce the error you mentioned with bash-3.2.57. It occurs when using sh.

– Cyrus
Jan 1 at 9:52







@IsaacA: I can't reproduce the error you mentioned with bash-3.2.57. It occurs when using sh.

– Cyrus
Jan 1 at 9:52






1




1





@JamesBrown Eventually what you suggested was the right answer for me! Thanks and thank you all! grep -m 1 string_to_match <(tail -n 0 -f /var/log/syslog.log)

– Isaac A
Jan 2 at 8:03







@JamesBrown Eventually what you suggested was the right answer for me! Thanks and thank you all! grep -m 1 string_to_match <(tail -n 0 -f /var/log/syslog.log)

– Isaac A
Jan 2 at 8:03






















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • 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%2fstackoverflow.com%2fquestions%2f53994136%2ftail-and-grep-print-and-exit-first-match%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

MongoDB - Not Authorized To Execute Command

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

Npm cannot find a required file even through it is in the searched directory