Shell script mask or replace ip-address with string

Using regular expression in shell script we can easily search and replace all ip address to a specific character or a string using sed command.

$ sed -e 's/[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}/x.x.x.x/g' file.txt

above command will give the output by replace the all ip-address using the matching patter and replace it with x.x.x.x. Transfer the output to a tmporay files and replace the file.

Using grep we can get the list of all IP-Address available in a file

$ grep -Eo "(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]).){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])" file.txt

above command will extract all IP-Address with in the file.

Output:
192.168.25.123
192.168.35.15
192.168.22.1
192.168.25.4
192.168.25.24
Facebook Facebook Twitter Reddit

Leave a Reply