Compare two files using Shellscript

Asked

Viewed 632 times

1

I have a series of data collectors connected to a service. This service is provided by third parties and I do not have "admin" access to create queries as my interest. The only way to know if these collectors are active is via a URL that returns me a plain text with the IP addresses of the equipment:

["/10.0.0.110","/10.0.0.119","/10.0.0.130","/10.0.0.114"/10.0.0.110","/10.0.0.119"]

This format is bad for the end user, often comes duplicate addresses, would need something more friendly (I have 20 of these, if you have a disconnected, it is bad to identify and do not want to be tied to this task of checking)

I was able to treat "archive1" to look like this (Single and sequential values)

10.0.0.110
10.0.0.113
10.0.0.119
10.0.0.130

However, as we know users tend to show a certain laziness to relate the numbers. I have a "table2" with the following data:

10.0.0.110 ---> coletor A
[...]
10.0.0.130 ---> coletor R

I would like some suggestion to compare the occurrences in archive1 and table2. The expected return would be something like:

10.0.0.110 --> coletor A
10.0.0.113 --> coletor D
10.0.0.119 --> coletor H
10.0.0.130 --> coletor R

Thanks in advance.

I managed to solve with the tip of friend @Jjoao.

$join arquivo1 tabela2 > combinados

"Join" requires the files to be ordered in ascending order. To do this use:

$cat arquivo |sort |uniq > arquivo1
  • 2

    I haven’t read it all, so I might be fooling myself. Have you tried to see the remote diff? To compare two files?

  • experiment join file1 tabela2 supposing that both are ordered.

  • 1

    @Jjoao, Thank you! It worked the way I needed it.

  • Could you please create an answer with the solution found? Leaving it in the question makes it erroneously unanswered. Thank you

1 answer

0


Here is the answer to the problem solution. I was able to solve it using the tip @Jjoao gave: "Join" requires the files to be ordered in ascending order. To do this use:

$cat arquivo |sort |uniq > arquivo1

Example of using the "Join command":

$join arquivo1 tabela2 > combinados

$man Join

Join - Join Lines of two files on a common field

SYNOPSIS Join [OPTION]... FILE1 FILE2

DESCRIPTION For each pair of input Lines with identical Join Fields, write a line to standard output. The default Join field is the first, delimited by whitespace. When FILE1 or FILE2 (not Both) is -, read standard input.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.