解析特定信息

解析特定信息

我在 Linux 系统上运行命令并将输出重定向到文件中。我的文件如下所示:

client: tc-abcdefgh-igjk-lmno-pqrs-tuvwxyza
        version: xxxx
        uuid: abcdefgh-igjk-lmno-pqrs-tuvwxyza
        user-uid: 10000
        user: someuser
        primary-server: xxx.xxx.xxx.xxx
        operational-mode: normal
        mac: XX:XX:XX:XX:XX:XX
        locked-time: xx:xx:xx
        ipaddr: xxx.xxx.xxx.xxx
        hostname: tc-abcdefgh-igjk-lmno-pqrs-tuvwxyza
        connection-1: frdp::Green::Test
        connection-0: frdp::Purple::Test
client: tc-abcdefgh-igjk-lmno-pqrs-abcdefgh
        version: xxxx
        uuid: abcdefgh-igjk-lmno-pqrs-abcedfgh
        user-uid: 10000
        user: someuser
        primary-server: xxx.xxx.xxx.xxx
        operational-mode: normal
        mac: XX:XX:XX:XX:XX:XX
        locked-time: xx:xx:xx
        ipaddr: xxx.xxx.xxx.xxx
        hostname: tc-abcdefgh-igjk-lmno-pqrs-abcdefgh
        connection-0: frdp::Purple::Test
client: tc-abcdefgh-igjk-lmno-pqrs-ijklmnop
        version: xxxx
        uuid: abcdefgh-igjk-lmno-pqrs-ijklmnop
        user-uid: 10000
        user: someuser
        primary-server: xxx.xxx.xxx.xxx
        operational-mode: normal
        mac: XX:XX:XX:XX:XX:XX
        locked-time: xx:xx:xx
        ipaddr: xxx.xxx.xxx.xxx
        hostname: tc-abcdefgh-igjk-lmno-pqrs-ijklmnop
client: tc-abcdefg-higj-klmn-opqr-qrstuvwx
        version: xxxx
        uuid: abcdefgh-igjk-lmno-pqrs-qrstuvwx
        user-uid: 10000
        user: someuser
        primary-server: xxx.xxx.xxx.xxx
        operational-mode: normal
        mac: XX:XX:XX:XX:XX:XX
        locked-time: xx:xx:xx
        ipaddr: xxx.xxx.xxx.xxx
        hostname: tc-abcdefgh-igjk-lmno-pqrs-qrstuvwx
        connection-0: frdp::Purple::Test
client: tc-abcdefgh-igjk-lmno-pqrs-12345678
        version: xxxx
        uuid: abcdefgh-igjk-lmno-pqrs-12345678
        user-uid: 10000
        user: someuser
        primary-server: xxx.xxx.xxx.xxx
        operational-mode: normal
        mac: XX:XX:XX:XX:XX:XX
        locked-time: xx:xx:xx
        ipaddr: xxx.xxx.xxx.xxx
        hostname: tc-abcdefgh-igjk-lmno-pqrs-12345678
        connection-0: frdp::Green::Test
client: tc-abcdefg-higj-klmn-opqr-09876543
        version: xxxx
        uuid: abcdefgh-igjk-lmno-pqrs-09876543
        user-uid: 10000
        user: someuser
        primary-server: xxx.xxx.xxx.xxx
        operational-mode: normal
        mac: XX:XX:XX:XX:XX:XX
        locked-time: xx:xx:xx
        ipaddr: xxx.xxx.xxx.xxx
        hostname: tc-abcdefgh-igjk-lmno-pqrs-09876543
        connection-0: frdp::Purple::Test
client: tc-abcdefgh-igjk-lmno-pqrs-135792468
        version: xxxx
        uuid: abcdefgh-igjk-lmno-pqrs-135792468
        user-uid: 10000
        user: someuser
        primary-server: xxx.xxx.xxx.xxx
        operational-mode: normal
        mac: XX:XX:XX:XX:XX:XX
        locked-time: xx:xx:xx
        ipaddr: xxx.xxx.xxx.xxx
        hostname: tc-abcdefgh-igjk-lmno-pqrs-135792468
        connection-0: frdp::Green::Test
client: tc-abcdefgh-igjk-lmno-pqrs-08649753
        version: xxxx
        uuid: abcdefgh-igjk-lmno-pqrs-08649753
        user-uid: 10000
        user: someuser
        primary-server: xxx.xxx.xxx.xxx
        operational-mode: normal
        mac: XX:XX:XX:XX:XX:XX
        locked-time: xx:xx:xx
        ipaddr: xxx.xxx.xxx.xxx
        hostname: tc-abcdefgh-igjk-lmno-pqrs-08649753
        connection-0: frdp::Purple::Test

我想将仅显示与 Purple 的连接的客户端主机名和没有连接的客户端提取到新文件中。我已经使用以下方法成功地获取了所有不相关的信息:

awk -F '\t' '/^client: / || /connection/' myfile1 >> myfile2

我无法仅提取那些连接到紫色或没有连接的客户端。我想忽略与绿色有联系的所有内容,即使客户端与紫色有联系。

编辑:这是我使用 awk 时得到的结果

client: tc-abcdefgh-igjk-lmno-pqrs-tuvwxyza
        connection-1: frdp::Green::Test
        connection-0: frdp::Purple::Test
client: tc-abcdefgh-igjk-lmno-pqrs-abcdefgh
        connection-0: frdp::Purple::Test
client: tc-abcdefgh-igjk-lmno-pqrs-ijklmnop
client: tc-abcdefg-higj-klmn-opqr-qrstuvwx
        connection-0: frdp::Purple::Test
client: tc-abcdefgh-igjk-lmno-pqrs-12345678
        connection-0: frdp::Green::Test
client: tc-abcdefg-higj-klmn-opqr-09876543
        connection-0: frdp::Purple::Test
client: tc-abcdefgh-igjk-lmno-pqrs-135792468
        connection-0: frdp::Green::Test
client: tc-abcdefgh-igjk-lmno-pqrs-08649753
        connection-0: frdp::Purple::Test

编辑:我没有考虑到我的不明确性。我正在寻找的输出应该如下所示:

tc-abcdefgh-igjk-lmno-pqrs-abcdefgh
tc-abcdefgh-igjk-lmno-pqrs-ijklmnop
tc-abcdefg-higj-klmn-opqr-qrstuvwx
tc-abcdefg-higj-klmn-opqr-09876543
tc-abcdefgh-igjk-lmno-pqrs-08649753

我只想要 5 个仅与 Purple 连接但没有连接的客户端。

答案1

在每个 Unix 机器上的任何 shell 中使用任何 awk:

$ cat tst.awk
$1 == "client:" {
    prt()
}
/connection/ {
    gotConns = 1
    if ( /Purple/ ) {
        gotTarget = 1
    }
    else {
        gotOther = 1
    }
}
$1 == "hostname:" {
    host = $2 ORS
}
END {
    prt()
}

function prt(   found) {
    if ( (gotTarget && !gotOther) || !gotConns ) {
        printf "%s", host
    }
    host = gotConns = gotTarget = gotOther = ""
}

$ awk -f tst.awk file
tc-abcdefgh-igjk-lmno-pqrs-abcdefgh
tc-abcdefgh-igjk-lmno-pqrs-ijklmnop
tc-abcdefgh-igjk-lmno-pqrs-qrstuvwx
tc-abcdefgh-igjk-lmno-pqrs-09876543
tc-abcdefgh-igjk-lmno-pqrs-08649753

答案2

如果我理解正确的话:

$ tr -s '\n' ' ' < file | \
  sed 's/client:/\n/g' | \
  grep -o 'hostname: [^ ]\+ connection-0.*Purple\|hostname: [^ ]\+ $' | \
  cut -d' ' -f2
tc-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
tc-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
tc-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
tc-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
tc-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

  • $ tr -s '\n' ' '将文件转换为单个字符串。

  • sed 's/client:/\n/g'将“client”替换为换行符。

  • grep -o 'hostname: [^ ]\+ connection-0.*Purple\|hostname: [^ ]\+ $'捕获具有“connection-0...Purple”的线路或没有连接的线路。

    hostname: tc-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx connection-0: frdp::Purple
    hostname: tc-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx 
    hostname: tc-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx connection-0: frdp::Purple
    hostname: tc-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx connection-0: frdp::Purple
    hostname: tc-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx connection-0: frdp::Purple
    
  • cut -d' ' -f2仅打印主机名。


要写入新文件,请附加> newfile.

相关内容