Implementing an extended regexp to add a variable number of leading zeros based on position in a string
bash can handle this. It'll be a lot slower than perl though:
echo "1.1.1.1,Some Text Here" |
while IFS=., read -r a b c d text; do
printf "%d.%02d.%02d.%03d,%s\n" "$a" "$b" "$c" "$d" "$text"
done
1.01.01.001,Some Text Here
You haven't specifically asked for a perl
solution but here's one anyway. Personally I think this is a little easier to read, especially when broken into several lines.
First here is the one-liner:
(
echo '1.2.3.4,Some Text Here'
echo '1.01.01.1,Some Text Here'
echo '1.1.1.1,Some Number 1 Here'
echo '1.1.1.1,Some Text Referring to Document XXX Heading 1.2.3'
echo '1.2.3.4,Some \n \s \text'
) |
perl -ne '($ip, $text) = split(/,/, $_, 2); $ip = sprintf("%1d.%02d.%03d.%03d", split(/\./, $ip)); print "$ip,$text"'
Its results:
1.02.003.004,Some Text Here
1.01.001.001,Some Text Here
1.01.001.001,Some Number 1 Here
1.01.001.001,Some Text Referring to Document XXX Heading 1.2.3
1.02.003.004,Some \n \s \text
And here is the perl
script broken out and commented (the -n
flag puts an implicit while read; do ... done
loop around the code):
($ip, $text) = split(/,/, $_, 2); # Split line into two parts by comma
@octets = split(/\./, $ip) # Split IP address into octets by dots
$ip = sprintf("%1d.%02d.%03d.%03d", @octets); # Apply the formatting
print "$ip,$text" # Output the two parts
Usage: leading_zero.sh input.txt
#!/bin/bash
sed -r '
s/\.([0-9]{1,2})\.([0-9]{1,2})\.([0-9]{1,3},)/.0\1.0\2.00\3/
s/\.0*([0-9]{2})\.0*([0-9]{2})\.0*([0-9]{3})/.\1.\2.\3/
' "$1"
Explanation:
- First subtitution add certain amount of zeros to each number. 1 zero to 2 and 3 numbers, 2 zero to 4 number. Doesn't matter, how much digits already there are.
- Second substution removes all extra zeros, leaving only needed amount of numbers. 2 and 3 numbers should be contain only 2 digits. Leaves them and removes rests. Fourth number should be contain only 3 digits. Leaves them and removes rests.
input.txt
1.1.1.1,Some Text Here
1.1.1.1,Some Text Here
1.11.1.11,Some Text Referring to Document XXX Heading 1.2.3
1.1.1.1,Some Text Here
1.1.11.111,Some Text Referring to Document XXX Heading 1.2.3
1.11.1.1,Some Text Here
output.txt
1.01.01.001,Some Text Here
1.01.01.001,Some Text Here
1.11.01.011,Some Text Referring to Document XXX Heading 1.2.3
1.01.01.001,Some Text Here
1.01.11.111,Some Text Referring to Document XXX Heading 1.2.3
1.11.01.001,Some Text Here