1

I have a file created from a command I run on my system. After the file is created I want to extract only specific information from that file but only it it meets certain criteria.

Here is what my file looks like:

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

I want to extract just the client hostnames that just show a connection to Purple and clients with no connections into a new file. I have been able to break out just hostnames or connections using awk but not specific ones without a connection.

EDIT: I have added entries into the file.

4
  • Do you control the generation of this file? That is, is this output of a program you have written? More generally, what command is generating this output? why is this a question about Ubuntu?
    – James S.
    Oct 18, 2021 at 16:36
  • The command that generates the list is proprietary and the system runs on top of Ubuntu. The file is generate by running the command and redirecting the output into a file. This is the format after the command is run.
    – scott20041
    Oct 18, 2021 at 17:40
  • Lots of applications run "on top of Ubuntu", but support of those applications is not properly a question about Ubuntu. You are asking a general text processing question, and there are several thousand ways to parse that file. You might try asking in the more general unix and linux stackexchange.
    – James S.
    Oct 18, 2021 at 17:51
  • Thanks for you help. I will do that.
    – scott20041
    Oct 18, 2021 at 17:52

1 Answer 1

0

If I understand correctly what you want, it is to filter away clients with connections other than 'Purple' (only 'Green' in your example, but possibly other colours).

The following commands use grep to remove lines with 'Purple', tr to make one line and sed to put line breaks before 'client' and finally grep to remove remaining lines with 'connection' and to select only the desired piece of information for each client.

The whole information set:

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

The hostname and whatever listed behind:

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

Only hostname, which you wanted according to what I could read:

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

The other group (that should be filtered out) with hostname and connection other than 'Purple':

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .