[英]Store perl regex capture groups in bash variables
I'm writing a small bash script to get the status of certain hardware through our api.我正在编写一个小的 bash 脚本来通过我们的 api 获取某些硬件的状态。
curl -sG ip:port/device/status -o temp.txt
I use a curl command to get the status and write to a temp file我使用 curl 命令获取状态并写入临时文件
I played around with different regex types, the simple bash regex was not enough and ended up using perl.我尝试了不同的正则表达式类型,简单的 bash 正则表达式还不够,最终使用了 perl。
perl -nle 'printf("'%-15s' \t '%-15s' \t '%-15s' \t '%-15s' \n", "$1", "$2", "$3", "$4") while m{regex_to_capture_data}g' temp.txt
My regex capture is working and prints out perfectly (some of the iterations will have blank capture groups, which is important info, and that prints as expected)我的正则表达式捕获正在工作并完美打印(一些迭代将有空白捕获组,这是重要信息,并且按预期打印)
is there a way to have arrays populate with those capture groups and use them further in my bash script?有没有办法让 arrays 填充这些捕获组并在我的 bash 脚本中进一步使用它们?
say for instance $1, $2, $3, $4 were fields device_id, uptime, status, last_error.例如 $1、$2、$3、$4 是字段 device_id、uptime、status、last_error。 and in my curl command I have info of 4 devices.
在我的 curl 命令中,我有 4 个设备的信息。 I would like to have device_id[0] up to [device_id[3], and the same for each capture group.
我想让 device_id[0] 达到 [device_id[3],并且每个捕获组都相同。
I'm not experienced with perl, and only used it as suggested by a few forum posts, so any advice on how to achieve this would be greatly appreciated.我没有使用 perl 的经验,并且只按照一些论坛帖子的建议使用它,所以任何关于如何实现这一点的建议将不胜感激。
Your example code is obviously broken;您的示例代码显然已损坏; you can't nest single quotes;
你不能嵌套单引号; though Perl provides the convenience operator
q()
so you can easily work around this.虽然 Perl 提供了方便的运算符
q()
所以你可以很容易地解决这个问题。 The formatting has other errors, too.格式也有其他错误。 Probably you mean something like
可能你的意思是
perl -nle 'print(join("\t", "$1", "$2", "$3", "$4"), "\n") while m{regex_to_capture_data}g' temp.txt
You can pipe this to你可以 pipe 这个到
while IFS=$'\t' read -r boulders rubble stones rocks; do
...
though probably you could just run the body of whatever that loop was doing from Perl directly, too.尽管您可能也可以直接从 Perl 运行该循环的主体。
perl -nle 'system("geologee", "--boulders", $1, "--rubbleUrl", "$2:$3/$4") while m{regex_to_capture_data}g' temp.txt
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.