menu

Questions & Answers

Find and show information from logs inside a folder in linux

I'm trying to create a little script using bash in linux. That allows me to find if there is any tag 103=16 inside a log

I have multiple folders named for example l51prdsrv-api1.nebex.local, l51prdsrv-oe1.nebex.local, etc... inside those folders are .log files like TRADX_gsoe3.log, TRADX_gseuoe2.log, etc... .

I need to find if inside those logs there is the tag 103=16

I'm trying this command

find . /opt/FIXLOGS/l51prdsrv* -iname "TRADX_" -type f | grep -e 103=16

But what it does is that is showing just the logs names and not the content to see if there is a tag 103=16

Answers(2) :

First of all, you are not searching files of the form TRADX_something.log, but only files which are just named TRADX_ (case-insensitively, so TradX_ would also be found).

Then you are feeding to grep the names of the files, but never look into the content of those files. From the grep man page, you see that the file content can be supplied either via stdin, or by specifying the file name on the command line. In your case, the latter is the way to go. Therefore you can either do a

find . /opt/FIXLOGS/l51prdsrv* -iname "TRADX_*.log" -type f -exec grep -f 103=16 {} \;

if you are only interested in the matchin lines, or a

find . /opt/FIXLOGS/l51prdsrv* -iname "TRADX_*.log" -type f -exec grep -f 103=16 {} /dev/null \;

if you also want to see the file names where the pattern matches. The reason is that grep is printing the filename only if it sees more than 1 filename on the command line and the /dev/null provides a second dummy file. find replaces the {} by the filename.

BTW, I used -f for grep instead of your -e, because you don't seem to use any specific regular expression pattern anyway.

But you don't need find for this task. An alternative would be an explicit loop:

shopt -s nocasematch # make globbing case-insensitive
shopt -s globstar # turn on ** globbing
for f in {.,/opt/FIXLOGS/l51prdsrv*}/**/tradx_*.log
do
  [[ -f $f ]] && grep -f 103=16 "$f" /dev/null
done

While the loop looks more complicated at first glance, it is easier to extend the logic in case you want to do more with the files instead of just grepping the lines, for instance taking specific actions on those files which contain the pattern.

Comments:
2023-01-24 23:52:09
Thank you very much, yes you are right as well with the -f, lets see if i can get it done with it!

You are doing:

find . /opt/FIXLOGS/l51prdsrv* -iname "TRADX_" -type f | grep -e 103=16

I propose you do:

find . /opt/FIXLOGS/l51prdsrv* -iname "TRADX_" -type f -exec grep -e "103=16" {} /dev/null \;

What's the difference?

find ... -type f

=> gives you a list of files.

When you add | grep -e 103=16, then you perform that on the filenames.
When you add -exec grep ..., then you perform that on the files itselfs.

Comments:
2023-01-24 23:52:09
Thank you for your feedback! i will try it. Hopefully this will do the trick!