Bash 正则表达式 returns 来自 AT 命令 AT+COPS 的短字母数字运算符名称

Bash regex that returns short alphanumeric operator name from AT command AT+COPS

我正在使用 bash 脚本和 AT 命令 AT+COPS=?其中 returns 以下字符串 :

+COPS: (1,"Orange F","Orange","20801",2),(1,"Swisscom","Swisscom","22801",7),(1,"Swisscom","Swisscom","22801",2),(1,"Salt","Salt","22803",2),(1,"Sunrise","Sunrise","22802",2),(1,"Sunrise","Sunrise","22802",7),(1,"Sunrise","Sunrise","22802",0),(2,"Salt","Salt","22803",7),(1,"Free","Free","20815",2),(1,"F SFR","SFR","20810",7),(1,"F-Bouygues Telecom","BYTEL","20820",7),,(0-4),(0-2)

我正试图找到一个正则表达式来匹配每个短的字母数字运算符名称。所以在这里,它是:

Orange Swisscom Swisscom Salt Sunrise Sunrise Sunrise Salt Free SFR BYTEL

比如在群里

(1,"F-Bouygues Telecom","BYTEL","20820",7)

有趣的是 'BYTEL' 部分。 'BYTEL' 也可以是小写字符和数字。

我尝试了多种解决方案,但它们都不是 100% 匹配。目前,我正在使用:

 grep -oP '"([a-zA-z])\w+"'

但它在某些特殊情况下不起作用,并且还匹配长字母数字运算符名称(引号之间的名字)。

这是一种讨厌的解析格式。我想到了

fugly command |
grep -o '([^()]*)' |
awk -F , 'NF==5 { x=; gsub(/\"/, "", x); print x }'

这在某种意义上并不令人满意,但我希望它足够容易理解。

演示:https://ideone.com/jH0NKj

这是一个简化的纯 Awk 变体,假设略有不同:

awk -F , -v RS='(' 'NF>=5 { x=; gsub(/\"/, "", x); print x}'

演示:https://ideone.com/UvEXXH

试试这个 sed 变体

sed 's/(/\n/g' file | sed -n 's/.,".*","\(.*\)",".*".*//p'

你的测试字符串在文件中,输出

Orange
Swisscom
Swisscom
Salt
Sunrise
Sunrise
Sunrise
Salt
Free
SFR
BYTEL

结合使用 sed 和 awk

sed 's/(/\n/g' file | awk -F, '{print gensub(/\"/,"","g", )}'

Orange
Swisscom
Swisscom
Salt
Sunrise
Sunrise
Sunrise
Salt
Free
SFR
BYTEL