I need to pad IP addresses with Zeroes for each octet

Posted by Felipe Alvarez on Stack Overflow See other posts from Stack Overflow or by Felipe Alvarez
Published on 2010-06-13T11:13:11Z Indexed on 2010/06/13 11:42 UTC
Read the original article Hit count: 201

Filed under:
|

Starting with a string of an unspecified length, I need to make it exactly 43 characters long (front-padded with zeroes). It is going to contain IP addresses and port numbers. Something like:

### BEFORE
# Unfortunately includes ':' colon
66.35.205.123.80-137.30.123.78.52172:
### AFTER
# Colon removed.
# Digits padded to three (3) and five (5) 
# characters (for IP address and port numbers, respectively)
066.035.05.123.00080-137.030.123.078.52172  

This is similar to the output produced by tcpflow.

Programming in Bash. I can provide copy of script if required.
If it's at all possible, it would be nice to use a bash built-in, for speed. Is printf suitable for this type of thing?

© Stack Overflow or respective owner

Related posts about bash

Related posts about string-manipulation