Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a list of log files that I create as part of a video encoding script that I wrote.

I would like to search all of them and print out certain statistics from the encode - how fast they were encoded, what settings were used, etc.

I can search for the average framerate in one file via this 1 liner:

cat ${filename} | grep average

which outputs:

 work: average encoding speed for job is 23.211176 fps

and search for the ratefactor:

cat ${filename} | grep RF

I would like to search all files in the directory and print off one, or prefereably both pieces of information along with the filename. Is there any way I can use find or grep to get this in a one-liner, or do I need to write a script?

I would like output like this:

    <RF line>
    <average line>

I would like this to either work using FreeBSD 9 or Ubuntu 12.04.

share|improve this question
Useless use of cat award! grep something somewhere is enough. – slhck Jun 25 '12 at 15:52
Haha, fair enough. I want a better solution, hence the question :) – javanix Jun 25 '12 at 19:30
up vote 2 down vote accepted

Using awk with the desired output format:

awk '/average|RF/{ if(f!=FILENAME){f=FILENAME;print f} print"\t"$0}' *.log

For recursive search,

find path -name '*.log' -type f -exec \
  awk '/average|RF/{if(f!=FILENAME){f=FILENAME;print f}print"\t"$0}' {} \;
share|improve this answer

Do this:

egrep 'RF|average' *

The output won't match the output you show above. It will be like this:

filename1: ...
filename1: ...
filename2: ...
filename3: ...

Some file names will appear just once and some more, depending on how many matches there were.

share|improve this answer

I'd say you can do this with grep only:

grep -srHE 'average|RF' path/

s to suppress errors, r for recursive, H to display the filename and E (and the pipe) to match any of both patterns.

share|improve this answer
Awesome, that works perfectly. Thanks! – javanix Jun 25 '12 at 19:34
You're welcome, have a look at the answers using awk as you can get better formatting with it, even though the syntax is pretty obscure at first. – Shadok Jun 26 '12 at 9:40
The only thing I don't like about this one (although it seems the quickest for my use) is that it also outputs errors on files it can't read, and on files that seem to not be there during the command. +1 though – Canadian Luke Feb 21 '14 at 17:14
I just updated my post with the s flag :) – Shadok Feb 21 '14 at 23:02
Confirmed: works for me – Canadian Luke Feb 21 '14 at 23:07

Try this command:

grep -P "average|RF" *

grep will search in all files in the directory (*) and show all matches for the using the Perl Compatible Regular Expression (PCRE) average|RF, which matches both average and RF.

The output format will be the following:

filename.log:<RF line>
filename.log:<average line>
share|improve this answer

Try this one-liner:

find -exec sh -c 'x=$(grep average "$0";grep RF "$0") && echo "$0\n$x"' {} \;

It will produce:

<file 1>
<RF line>
<average line>

As you can see there's no indentation to keep the code short, an alternative would be placing a mark on the file line, something like:

[...] echo ">>> $0\n$x"' {} \;
share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .