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

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

    If you are not given a specific file and you need to extract IP address then we need to do it recursively. grep command -> Searches a text or file for matching a given string and displays the matched string .

    grep -roE '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' | grep -oE '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}'
    

    -r We can search the entire directory tree i.e. the current directory and all levels of sub-directories. It denotes recursive searching.

    -o Print only the matching string

    -E Use extended regular expression

    If we would not have used the second grep command after the pipe we would have got the IP address along with the path where it is present

提交回复
热议问题