Sed Append – Appending to Input

sed append

I had the need to add a port number to a list of IP addresses and I couldn’t find anything that I could copy & paste from my google searches so I decided to implement it myself and share it for others who in the future might need to do something similar. In this artcile I will display sed append with that example.

Sed Append

In my case I had a list looking like the following:

One way to sed append would be to use /a. However, this appends on a newline. A sed append on the same line can be done with /\1 and the sed expression that I came up with is the following.

An example of it in use can be seen below.


$ echo '"10.216.53.1", "10.216.53.5", "10.216.53.21"' | sed -r 's/(([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3}))/\1:8080/g'
$ "10.216.53.1:8080", "10.216.53.5:8080", "10.216.53.21:8080"
Conclusion

We have looked at how we, in an easy way, can modify an input of IP addresses and add a port to them using basic tools available in UNIX. Naturally, the command can easily be modified to modify and add other stuff. Simply switch the regexp pattern to something that you want to match against and switch the port number to something else, like the example below, simply replace [].

Leave a Reply

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