Bash正则表达式匹配

时间:2016-10-23 06:56:35

标签: regex bash text-parsing

我的文字如下:

Insights into Wireless
53m 16s
Insights into Wireless8m 34s
New Terms to Learn11m 19s
Advantages & Disadvantages5m 50s
Types of Wi-Fi Networks2m 39s
Wi-Fi Standards7m 24s
Wi-Fi Authentication Modes3m 11s
Chalking4m 38s
Antenna Types7m 22s
Summary2m 17s
Encryption in Wireless
23m 50s
Encryption in Wireless1m 47s
WEP Encryption3m 52s
WPA & WPA2 Encryption7m 40s
Breaking Encryption6m 7s
Defending Against Cracking2m 23s
Summary1m 58s
Threats from Wireless
18m 23s
Threats from Wireless1m 38s
Types of Attacks7m 28s
Attack on the AP5m 29s
Attack on the Client2m 43s
Summary1m 3s
The Methodology of Hacking Wireless
45m 20s
The Method of Hacking Wireless1m 8s
Wi-Fi Discovery4m 47s
GPS Mapping13m 51s
Wireless Traffic Analysis6m 9s
Launching Attacks4m 59s
Let's Go Look-ng3m 52s
Cracking Wi-Fi Encryption2m 7s
Let's See How Much Damage We Can Do!6m 59s
Summary1m 24s
Hacking Bluetooth
17m 23s
Hacking Bluetooth1m 0s
The Threats1m 40s
New Terms3m 26s
All About Bluetooth4m 14s
Security5m 43s
Summary1m 18s
Countermeasures
18m 51s
Countermeasures52s
Bluetooth1m 45s
Rogue APs2m 6s
6 Layers of Wireless3m 22s
Best Practices6m 55s
Tools2m 1s
Summary1m 47s

我希望从每一行的末尾(如果有的话)中删除持续时间并保留标题,如下所述 -

Insights into Wireless8m 34s

应转换为:

Insights into Wireless

我在网上使用了tool,发现([0-9]{1,2})m\s([0-9]{1,2})s会产生预期效果。

  

但是,在我的bash脚本中使用此正则表达式不会产生任何结果!

以下是 bash代码

name="${string%%'([0-9]{1,2})m\s([0-9]{1,2})s'}"

$ string 包含最顶层给定示例文本中的每一行。

我是否正确使用正则表达式的bash 语法?

1 个答案:

答案 0 :(得分:2)

%%之后的部分不是正则表达式,而是一个glob模式(用于文件名的内容,如*.jpg)。

您可以使用bash' extended glob patterns执行此操作。要启用它们,您必须将shopt -s extglob放在脚本的开头。这是一个例子:

shopt -s extglob

string='GPS Mapping13m 51s'
name="${string%%+([0-9])m +([0-9])s}"
echo "$name"