0

I have a list of IP ranges in the following format:

Long description:111.22.33.0-111.22.33.40
Another description:5.5.5.0-5.5.5.100
Yet another description:111.22.33.0-111.22.33.40
And another one:111.22.33.0-111.22.33.40
Something different:8.1.1.0-8.1.1.20
etc.

I'd like to delete lines having duplicate IP ranges, even when the "description" differs (e.g. only match text after the : character).

The example above should become:

Long description:111.22.33.0-111.22.33.40
Another description:5.5.5.0-5.5.5.100
Something different:8.1.1.0-8.1.1.20
0

1 Answer 1

2

You could ask sort for the unique lines based on the 2nd field, delimited by colons:

sort -u -t: -k2 < input > output

The hard work is done by -u, which:

output(s) only the first of an equal run

(my emphasis)

Reference:

2
  • Thanks, it works. To make things more complicated, would it be possible to also delete overlapping IP ranges? e.g. 111.22.33.0-111.22.33.40 includes 111.22.33.0-111.22.33.5 which should be deleted. Commented Jul 5, 2018 at 16:17
  • It would make it more complicated, since sort -u only deals with exact textual matches. I would recommend asking a separate question. Commented Jul 5, 2018 at 16:18

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.