Skip to main content
added using it inside a script
Source Link

Tested in SmartOS (a variant of Solaris), hopefully should work in other *nix environments:

egrep '(([0-9]|[0-9]{2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[0-9]{2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])'

Example:

$ cat >file.txt
IP1: 192.168.1.1
IP2: 261.480.201.311
IP3: 1012.680.921.3411

$ egrep '(([0-9]|[0-9]{2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[0-9]{2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])' file.txt
IP1: 192.168.1.1

This pattern matches only valid IPv4, i.e, x.x.x.x where x range from 0-255. Should you need to extract only the matched IP, add an -o option to the above command. You could embed this command in a bash script and presumably in other shell scripts as well. And, if egrep fails, try grep -E ...

Using it in a (bash) shell script:

ip=$(egrep -o '(([0-9]|[0-9]{2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[0-9]{2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])' file.txt) echo $ip

Tested in SmartOS (a variant of Solaris), hopefully should work in other *nix environments:

egrep '(([0-9]|[0-9]{2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[0-9]{2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])'

Example:

$ cat >file.txt
IP1: 192.168.1.1
IP2: 261.480.201.311
IP3: 1012.680.921.3411

$ egrep '(([0-9]|[0-9]{2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[0-9]{2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])' file.txt
IP1: 192.168.1.1

This pattern matches only valid IPv4, i.e, x.x.x.x where x range from 0-255. Should you need to extract only the matched IP, add an -o option to the above command. You could embed this command in a bash script and presumably in other shell scripts as well. And, if egrep fails, try grep -E ...

Tested in SmartOS (a variant of Solaris), hopefully should work in other *nix environments:

egrep '(([0-9]|[0-9]{2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[0-9]{2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])'

Example:

$ cat >file.txt
IP1: 192.168.1.1
IP2: 261.480.201.311
IP3: 1012.680.921.3411

$ egrep '(([0-9]|[0-9]{2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[0-9]{2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])' file.txt
IP1: 192.168.1.1

This pattern matches only valid IPv4, i.e, x.x.x.x where x range from 0-255. Should you need to extract only the matched IP, add an -o option to the above command. You could embed this command in a bash script and presumably in other shell scripts as well. And, if egrep fails, try grep -E ...

Using it in a (bash) shell script:

ip=$(egrep -o '(([0-9]|[0-9]{2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[0-9]{2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])' file.txt) echo $ip

corrected spelling
Source Link

Tested in SmartOS (a variant of Solaris), hopefully should work in other *nix environments:

egrep '(([0-9]|[0-9]{2}|1[0-9]{2}|2[0-5][04][0-9]|25[0-5])\.){3}([0-9]|[0-9]{2}|1[0-9]{2}|2[0-5][04][0-9]|25[0-5])'

Example:

$ cat >file.txt
IP1: 192.168.1.1
IP2: 261.480.201.311
IP3: 1012.680.921.3411

$ egrep '(([0-9]|[0-9]{2}|1[0-9]{2}|2[0-5][04][0-9]|25[0-5])\.){3}([0-9]|[0-9]{2}|1[0-9]{2}|2[0-5][04][0-9]|25[0-5])' file.txt
IP1: 192.168.1.1

This pattern matches only valid IPv4, i.e, x.x.x.x where x range from 0-255. Should you need to extract only the matched IP, add an -o option to the above command. You could embed this command in a bash script and presumably in other shell scripts as well. And, if egrep fails, try grep -E ...

Tested in SmartOS (a variant of Solaris), hopefully should work in other *nix environments:

egrep '(([0-9]|[0-9]{2}|1[0-9]{2}|2[0-5][0-5])\.){3}([0-9]|[0-9]{2}|1[0-9]{2}|2[0-5][0-5])'

Example:

$ cat >file.txt
IP1: 192.168.1.1
IP2: 261.480.201.311
IP3: 1012.680.921.3411

$ egrep '(([0-9]|[0-9]{2}|1[0-9]{2}|2[0-5][0-5])\.){3}([0-9]|[0-9]{2}|1[0-9]{2}|2[0-5][0-5])' file.txt
IP1: 192.168.1.1

This pattern matches only valid IPv4, i.e, x.x.x.x where x range from 0-255. Should you need to extract only the matched IP, add an -o option to the above command. You could embed this command in a bash script and presumably in other shell scripts as well. And, if egrep fails, try grep -E ...

Tested in SmartOS (a variant of Solaris), hopefully should work in other *nix environments:

egrep '(([0-9]|[0-9]{2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[0-9]{2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])'

Example:

$ cat >file.txt
IP1: 192.168.1.1
IP2: 261.480.201.311
IP3: 1012.680.921.3411

$ egrep '(([0-9]|[0-9]{2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[0-9]{2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])' file.txt
IP1: 192.168.1.1

This pattern matches only valid IPv4, i.e, x.x.x.x where x range from 0-255. Should you need to extract only the matched IP, add an -o option to the above command. You could embed this command in a bash script and presumably in other shell scripts as well. And, if egrep fails, try grep -E ...

Better usage of the wiki syntax
Source Link

Tested in SmartOS (a variant of Solaris), hopefully should work in other *nix environments: egrep '(([0-9]|[0-9]{2}|1[0-9]{2}|2[0-5][0-5])\.){3}([0-9]|[0-9]{2}|1[0-9]{2}|2[0-5][0-5])'

egrep '(([0-9]|[0-9]{2}|1[0-9]{2}|2[0-5][0-5])\.){3}([0-9]|[0-9]{2}|1[0-9]{2}|2[0-5][0-5])'

Example: $ cat >file.txt IP1: 192.168.1.1 IP2: 261.480.201.311 IP3: 1012.680.921.3411

$ egrep '(([0-9]|[0-9]{2}|1[0-9]{2}|2[0-5][0-5])\.){3}([0-9]|[0-9]{2}|1[0-9]{2}|2[0-5][0-5])' file.txt IP1: 192.168.1.1

$ cat >file.txt
IP1: 192.168.1.1
IP2: 261.480.201.311
IP3: 1012.680.921.3411

$ egrep '(([0-9]|[0-9]{2}|1[0-9]{2}|2[0-5][0-5])\.){3}([0-9]|[0-9]{2}|1[0-9]{2}|2[0-5][0-5])' file.txt
IP1: 192.168.1.1

This pattern matches only valid IPv4, i.e, x.x.x.xx.x.x.x where xx range from 0-255. Should you need to extract only the matched IP, add an -o option to the above command. A better suggestion is welcome :)

PS: You could embed this command in a bash script and presumably in other shell scripts as well. And, if egrep fails, try grep -E ...

Tested in SmartOS (a variant of Solaris), hopefully should work in other *nix environments: egrep '(([0-9]|[0-9]{2}|1[0-9]{2}|2[0-5][0-5])\.){3}([0-9]|[0-9]{2}|1[0-9]{2}|2[0-5][0-5])'

Example: $ cat >file.txt IP1: 192.168.1.1 IP2: 261.480.201.311 IP3: 1012.680.921.3411

$ egrep '(([0-9]|[0-9]{2}|1[0-9]{2}|2[0-5][0-5])\.){3}([0-9]|[0-9]{2}|1[0-9]{2}|2[0-5][0-5])' file.txt IP1: 192.168.1.1

This pattern matches only valid IPv4, i.e, x.x.x.x where x range from 0-255. Should you need to extract only the matched IP, add an -o option to the above command. A better suggestion is welcome :)

PS: You could embed this command in a bash script and presumably in other shell scripts as well. And, if egrep fails, try grep -E ...

Tested in SmartOS (a variant of Solaris), hopefully should work in other *nix environments:

egrep '(([0-9]|[0-9]{2}|1[0-9]{2}|2[0-5][0-5])\.){3}([0-9]|[0-9]{2}|1[0-9]{2}|2[0-5][0-5])'

Example:

$ cat >file.txt
IP1: 192.168.1.1
IP2: 261.480.201.311
IP3: 1012.680.921.3411

$ egrep '(([0-9]|[0-9]{2}|1[0-9]{2}|2[0-5][0-5])\.){3}([0-9]|[0-9]{2}|1[0-9]{2}|2[0-5][0-5])' file.txt
IP1: 192.168.1.1

This pattern matches only valid IPv4, i.e, x.x.x.x where x range from 0-255. Should you need to extract only the matched IP, add an -o option to the above command. You could embed this command in a bash script and presumably in other shell scripts as well. And, if egrep fails, try grep -E ...

Suggested an alternate and made the answer more relevant to the question
Source Link
Loading
Source Link
Loading