如何使用shell脚本将美国电话号码转换为E.164格式?
我有一个包含我们电话号码的文本文件。我正在编写一个shell脚本,将它们转换为E.164格式 我想我只需要删除除数字以外的所有字符,然后在前面加上“+1”。因此,如果电话号码看起来像如何使用shell脚本将美国电话号码转换为E.164格式?,shell,Shell,我有一个包含我们电话号码的文本文件。我正在编写一个shell脚本,将它们转换为E.164格式 我想我只需要删除除数字以外的所有字符,然后在前面加上“+1”。因此,如果电话号码看起来像(800)123-4567或1-800-123-4567,例如,它的格式应该是+18001234567 我如何做到这一点,仅仅使用shell脚本 类似问题 -相同的问题,但使用Java,需要适用于所有国际电话号码(不仅仅是美国) 只是壳 #/垃圾箱/垃圾箱 phonefile=“数字” 执行0“${outfile
(800)123-4567
或1-800-123-4567
,例如,它的格式应该是+18001234567
我如何做到这一点,仅仅使用shell脚本
类似问题
- -相同的问题,但使用Java,需要适用于所有国际电话号码(不仅仅是美国)
#/垃圾箱/垃圾箱
phonefile=“数字”
执行0<“${phonefile}”
outfile=“e164”
执行1>“${outfile}”
如果s='()-'
读数字时
做
如果[-z“${number%%1*}”];然后
printf“+”
其他的
打印F“+1”
fi
printf“%s”${number}
printf“\n”
完成
更一般的版本:
#!/bin/sh
numberfilter() {
local phonefile="$1"
local outfile="$2"
local filter="$3"
local countrycode="$4"
exec 3<&0
exec 0< "${phonefile}"
exec 4<&1
exec 1> "${outfile}"
local oldIFS="$IFS"
IFS='()- '
while read number
do
case ${number} in
+*) ;;
011*) number=${number#011} ; printf '+' ;;
00*) number=${number#00} ; printf '+' ;;
1*) printf '+' ;;
*) printf "+${countrycode}" ;;
esac
printf '%s' ${number}
printf '\n'
done
IFS="${oldIFS}"
exec 0<&3
exec 3<&-
exec 1<&4
exec 4<&-
}
numberfilter numbers e164_1 '()- ' 1
numberfilter numbers e164_55 '()- ' 55
#/垃圾箱/垃圾箱
数字过滤器(){
本地电话文件=“$1”
本地输出文件=“$2”
本地筛选器=“$3”
本地国家代码=“$4”
exec 3是否每行一个数字?您是否有示例输入和输出?您是否从这一点开始并失败?具体如何?是每行一个数字,例如:(800)123-4567
在第一行,第二行,1-800-123-4567
在第二行,等等。试图找出是否有其他人已经这样做了。目前正在尝试使用类似于e164=$(echo“+1$(echo”$phone_number“tr-dc'0-9')的内容)
效果很好,谢谢!我用1-800-555-1212
、800-555-1212
和(800)555-1212
测试了这一点,这三种方法都有效!
#!/bin/sh
numberfilter() {
local phonefile="$1"
local outfile="$2"
local filter="$3"
local countrycode="$4"
exec 3<&0
exec 0< "${phonefile}"
exec 4<&1
exec 1> "${outfile}"
local oldIFS="$IFS"
IFS='()- '
while read number
do
case ${number} in
+*) ;;
011*) number=${number#011} ; printf '+' ;;
00*) number=${number#00} ; printf '+' ;;
1*) printf '+' ;;
*) printf "+${countrycode}" ;;
esac
printf '%s' ${number}
printf '\n'
done
IFS="${oldIFS}"
exec 0<&3
exec 3<&-
exec 1<&4
exec 4<&-
}
numberfilter numbers e164_1 '()- ' 1
numberfilter numbers e164_55 '()- ' 55