Bash 正则表达式匹配
Bash Regular Expression Matching
我有如下文字:
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
我希望从每一行的末尾删除 duration(如果有的话)并保留 title,如前所述下面-
Insights into Wireless8m 34s
应该转化为:
Insights into Wireless
我在网上使用了这个 tool,发现 ([0-9]{1,2})m\s([0-9]{1,2})s
产生了我想要的结果。
However, using this regex in my bash script yields no results!
下面是我的bash代码:
name="${string%%'([0-9]{1,2})m\s([0-9]{1,2})s'}"
$string 包含最顶部给定示例文本的每一行。
我没有正确使用 bash regex 的语法吗?
%%
之后的部分不是正则表达式而是 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"
我有如下文字:
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
我希望从每一行的末尾删除 duration(如果有的话)并保留 title,如前所述下面-
Insights into Wireless8m 34s
应该转化为:
Insights into Wireless
我在网上使用了这个 tool,发现 ([0-9]{1,2})m\s([0-9]{1,2})s
产生了我想要的结果。
However, using this regex in my bash script yields no results!
下面是我的bash代码:
name="${string%%'([0-9]{1,2})m\s([0-9]{1,2})s'}"
$string 包含最顶部给定示例文本的每一行。
我没有正确使用 bash regex 的语法吗?
%%
之后的部分不是正则表达式而是 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"