Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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:

/home/javanix/filename.log
    <RF line>
    <average line>

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

share|improve this question
2  
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

5 Answers 5

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

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

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
1  
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 at 17:14
    
I just updated my post with the s flag :) –  Shadok Feb 21 at 23:02
1  
Confirmed: works for me –  Canadian Luke Feb 21 at 23:07

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.