How do you extract IP addresses from files using a regex in a linux shell?

前端 未结 19 1700
被撕碎了的回忆
被撕碎了的回忆 2020-11-28 02:43

How to extract a text part by regexp in linux shell? Lets say, I have a file where in every line is an IP address, but on a different position. What is the simplest way to e

19条回答
  •  囚心锁ツ
    2020-11-28 03:25

    I have tried all answers but all of them had one or many problems that I list a few of them.

    1. Some detected 123.456.789.111 as valid IP
    2. Some don't detect 127.0.00.1 as valid IP
    3. Some don't detect IP that start with zero like 08.8.8.8

    So here I post a regex that works on all above conditions.

    Note : I have extracted more than 2 millions IP without any problem with following regex.

    (?:(?:1\d\d|2[0-5][0-5]|2[0-4]\d|0?[1-9]\d|0?0?\d)\.){3}(?:1\d\d|2[0-5][0-5]|2[0-4]\d|0?[1-9]\d|0?0?\d)
    

提交回复
热议问题