[英]Replace substring from a field which is also a substring
I have this stdout from a command:我从一个命令得到这个标准输出:
2022-08-05T06:30:00.503053001Z projects/12345/locations/europe-west4/workflows/workflow_name/executions/a1f339e1-XXXX
I only want to get the timestamp and the workflow name.我只想获取时间戳和工作流名称。 In other words I want to return this:换句话说,我想返回这个:
2022-08-05T06:30:00.503053001Z workflow_name
but I can't figure out how to do it.但我不知道该怎么做。 I've tried various flavours of cut
, but to no avail:我尝试了各种口味的cut
,但无济于事:
➜ echo 2022-08-05T06:30:00.503053001Z projects/12345/locations/europe-west4/workflows/workflow_name/executions/a1f339e1-XXXX | cut -d'/' -f 6
workflow_name
➜ echo 2022-08-05T06:30:00.503053001Z projects/12345/locations/europe-west4/workflows/workflow_name/executions/a1f339e1-XXXX | cut -d'/' -f 1,6
2022-08-05T06:30:00.503053001Z projects/workflow_name
➜ echo 2022-08-05T06:30:00.503053001Z projects/12345/locations/europe-west4/workflows/workflow_name/executions/a1f339e1-XXXX | cut -d'/' -f 0,6
cut: [-cf] list: values may not include zero
OK, as I've been writing this post I've figured out a way to do it using sed
好的,当我一直在写这篇文章时,我已经找到了一种使用sed
的方法
➜ echo 2022-08-05T06:30:00.503053001Z projects/12345/locations/europe-west4/workflows/workflow_name/executions/a1f339e1-XXXX | cut -d'/' -f 1,6 | sed 's/projects\///g'
2022-08-05T06:30:00.503053001Z workflow_name
But that feels a bit clumsy.但这感觉有点笨拙。 I was hoping there was a better way using cut
.我希望有更好的方法使用cut
。 I would also quite like to remove the millisecond precision from the timestamp.我也很想从时间戳中删除毫秒精度。
Open to suggestions as to how this could be improved接受有关如何改进的建议
If you can use perl
:如果您可以使用perl
:
perl -ne 'print "$1 $2\n" if (m{(.*?)\.\d+Z.*?/workflows/([^/]+)})' <<< "2022-08-05T06:30:00.503053001Z projects/12345/locations/europe-west4/workflows/workflow_name/executions/a1f339e1-XXXX"
perhaps with sed也许与 sed
echo "2022-08-05T06:30:00.503053001Z projects/12345/locations/europe-west4/workflows/workflow_name/executions/a1f339e1-XXXX" | sed -E "s/([0-9T:\.\-]*Z).*workflows\/([a-zA-Z_]*)\/.*/\1 \2/g"
on mac prompt my output在 mac 上提示我的 output
2022-08-05T06:30:00.503053001Z workflow_name
awk is an option awk 是一个选项
$ echo "2022-08-05T06:30:00.503053001Z projects/12345/locations/europe-west4/workflows/workflow_name/executions/a1f339e1-XXXX" | awk -F '( |/)' '{print $1,$7}'
2022-08-05T06:30:00.503053001Z workflow_name
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.