我有文件调用phonenum.txt,其中包含已通过procmail配方从文件中过滤的电话号码列表:
60123456098,60135673434,60149023459,60123456334
基于此文件,需要满足两个条件才能获得输出。
无论如何,我可以通过在awk或sed中使用匹配正则表达式来获取电话号码吗?
答案 0 :(得分:1)
awk -F, '{for(i=1;i<=NF;i++)if($i~/^60/){print "phone number is ",substr($i,3)}}' your_file
下面测试:
> cat temp
60123456098,60135673434,60149023459,60123456334,90123456789
> nawk -F, '{for(i=1;i<=NF;i++)if($i~/^60/){print "phone number is ",substr($i,3)}}' temp
phone number is 123456098
phone number is 135673434
phone number is 149023459
phone number is 123456334
>
答案 1 :(得分:1)
你可以做到
awk -v RS="," '{gsub(/^60/,"")} 1' file
如果你想要一条线回来
awk -v RS="," '{gsub(/^60/,"")} {printf s $0; s=","}' file
答案 2 :(得分:0)
sed
使用(^|,)60
进行全局替换:
$ sed -r 's/(^|,)60/\1/g' <<< 60123456098,60135673434,60149023459,60123456334
123456098,135673434,149023459,123456334
Regexplanation:
s/ # Substitution
(^|,) # Match start of the line or comma (captured)
60 # Followed by the literal 60
/ # Replace with
\1 # Captured group (put back the comma)
/g # Global flag
答案 3 :(得分:0)
$ grep -oP '(?<=\b60)\d+' <<< "60123456098,60135673434,60149023459,60123456334"
123456098
135673434
149023459
123456334
答案 4 :(得分:0)
这可能适合你(GNU sed):
sed 's/^60//;s/,60/\n/g;P;D' file