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

前端 未结 19 1705
被撕碎了的回忆
被撕碎了的回忆 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:21

    Everyone here is using really long-handed regular expressions but actually understanding the regex of POSIX will allow you to use a small grep command like this for printing IP addresses.

    grep -Eo "(([0-9]{1,3})\.){3}([0-9]{1,3})"
    

    (Side note) This doesn't ignore invalid IPs but it is very simple.

提交回复
热议问题