Main Menu

Search

LINUX: GREP Command To Extract IP Addresses From Command Output Or File (How To Doc)

Below grep command can be used to extract IP addresses from a file.
grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}' <filename>

To extract IP addresses from command output below grep command can be used.

<command> | grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}'

For example, below is grep command to extract IP addresses from "ip addr" command output.

ip addr | grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}'

Below is sample output of above command.

10.10.10.137
10.20.11.12


Products to which Article Applies

All Linux Operating Systems


Additional Reference

https://www.shellhacks.com/regex-find-ip-addresses-file-grep/




tarun boyella

No comments:

Post a Comment