grep - 使用 Bash 从 Cargo.toml 中提取 bin 名称
问题描述
我正在尝试使用 Bash 从 Cargo.toml 中提取 bin 名称,我启用了这样的 perl 正则表达式
第一次尝试
grep -Pzo '(?<=(^\[\[bin\]\]))\s*name\s*=\s*"(.*)"' ./Cargo.toml
正则表达式在regex101 测试 但一无所获
Pzo
选项用法可以在这里找到
第二次尝试
grep -P (?<=(^[[bin]]))\n*\s名称\s =\s*"(.*)" ./Cargo.toml
依然没有
grep -Pzo '(?<=(^\[\[bin\]\]))\s*name\s*=\s*"(.*)"' ./Cargo.toml
货运.toml
[[bin]]
name = "acme1"
path = "bin/acme1.rs"
[[bin]]
name = "acme2"
path = "src/acme1.rs"
解决方案
使用您展示的示例和尝试,请尝试使用tac
+awk
组合的以下代码,这将更易于维护,并且可以轻松完成工作,这在grep
.
tac Input_file |
awk '
/^name =/{
gsub(/"/,"",$NF)
value=$NF
next
}
/^path[[:space:]]+=[[:space:]]+"bin\//{
print value
value=""
}
' |
tac
说明:为上述代码添加详细说明。
tac Input_file | ##Using tac command on Input_file to print it in bottom to top order.
awk ' ##passing tac output to awk as standard input.
/^name =/{ ##Checking if line starts from name = then do following.
gsub(/"/,"",$NF) ##Globally substituting " with NULL in last field.
value=$NF ##Setting value to last field value here.
next ##next will skip all further statements from here.
}
/^path[[:space:]]+=[[:space:]]+"bin\//{ ##Checking if line starts from path followed by space = followed by spaces followed by "bin/ here.
print value ##printing value here.
value="" ##Nullifying value here.
}
' | ##Passing awk program output as input to tac here.
tac ##Printing values in their actual order.
推荐阅读
- ios - Swift 3 中的 Moscapsule,在 iPhone 屏幕关闭时断开连接
- html - 如何根据通过 Selenium 的 html 通过 onclick 事件单击元素?
- javascript - 如果页面在离线时加载/刷新,JavaScript 后台同步将停止工作
- angular - 角度为 4 的 Ng-bootstrap 未显示工具提示
- javascript - JS函数在Angular5应用程序中不起作用
- php - yii2上传文件给空白数组
- javascript - 带有 LineSegmentsGeometry 和 LineSegments2 的threejs linewidth 属性
- javascript - 在 calc() 函数中使用变量
- python - Python:如何将一个数字向下舍入到下一个 1000
- c# - 使用来自 seckeygeneratepair 的 publicKey 进行加密