使用 awk、sed 或 grep 从文件中提取唯一的文本片段

使用 awk、sed 或 grep 从文件中提取唯一的文本片段

从 的输出中pactl list sink-inputs,我需要抓住接收器输入VLC 的号码。在此之前,我尝试提取仅包含 VLC 输出的部分。所有我认为有效的方法都有缺点。这是一个示例输出:

$ pactl list sink-inputs
Sink Input #1373
    Driver: protocol-native.c
    Owner Module: 9
    Client: 10350
    Sink: 0
    Sample Specification: float32le 2ch 44100Hz
    Channel Map: front-left,front-right
    Format: pcm, format.sample_format = "\"float32le\""  format.rate = "44100"  format.channels = "2"  format.channel_map = "\"front-left,front-right\""
    Corked: no
    Mute: no
    Volume: 0: 100% 1: 100%
            0: 0,00 dB 1: 0,00 dB
            balance 0,00
    Buffer Latency: 453287 usec
    Sink Latency: 19697 usec
    Resample method: copy
    Properties:
        media.role = "video"
        media.name = "audio stream"
        application.name = "VLC media player (LibVLC 2.1.5)"
        native-protocol.peer = "UNIX socket client"
        native-protocol.version = "28"
        application.id = "org.VideoLAN.VLC"
        application.version = "2.1.5"
        application.icon_name = "vlc"
        application.language = "pt_BR.UTF-8"
        application.process.id = "19965"
        application.process.machine_id = "948146522454ae6aa2bb8ed153f4bce4"
        application.process.session_id = "948146522454ae6aa2bb8ed153f4bce4-1431635199.85146-1790309877"
        application.process.user = "teresaejunior"
        application.process.host = "localhost"
        application.process.binary = "vlc"
        window.x11.display = ":0.0"
        module-stream-restore.id = "sink-input-by-media-role:video"

Sink Input #1378
    Driver: protocol-native.c
    Owner Module: 9
    Client: 10378
    Sink: 0
    Sample Specification: s16le 2ch 44100Hz
    Channel Map: front-left,front-right
    Format: pcm, format.sample_format = "\"s16le\""  format.rate = "44100"  format.channels = "2"  format.channel_map = "\"front-left,front-right\""
    Corked: no
    Mute: no
    Volume: 0:  87% 1:  87%
            0: -3,63 dB 1: -3,63 dB
            balance 0,00
    Buffer Latency: 989841 usec
    Sink Latency: 19572 usec
    Resample method: n/a
    Properties:
        media.name = "audio stream"
        application.name = "mplayer2"
        native-protocol.peer = "UNIX socket client"
        native-protocol.version = "28"
        application.process.id = "20093"
        application.process.user = "teresaejunior"
        application.process.host = "localhost"
        application.process.binary = "mplayer2"
        application.language = "C"
        window.x11.display = ":0.0"
        application.process.machine_id = "948146522454ae6aa2bb8ed153f4bce4"
        module-stream-restore.id = "sink-input-by-application-name:mplayer2"

两者都awk '/^Sink/,/VLC/'抓取sed -n '/^Sink/,/VLC/p'VLC 部分,但也抓取 mplayer2 部分,直到输出结束:

$ pactl list sink-inputs | awk '/^Sink/,/VLC/'
Sink Input #1373
    Driver: protocol-native.c
    Owner Module: 9
    Client: 10350
    Sink: 0
    Sample Specification: float32le 2ch 44100Hz
    Channel Map: front-left,front-right
    Format: pcm, format.sample_format = "\"float32le\""  format.rate = "44100"  format.channels = "2"  format.channel_map = "\"front-left,front-right\""
    Corked: no
    Mute: no
    Volume: 0: 100% 1: 100%
            0: 0,00 dB 1: 0,00 dB
            balance 0,00
    Buffer Latency: 437414 usec
    Sink Latency: 19666 usec
    Resample method: copy
    Properties:
        media.role = "video"
        media.name = "audio stream"
        application.name = "VLC media player (LibVLC 2.1.5)"
Sink Input #1379
    Driver: protocol-native.c
    Owner Module: 9
    Client: 10381
    Sink: 0
    Sample Specification: s16le 2ch 44100Hz
    Channel Map: front-left,front-right
    Format: pcm, format.sample_format = "\"s16le\""  format.rate = "44100"  format.channels = "2"  format.channel_map = "\"front-left,front-right\""
    Corked: no
    Mute: no
    Volume: 0:  87% 1:  87%
            0: -3,63 dB 1: -3,63 dB
            balance 0,00
    Buffer Latency: 980045 usec
    Sink Latency: 19563 usec
    Resample method: n/a
    Properties:
        media.name = "audio stream"
        application.name = "mplayer2"
        native-protocol.peer = "UNIX socket client"
        native-protocol.version = "28"
        application.process.id = "20093"
        application.process.user = "teresaejunior"
        application.process.host = "localhost"
        application.process.binary = "mplayer2"
        application.language = "C"
        window.x11.display = ":0.0"
        application.process.machine_id = "948146522454ae6aa2bb8ed153f4bce4"
        module-stream-restore.id = "sink-input-by-application-name:mplayer2"

grep -Poz '^Sink(?s).*?VLC'有效,但如果 VLC 输出应该在 mplayer2 之后,它将失败(使用 mplayer2 而不是 VLC 进行测试):

$ pactl list sink-inputs | grep -Poz '^Sink(?s).*?mplayer'
Sink Input #1373
    Driver: protocol-native.c
    Owner Module: 9
    Client: 10350
    Sink: 0
    Sample Specification: float32le 2ch 44100Hz
    Channel Map: front-left,front-right
    Format: pcm, format.sample_format = "\"float32le\""  format.rate = "44100"  format.channels = "2"  format.channel_map = "\"front-left,front-right\""
    Corked: no
    Mute: no
    Volume: 0: 100% 1: 100%
            0: 0,00 dB 1: 0,00 dB
            balance 0,00
    Buffer Latency: 441088 usec
    Sink Latency: 18159 usec
    Resample method: copy
    Properties:
        media.role = "video"
        media.name = "audio stream"
        application.name = "VLC media player (LibVLC 2.1.5)"
        native-protocol.peer = "UNIX socket client"
        native-protocol.version = "28"
        application.id = "org.VideoLAN.VLC"
        application.version = "2.1.5"
        application.icon_name = "vlc"
        application.language = "pt_BR.UTF-8"
        application.process.id = "19965"
        application.process.machine_id = "948146522454ae6aa2bb8ed153f4bce4"
        application.process.session_id = "948146522454ae6aa2bb8ed153f4bce4-1431635199.85146-1790309877"
        application.process.user = "teresaejunior"
        application.process.host = "localhost"
        application.process.binary = "vlc"
        window.x11.display = ":0.0"
        module-stream-restore.id = "sink-input-by-media-role:video"

Sink Input #1380
    Driver: protocol-native.c
    Owner Module: 9
    Client: 10396
    Sink: 0
    Sample Specification: s16le 2ch 44100Hz
    Channel Map: front-left,front-right
    Format: pcm, format.sample_format = "\"s16le\""  format.rate = "44100"  format.channels = "2"  format.channel_map = "\"front-left,front-right\""
    Corked: no
    Mute: no
    Volume: 0:  87% 1:  87%
            0: -3,63 dB 1: -3,63 dB
            balance 0,00
    Buffer Latency: 989841 usec
    Sink Latency: 18084 usec
    Resample method: n/a
    Properties:
        media.name = "audio stream"
        application.name = "mplayer

所需的输出:

Sink Input #1373
    Driver: protocol-native.c
    Owner Module: 9
    Client: 10350
    Sink: 0
    Sample Specification: float32le 2ch 44100Hz
    Channel Map: front-left,front-right
    Format: pcm, format.sample_format = "\"float32le\""  format.rate = "44100"  format.channels = "2"  format.channel_map = "\"front-left,front-right\""
    Corked: no
    Mute: no
    Volume: 0: 100% 1: 100%
            0: 0,00 dB 1: 0,00 dB
            balance 0,00
    Buffer Latency: 441088 usec
    Sink Latency: 18159 usec
    Resample method: copy
    Properties:
        media.role = "video"
        media.name = "audio stream"
        application.name = "VLC media player (LibVLC 2.1.5)"

答案1

ed

ed -s <<'IN'
r !pactl list sink-inputs
/VLC/+,$d
?Sink Input?,.p
q
IN

它将r命令输出写入文本缓冲区,d删除第一行匹配之后的所有内容VLC,然后p从上一行匹配Sink Input到当前行。

sed

pactl list sink-inputs | sed -n 'H;/Sink Input/h;/VLC/{x;p;q}'

它将每一行附加到H旧缓冲区,如果一行匹配,Sink Input它会覆盖h旧缓冲区,当一行匹配 VLC 时,它x会更改保留空间 w。模式空间、prins 和quits。

答案2

我会使用 Perl 的段落模式:

pactl list sink-inputs | perl -00ne 'print if s/(.*?VLC.*?\n).*/$1/ms' 

-00输入记录分隔符设置为\n\n“行”是一个段落。然后,替换将匹配第一个之前的所有内容VLC,然后是第一个换行之前的所有内容,并将它们保存为$1.之后的所有内容都将被删除(因为我们将所有内容替换为$1)。最后,打印替换成功的“行”。

答案3

这些实际上是由空行分隔的多行记录。 Awk 非常适合处理此类数据:

pactl list sink-inputs | awk -v RS="" '/VLC/' 

如果您想对第一次出现“VLC”后不包括记录的底部部分非常挑剔,那么:

pactl list sink-inputs | awk -v RS="" -v FS="\n" '/VLC/{ for(i=1; i<=NF; i++) { print $i; if($i ~ /VLC/) exit}}' 

相关内容