1
tail -f a/b/c.log d/e/f.log

logs like this:

==> a/b/c.log <==
xxx
yyy
Exception happened 1
zzz

==> d/e/f.log <==
rrr
Exception happened 2
sss

How to change command so that the lines containing "Exception" are picked up like this:

a/b/c.log: Exception happened 1
d/e/f.log: Exception happened 2

The solution can use any Linux commands.

3
  • 1
    Have you actually tried anything? If the files are all in directories in that same pattern and you are only looking for that string, then what's wrong with grep -H "Exception" */*/*.log? If the pattern is actually different from your example, then you'll need to adjust it. Commented Oct 26, 2023 at 10:33
  • The command should follow constantly new lines coming to the files Commented Oct 26, 2023 at 10:55
  • 1) You question needs to specify that. One can put it together after your comment but with the way that it's written, it looks like you just want a way to get that output. 2) Edit the question and add what you have tried so something similar is suggested. It's also important to show that you've made an effort rather than asking others to do it for you. Commented Oct 26, 2023 at 11:33

1 Answer 1

1

If you have multitail installed you could use it with -E to select only lines that match a certain regex and with --label to prepend those lines with the filename e.g. (adjust -N 0 per your needs):

multitail --follow-all -N 0 --mergeall -E 'Exception' \
--label 'a/b/c.log: ' a/b/c.log --label  'd/e/f.log: ' d/e/f.log

If don't have access to multitail, you could just tail -f each file and post-process the output with tools like grep, sed, awk e.g.

tail -f a/b/c.log | sed '/Exception/!d;s|^|/a/b/c.log: |' &
tail -f d/e/f.log | sed '/Exception/!d;s|^|/d/e/f.log: |'

or

tail -f a/b/c.log | awk '/Exception/{print "a/b/c.log: " $0}' &
tail -f d/e/f.log | awk '/Exception/{print "d/e/f.log: " $0}'
4
  • I don't have possibility to install multitail Commented Oct 30, 2023 at 13:03
  • @MikeRapsen - ok, see edit Commented Oct 30, 2023 at 14:09
  • tail -f a/b/c.log | sed '/Exception/!d;s/^/a/b/c.log: /' causes error sed: -e expression #1, char 27: unknown option to `s' I have my own real folder structure and file. It can affect to error message. Solution tail -f a/b/c.log | awk '/Exception/{print "a/b/c.log: " $0}' & tail -f d/e/f.log | awk '/Exception/{print "d/e/f.log: " $0}' works Commented Nov 2, 2023 at 8:14
  • @MikeRapsen - yes, my mistake, I used files in the current directory for testing and I forgot to change the delimiter of the substitution when I replaced their names with your sample paths (and those contain / hence the error). It should work now. Commented Nov 2, 2023 at 17:11

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.