在bash中获取最后的grep匹配行

时间:2019-02-13 22:44:44

标签: linux bash awk sed grep

我正在执行bash脚本,并且需要在特定行中获取特定值。这些线路本身可以有多个,尽管它们属于不同的类别。这是一个示例输出:

ffmpeg version N-92906-g54109b1d14 Copyright (c) 2000-2019 the FFmpeg developers
  built with gcc 8.2.1 (GCC) 20181201
  configuration: --enable-gpl --enable-version3 --enable-sdl2 --enable-fontconfig --enable-gnutls --enable-iconv --enable-libass --enable-libbluray --enable-libfreetype --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libtheora --enable-libtwolame --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libzimg --enable-lzma --enable-zlib --enable-gmp --enable-libvidstab --enable-libvorbis --enable-libvo-amrwbenc --enable-libmysofa --enable-libspeex --enable-libxvid --enable-libaom --enable-libmfx --enable-amf --enable-ffnvcodec --enable-cuvid --enable-d3d11va --enable-nvenc --enable-nvdec --enable-dxva2 --enable-avisynth --enable-libopenmpt
  libavutil      56. 25.100 / 56. 25.100
  libavcodec     58. 43.100 / 58. 43.100
  libavformat    58. 25.100 / 58. 25.100
  libavdevice    58.  6.101 / 58.  6.101
  libavfilter     7. 46.101 /  7. 46.101
  libswscale      5.  4.100 /  5.  4.100
  libswresample   3.  4.100 /  3.  4.100
  libpostproc    55.  4.100 / 55.  4.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '.\6 channel.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    creation_time   : 2019-01-31T00:03:03.000000Z
  Duration: 00:00:30.12, start: 0.000000, bitrate: 2527 kb/s
    Stream #0:0: Video: h264 (High) (avc1 / 0x31637661), yuv420p(tv, bt709), 960x540 [SAR 1:1 DAR 16:9], 2128 kb/s, 29.97 fps, 29.97 tbr, 30k tbn, 59.94 tbc (default)
    Metadata:
      creation_time   : 2019-01-31T00:03:03.000000Z
      handler_name    : VideoHandler
      timecode        : 00:00:00:00
    Stream #0:1: Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 98 kb/s (default)
    Metadata:
      creation_time   : 2019-01-31T00:03:03.000000Z
      handler_name    : SoundHandler
    Stream #0:2: Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 98 kb/s (default)
    Metadata:
      creation_time   : 2019-01-31T00:03:03.000000Z
      handler_name    : SoundHandler
    Stream #0:3: Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 98 kb/s (default)
    Metadata:
      creation_time   : 2019-01-31T00:03:03.000000Z
      handler_name    : SoundHandler
    Stream #0:4: Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 98 kb/s (default)
    Metadata:
      creation_time   : 2019-01-31T00:03:03.000000Z
      handler_name    : SoundHandler
    Stream #0:5: Data: none (tmcd / 0x64636D74), 0 kb/s (default)
    Metadata:
      creation_time   : 2019-01-31T00:03:03.000000Z
      handler_name    : TimecodeMediaHandler
      timecode        : 00:00:00:00
Stream mapping:
  Stream #0:2 -> #0:0 (aac (native) -> pcm_s16le (native))
Press [q] to stop, [?] for help
Output #0, null, to 'pipe:':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf58.25.100
    Stream #0:0: Audio: pcm_s16le, 44100 Hz, stereo, s16, 1411 kb/s (default)
    Metadata:
      creation_time   : 2019-01-31T00:03:03.000000Z
      handler_name    : SoundHandler
      encoder         : Lavc58.43.100 pcm_s16le
size=N/A time=00:00:30.06 bitrate=N/A speed= 362x
video:0kB audio:5180kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: unknown
[Parsed_astats_0 @ 000001e515c90f00] Channel: 1
[Parsed_astats_0 @ 000001e515c90f00] DC offset: 0.000098
[Parsed_astats_0 @ 000001e515c90f00] Min level: -0.394529
[Parsed_astats_0 @ 000001e515c90f00] Max level: 0.380586
[Parsed_astats_0 @ 000001e515c90f00] Min difference: 0.000000
[Parsed_astats_0 @ 000001e515c90f00] Max difference: 0.232958
[Parsed_astats_0 @ 000001e515c90f00] Mean difference: 0.005622
[Parsed_astats_0 @ 000001e515c90f00] RMS difference: 0.010923
[Parsed_astats_0 @ 000001e515c90f00] Peak level dB: -8.078412
[Parsed_astats_0 @ 000001e515c90f00] RMS level dB: -26.826611
[Parsed_astats_0 @ 000001e515c90f00] RMS peak dB: -16.475342
[Parsed_astats_0 @ 000001e515c90f00] RMS trough dB: -85.439368
[Parsed_astats_0 @ 000001e515c90f00] Crest factor: 8.657848
[Parsed_astats_0 @ 000001e515c90f00] Flat factor: 0.000000
[Parsed_astats_0 @ 000001e515c90f00] Peak count: 2
[Parsed_astats_0 @ 000001e515c90f00] Bit depth: 32/32
[Parsed_astats_0 @ 000001e515c90f00] Dynamic range: 215.482282
[Parsed_astats_0 @ 000001e515c90f00] Zero crossings: 109765
[Parsed_astats_0 @ 000001e515c90f00] Zero crossings rate: 0.082774
[Parsed_astats_0 @ 000001e515c90f00] Channel: 2
[Parsed_astats_0 @ 000001e515c90f00] DC offset: -0.000000
[Parsed_astats_0 @ 000001e515c90f00] Min level: -0.000010
[Parsed_astats_0 @ 000001e515c90f00] Max level: 0.000010
[Parsed_astats_0 @ 000001e515c90f00] Min difference: 0.000000
[Parsed_astats_0 @ 000001e515c90f00] Max difference: 0.000016
[Parsed_astats_0 @ 000001e515c90f00] Mean difference: 0.000000
[Parsed_astats_0 @ 000001e515c90f00] RMS difference: 0.000000
[Parsed_astats_0 @ 000001e515c90f00] Peak level dB: -100.304024
[Parsed_astats_0 @ 000001e515c90f00] RMS level dB: -136.845747
[Parsed_astats_0 @ 000001e515c90f00] RMS peak dB: -115.764979
[Parsed_astats_0 @ 000001e515c90f00] RMS trough dB: -1134.435605
[Parsed_astats_0 @ 000001e515c90f00] Crest factor: 67.156208
[Parsed_astats_0 @ 000001e515c90f00] Flat factor: 0.000000
[Parsed_astats_0 @ 000001e515c90f00] Peak count: 2
[Parsed_astats_0 @ 000001e515c90f00] Bit depth: 32/32
[Parsed_astats_0 @ 000001e515c90f00] Dynamic range: 130.387729
[Parsed_astats_0 @ 000001e515c90f00] Zero crossings: 7128
[Parsed_astats_0 @ 000001e515c90f00] Zero crossings rate: 0.005375
[Parsed_astats_0 @ 000001e515c90f00] Overall
[Parsed_astats_0 @ 000001e515c90f00] DC offset: 0.000098
[Parsed_astats_0 @ 000001e515c90f00] Min level: -0.394529
[Parsed_astats_0 @ 000001e515c90f00] Max level: 0.380586
[Parsed_astats_0 @ 000001e515c90f00] Min difference: 0.000000
[Parsed_astats_0 @ 000001e515c90f00] Max difference: 0.232958
[Parsed_astats_0 @ 000001e515c90f00] Mean difference: 0.002811
[Parsed_astats_0 @ 000001e515c90f00] RMS difference: 0.007724
[Parsed_astats_0 @ 000001e515c90f00] Peak level dB: -8.078412
[Parsed_astats_0 @ 000001e515c90f00] RMS level dB: -29.836911
[Parsed_astats_0 @ 000001e515c90f00] RMS peak dB: -16.475342
[Parsed_astats_0 @ 000001e515c90f00] RMS trough dB: -1134.435605
[Parsed_astats_0 @ 000001e515c90f00] Flat factor: 0.000000
[Parsed_astats_0 @ 000001e515c90f00] Peak count: 2.000000
[Parsed_astats_0 @ 000001e515c90f00] Bit depth: 32/32
[Parsed_astats_0 @ 000001e515c90f00] Number of samples: 1326080

我正在考虑通过管道传输整个输出,获得所有可能的匹配,并仅获取最后一个(对应于overall类别),并获得RMS电平dB:-29.836911(忽略行的第一部分)我猜是ffmpeg版本)。

如何获得与grep的比赛的最后一行?或者,如果您知道其他方式(例如sedawk,也可以!)

2 个答案:

答案 0 :(得分:3)

将grep的输出发送到tail -1,以仅获取最后一个匹配项:

grep <args> | tail -1

答案 1 :(得分:3)

tac file | grep -m 1 <args>

ffmpeg <args> | tac | grep -m 1 <args>

读取管道输入。

那将比您目前接受的grep | tail的答案快得多。例如:

$ seq 100000000 > file

$ time grep '3' file | tail -1
99999993

real    0m6.781s
user    0m7.500s
sys     0m1.435s

$ time tac file | grep -m 1 3
99999993

real    0m0.046s
user    0m0.015s
sys     0m0.015s