【问题标题】:Get the line of last grep match in bash获取 bash 中最后一个 grep 匹配的行
【发布时间】:2019-02-13 22:44:44
【问题描述】:

我正在执行一个 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,也可以!)

【问题讨论】:

标签: linux bash awk sed grep


【解决方案1】:
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

【讨论】:

  • Tnx!会检查出来!那个文件是什么?您可以将我的 ffmepg 命令添加到其中并获得完整的答案吗?
  • file 包含您的问题中显示的示例输入。如果输入直接来自名为cmd 的命令而不是名为file 的文件,则只需使用cmd | tac | grep 而不是tac file | grep
【解决方案2】:

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

grep <args> | tail -1

【讨论】:

  • 我在使用 grep 时遇到了一些问题。为什么这不起作用只显示RMS 行?! ffmpeg -i 540p.mp4 -map 0:a:1 -af astats -f null - | grep -i RMS
  • 好的,知道了。需要添加一些额外的东西:ffmpeg -i 540p.mp4 -map 0:a:1 -af astats -f null - 2&gt;&amp;1 | grep 'RMS level dB' | tail -1
  • 这将是一种非常缓慢的方法,因为 grep 将对每一行输入进行正则表达式比较,打印可能的多行输出,然后 tail 将放弃所有这些努力,除了最后一行输出!
  • 鉴于用例,我认为性能不会成为问题。也许如果 grepping 一个多千兆字节的文件......
  • 可能是这样,但我们不仅回答问题中提出的用例,还回答下一个在档案中搜索他们自己的Get the line of last grep match in bash 问题的答案并且确实需要有效的方法,因此值得一提。
猜你喜欢
  • 2012-06-13
  • 2013-09-13
  • 1970-01-01
  • 2020-02-29
  • 1970-01-01
  • 2017-03-31
  • 1970-01-01
  • 2013-11-25
  • 2013-11-28
相关资源
最近更新 更多