For example, I get a phone number like 9191234567
, how could I separate it into two parts, with the first part containing the three leading digits 919
and the other part containing the rest seven digits 1234567
? After that, I want to store these two parts into two different variables in ksh
.
I don't know if this could be done with sed
?
You could try this :
echo "9191234567" | sed 's/^\([0-9]\{3\}\)\([0-9]\{7\}\)$/\1 \2/'
To store each part in a separate variable, you could do this :
phone="9191234567"
part1=$(echo $phone | sed 's/^\([0-9]\{3\}\)[0-9]\{7\}$/\1/')
part2=$(echo $phone | sed 's/^[0-9]\{3\}\([0-9]\{7\}\)$/\1/')
Or even more concise :
read part1 part2 <<< $(echo "9191234567" | sed 's/^\([0-9]\{3\}\)\([0-9]\{7\}\)$/\1 \2/')
cut
should work
echo '9191234567' | cut --characters 1-3,4- --output-delimiter ' '
919 1234567
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With