0

I'm trying to learn how to parse files using the Linux commands and tools. I'm always confused on how to best leverage grep/awk/sed.

Here is a specific use case.

I have a log file that contains the following strings:

Config Server received a Connection Establishment with an invalid public key, closing connection. Agent Identifier: SRV3 Socket IP: 192.168.2.6
Config Server received a Connection Establishment with an invalid public key, closing connection. Agent Identifier: TESTSRV4 Socket IP: 10.1.2.3
Config Server received a Connection Establishment with an invalid public key, closing connection. Agent Identifier: SRV1 Socket IP: 192.168.2.15
Config Server received a Connection Establishment with an invalid public key, closing connection. Agent Identifier: TESTSRV2 Socket IP: 10.1.2.4

My goal is to extract the host name that appears after "Agent Identifier" and the associated IP address for each line and export them to a txt file. What would be the best way of going about it?

2 Answers 2

2

Just do this:

$ cat file.log | awk '{ print $16, $19 }' 

And it will return a list like this:

SRV3 192.168.2.6
TESTSRV4 10.1.2.3
SRV1 192.168.2.15
TESTSRV2 10.1.2.4

You can redirect the output anywhere you like, for example, just add:

> hosts.text

to output your data to a file named hosts.txt

The above will clobber (replace) any content in the hosts.txt file. If you want to append your data to the end of your file use >> instead of >.

1
  • it is not necessary to use cat file.log |. Simply do awk '{ print $16, $19 }' file.log.
    – miracle173
    Oct 9, 2022 at 6:43
1

sed approach:

sed -n 's/.* Agent Identifier: \(.*\) Socket IP: \(.*\)/\1 \2/p' inputfile > host_list.txt

host_list.txt file contents(cat host_list.txt):

SRV3 192.168.2.6
TESTSRV4 10.1.2.3
SRV1 192.168.2.15
TESTSRV2 10.1.2.4
3
  • The sed approach might be better because there is additional data in the log file that doesn't conform to the strings that I posted in this thread. Using "Agent Identifier" as the common criteria will probably work. I will have to test.
    – Heisenberg
    Apr 20, 2017 at 21:02
  • Thanks @RomanPerekhrest. The sed approach works like a charm. Gives me a starting point to learn text processing further. I'm looking for a good resource with examples to get better using grep/sed/awk.
    – Heisenberg
    Apr 21, 2017 at 17:31
  • @Heisenberg, you're welcome. To learn awk - find the book "GAWK: Effective AWK Programming", for other tools - official documentation is good(it's also good for awk) Apr 21, 2017 at 19:48

You must log in to answer this question.

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