【问题标题】:Bash Regular Expression MatchingBash 正则表达式匹配
【发布时间】:2016-10-23 06:56:34
【问题描述】:

我有如下这样的文字:

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产生了想要的结果。

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

以下是我的 bash 代码

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

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

我没有正确使用 bash 正则表达式语法吗?

【问题讨论】:

  • ${var%%pattern} 不使用正则表达式。为什么会这样?
  • 我想删除一个子字符串,那么我还能如何使用没有正则表达式的模式?
  • @melpomene 伙计,你太棒了.. 现在我想我可以试着弄清楚了! Bash 太棒了!!!!

标签: regex bash text-parsing


【解决方案1】:

%% 之后的部分不是正则表达式,而是 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"

【讨论】:

  • Countermeasures52s 不起作用...我正在尝试类似?(+([0-9])m)?( )(+([0-9])s) 的东西,它修复了Countermeasures,但留下了所有其他的东西,例如Some Random Text11mSome Other Text4m.. 等等.. 什么我想念男人吗?
  • 这是一个错字。?(@(+([0-9])m +([0-9])s)|+([0-9])s) 这似乎工作正常。
猜你喜欢
  • 2011-10-17
  • 2023-03-14
  • 2013-08-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多