Home > database >  Get middle line from the output of a command
Get middle line from the output of a command

Time:12-14

The following command list the duration of I-frames present in a video file.

ffprobe -i ./test.mp4 -v quiet -skip_frame nokey -select_streams v:0 -of flat -show_entries frame=pkt_pts_time | awk -F'=' '{gsub(/"/, "", $NF); print $NF}'

I'm trying to get the duration of I-frame that is in middle. The output of the above command will be like

0.066667
2.066667
4.066667
9.066667
14.066667
19.066667
24.066667

How do I get the middle value? Here, I would like to get 9.066667 as the result. If the number of lines is even (say I've 2 values), I need to get the 1st value.

I tried using wc to get the number of lines and sed to print the middle line. But, this straightforward solution required me to run the command twice. I tried piping the command, but I didn't find any solution for storing the value in a variable (from the wc command) and using the same in another command (sed command) while retaining the piped output.

CodePudding user response:

You could do:

awk '{a[NR]=$0} END {print a[int((NR 1)/2)]}'
  • Related