Bourne shell script to convert numbers to phone format

I want to change a number, such as 1234567890 - 456-7890; is there any way to do this in Unix Shell programming?

+3
source share
5 answers

This is what DigitalRoss and Jonathan Leffler tried to say:

echo "1234567890"|sed 's/[0-9][0-9][0-9]\([0-9][0-9][0-9]\)\([0-9][0-9][0-9][0-9]\)/\1-\2/'

, sed, .., . , . , , , .

+2

@OP, . bash Solaris 10, , .

 $ n=1234567890    
 $ echo "${n:3:3}-${n:6}"
 456-7890
+3
$ sed -e 's/...\(...\)\(....\)/\1-\2/'
1234567890
456-7890

, , :

sed -e 's/\b[0-9]..\([0-9]..\)\([0-9]...\)\b/\1-\2/'
+1

G'day,

, . - :

echo "1234567890" | sed -e 's/\([0-9]{3}\)\([0-9]{4}\)$/\1-\2/'

,

+1

- , , (, Python/Ruby/Perl), sed/awk/grep.

, Ruby:

$ echo "Change 1234567890 to 456-7890" | ruby -e 'puts $stdin.read.gsub(/\d{3}(\d{3})(\d{3})/){|x| "#{$1}-#{$2}"}'

.. :

Change 456-7890 to 456-7890

() Ruby over sed:

  • Ruby/etc , sed/awk/grep, , , , , /
  • , "" script
0

Source: https://habr.com/ru/post/1720405/


All Articles