Want to parse output for variables in Bash

Trying to finish up my script that automates some video encoding work.

Situation: There is an MKV file to be transcoded.

Problem: MKVINFO will give a bunch of output about an MKV file, included in that output are two lines I am interested in:

|  + Default duration: 41.708ms (23.976 fps for a video track)
|   + Pixel width: 1280

Is there some way of echoing the output of from MKVINFO, and parsing down the two bits of information that I am looking for: fps (23.976) and pixel width (1280). I get the impression that this is doable, but I have only learned what little Bash I know from googling for examples, but I haven't found something similar to this in my looking.

Thanks, appreciate any help.

I'm on Mac OS X Snow Leopard 10.6.3. the command to run with mkvinfo is simply:

$ mkvinfo mkvfile.mkv

ballpark figure of about 100 lines of output from this command.

Try this:

mkvinfo mkvfile.mkv | 
awk '
/ Default duration:/ {sub(".*\(","");print $0 + 0}
/ Pixel width:/{print $NF}'

I tried that code exactly as shown above, and also as a 1 liner, but both times got this output:

awk: illegal primary in regular expression .*( at 
 source line number 2
 context is
	/ Default duration:/ >>>  {sub(".*\(","") <<< 

Try nawk or /usr/xpg4/bin/awk on Solaris.

Regards

Or,

/home->awk -F "[ (]" '/Default duration:/ { print $(NF-5)} /Pixel width:/ {print $NF}' f
23.976
1280
/home->

working for me.

Is that trailing 'f' a typo?

Thanks.

that is filename.

you use in the same way:

mkvinfo mkvfile.mkv | awk -F "[ (]" '/Default duration:/ { print $(NF-5)} /Pixel width:/ {print $NF}'

So far this is awesome! Going to have to run it against a few test MKV's to see that it's reliably getting that correct information - thank you so much!

# Adding a little routine to check the Pixel Width and Frames Per Second
MKVFPS=$( mkvinfo "$i" | awk -F "[ (]" '/Default duration:/ { print $(NF-5)}')
echo " *** Test MKV Information ***"
echo "MKVFPS: $MKVFPS"

MKVWIDTH=$( mkvinfo "$i" | awk -F "[ (]" '/Pixel width:/ {print $NF}')
echo "MKVWIDTH: $MKVFPS"

Outputs:

MKVFPS: 23.976
MKVWIDTH: 23.976

Two solutions :

/ Default duration:/ {sub(".*\\(","");print $0 + 0}

Or

/ Default duration:/ {sub(/.*\(/,"");print $0 + 0}

Jean-Pierre.

I was looking to leverage this to a very similar situation, in this other situation the pattern I am trying to match shows up in the information multiple times. Is there a way I can tell it to only return the 1st time after the pattern match and not continue to show the ones that show up afterwards.

e.g.:
width = 720
thisfield =
thatthat =
width = 853
thisotherfield =
thatotherfield =
width = 853

"width = " shows up multiple times, I would only like to see the value for the first, 720.