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

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

    This works fine for me in access logs.

    cat access_log | egrep -o '([0-9]{1,3}\.){3}[0-9]{1,3}'
    

    Let's break it part by part.

    • [0-9]{1,3} means one to three occurrences of the range mentioned in []. In this case it is 0-9. so it matches patterns like 10 or 183.

    • Followed by a '.'. We will need to escape this as '.' is a meta character and has special meaning for the shell.

    So now we are at patterns like '123.' '12.' etc.

    • This pattern repeats itself three times(with the '.'). So we enclose it in brackets. ([0-9]{1,3}\.){3}

    • And lastly the pattern repeats itself but this time without the '.'. That is why we kept it separately in the 3rd step. [0-9]{1,3}

    If the ips are at the beginning of each line as in my case use:

    egrep -o '^([0-9]{1,3}\.){3}[0-9]{1,3}'
    

    where '^' is an anchor that tells to search at the start of a line.

    0 讨论(0)
提交回复
热议问题