0

I have a line working from this thread that tails a file until a matching pattern is found. It works well, but I can't find a way to suppress the output that occurs afterwards. The line is:

sh -c 'tail -n +0 -f $logfile | { sed "/EOF/ q" && kill $$ ;}'

piping to /dev/null doesn't work as I don't get any output at all from the tail command that way. Also, I'm on OSX and various other sed and awk suggestions don't work due to the syntax.

It always finishes with the below, instead of nothing:
sh: line 10: 14285 Terminated: 15 sh -c 'tail -n +0 -f $logfile | { sed "/EOF/ q" && kill $$ ;}'

I'd also like not to display the matched text (EOF in the above example). Any suggestions welcomed.

Community
  • 1
  • 1
BSUK
  • 692
  • 1
  • 12
  • 28

3 Answers3

0

You can actually discard the stderr like this:

sh -c 'tail -n +0 -f $logfile | { sed "/EOF/q" && p=$$ && kill $((p+1)) ; }'
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • Thanks, that was worth a try but unfortunately didn't work. The error still displays: sh: line 11: 14864 Terminated: 15 sh -c 'tail -n +0 -f $logfile | { sed "/EOF/ q" && kill $$ ;}' 2> /dev/null – BSUK Oct 27 '13 at 19:46
  • sh: line 13: 83905 Terminated: 15 sh -c 'tail -n +0 -f $logfile 2>/dev/null | { sed "/EOF/ q" && kill $$ ;}' It's most strange! Thanks for your help though , appreciated. – BSUK Oct 28 '13 at 18:52
  • sh: line 14: 92708 Terminated: 15 sh -c 'tail -n +0 -f $logfile | { sed "/EOF/ q" && kill $$ > /dev/null 2>&1 ;}' I wonder if it's something to do with the sh command itself.. very odd. Thanks again though. I'm going to perhaps look for a completely different way again. I'm not bothered about the code being over multiple lines etc. – BSUK Oct 29 '13 at 08:23
  • Yes thanks, but unfortunately with this code it never actually ends (although EOF is added to the file). – BSUK Dec 10 '13 at 00:02
  • That's very strange since I tested it on my osx with a real log file. – anubhava Dec 10 '13 at 03:34
0

for a file (log for example)

sed -u "/pattern/ q" YourFile

for a pipe

ls -l | sed -u "/pattern/ q"

the -u of sed tell it to work as a stream input

NeronLeVelu
  • 9,908
  • 1
  • 23
  • 43
  • OSX gives me this: sed: illegal option -- u – BSUK Dec 01 '13 at 22:56
  • sed use a buffer by default so in stream input, it can reach the buffer limit if part of the action keep pattern in memory. Try without the -u it still should work but sometime on line input just after your pattern (buffer) – NeronLeVelu Dec 02 '13 at 06:56
  • Without the -u it just reads the file as is, then quits. It doesn't wait for any pattern first. Not sure why OSX doesn't like the -u option. Thanks anyway. – BSUK Dec 10 '13 at 00:03
0

It's the shell's job monitoring popping the message.

nomonitor() {
    set +m
    "$@"
    set -m
}
nomonitor sh -c 'tail -n +0 -f $logfile | { sed "/EOF/ q" && kill $$; }'
jthill
  • 55,082
  • 5
  • 77
  • 137
  • Thanks. I don't quite understand that, but it gives me: sh: line 13: 17622 Terminated: 15 "$@" – BSUK Dec 01 '13 at 22:59