Matching an IP Address using sed

Regular expression for matching an IP address.

sed ‘s/\([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}/&HelloWorld/gp’ infile > outfile

will append the string HelloWorld directly after an ipaddress in a file containing ipaddresses e.g.

10.200.200.10 blah

with

10.200.200.10HelloWorld blah

This can be adjusted to replace ip addresses with a string or used to inject some whitespace between an ipaddress and a string as required.

It looks more complicated than it is due to the many escape characters \ needed in the regular expression to tell the sed command that you’re passing a command and not a character to be matched.

Did you like this?
Tip cyberfella with Cryptocurrency

Donate Bitcoin to cyberfella

Scan to Donate Bitcoin to cyberfella
Scan the QR code or copy the address below into your wallet to send some bitcoin:

Donate Bitcoin Cash to cyberfella

Scan to Donate Bitcoin Cash to cyberfella
Scan the QR code or copy the address below into your wallet to send bitcoin:

Donate Ethereum to cyberfella

Scan to Donate Ethereum to cyberfella
Scan the QR code or copy the address below into your wallet to send some Ether:

Donate Litecoin to cyberfella

Scan to Donate Litecoin to cyberfella
Scan the QR code or copy the address below into your wallet to send some Litecoin:

Donate Monero to cyberfella

Scan to Donate Monero to cyberfella
Scan the QR code or copy the address below into your wallet to send some Monero:

Donate ZCash to cyberfella

Scan to Donate ZCash to cyberfella
Scan the QR code or copy the address below into your wallet to send some ZCash:

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.