从文件中提取特定信息

从文件中提取特定信息

我有一个通过我在系统上运行的命令创建的文件。创建文件后,我想从该文件中提取特定信息,但前提是它符合某些条件。

我的文件如下所示:

client: tc-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
        version: xxxx
        uuid: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxx
        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-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
        connection-1: frdp::Green::Test
        connection-0: frdp::Purple::Test
client: tc-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
        version: xxxx
        uuid: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxx
        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-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
        connection-0: frdp::Purple::Test
client: tc-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
        version: xxxx
        uuid: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxx
        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-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
client: tc-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
        version: xxxx
        uuid: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxx
        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-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
        connection-0: frdp::Purple::Test
client: tc-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
        version: xxxx
        uuid: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxx
        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-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
client: tc-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
        version: xxxx
        uuid: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxx
        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-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
        connection-0: frdp::Purple::Test
client: tc-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
        version: xxxx
        uuid: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxx
        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-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
        connection-0: frdp::Green::Test
client: tc-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
        version: xxxx
        uuid: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxx
        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-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
        connection-1: frdp::Green::Test
        connection-0: frdp::Purple::Test

我想将只显示与 Purple 连接的客户端主机名和没有连接的客户端提取到一个新文件中。我能够使用 awk 仅提取主机名或连接,但无法提取没有连接的特定主机名或连接。

编辑:我已将条目添加到文件中。

答案1

如果我正确理解了您的意思,那就是过滤掉除“紫色”之外的其他连接的客户端(在您的例子中只有“绿色”,但可能是其他颜色)。

以下命令用于grep删除带有“Purple”的行,tr生成一行并sed在“client”前放置换行符,最后grep删除带有“connection”的剩余行并仅为每个客户端选择所需的信息。

全部信息集:

< myfile grep -v 'Purple' | tr '\n' ' '| tr -s ' ' ' ' | sed "s/ client/\nclient/g" \
| grep -v 'connection'

主机名和后面列出的内容:

< myfile grep -v 'Purple' | tr '\n' ' '| tr -s ' ' ' '  | sed "s/ client/\nclient/g" \
| grep -v 'connection' | grep -o 'hostname: .*'

根据我所读的内容,您想要的只有主机名:

< myfile grep -v 'Purple' | tr '\n' ' '| tr -s ' ' ' '  | sed "s/ client/\nclient/g" \
| grep -v 'connection' | grep -o 'hostname: [^ ]*'

另一组(应该被过滤掉)的主机名和连接不是“紫色”:

< myfile grep -v 'Purple' | tr '\n' ' '| tr -s ' ' ' '  | sed "s/ client/\nclient/g" \
| grep 'connection' | grep -o 'hostname: .*'

相关内容