Skip to content
Advertisement

Extract desired information from a string in Linux

Below is the string that I get, now I want to extract the two 3840 from this line, what command should I be using in Bash scripting?

Stream #0:0(eng): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 3840x3840 [SAR 1:1 DAR 1:1], 100072 kb/s, 59.94 fps, 59.94 tbr, 60k tbn, 119.88 tbc

Advertisement

Answer

You can pipe it into grep grep -oE "[1-9][0-9]*x[1-9][0-9]*" which would extract 3840x3840.

Following is an example which retrieves both the width and height in separate variables:

INPUT="Stream #0:0(eng): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1024x768 [SAR 1:1 DAR 1:1], 100072 kb/s, 59.94 fps, 59.94 tbr, 60k tbn, 119.88 tbc"
RES=`echo $INPUT | grep -oE "[1-9][0-9]*x[1-9][0-9]*"` # 1024x768
WIDTH=`echo $RES | cut -f1 -dx` # get the first column, where 'x' is the separator
HEIGHT=`echo $RES | cut -f2 -dx` # get the second column, where 'x' is the separator
echo $WIDTH # 1024 in this example
echo $HEIGHT # 768 in this example
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement