过滤元素
我有下一个命令,该命令返回以下输出
kubectl get pods
输出:
NAME READY STATUS RESTARTS AGE
svc-admin-async-6d4cd4989f-8lzcq 1/1 Running 0 4d7h
svc-admin-c795b488d-kpcsn 1/1 Running 0 3d2h
svc-admin-c795b488d-m9mjg 1/1 Running 0 7d6h
svc-bpm-inbox-77fffc4d89-cx7g7 1/1 Running 0 7d6h
svc-bpm-tasks-654695689d-spvt7 1/1 Running 0 3d17h
svc-bpm-tasks-654695689d-wrclq 1/1 Running 0 3d17h
svc-claim-78b7d8db99-9m6zs 1/1 Running 0 120m
svc-claim-78b7d8db99-qvq9m 1/1 Running 0 120m
我只需要列出以给定参数开头的POD。例如,如果参数是svc-bpm-tasks,则只能显示:
svc-bpm-tasks-654695689d-spvt7
svc-bpm-tasks-654695689d-wrclq
我有以下脚本,但我不知道如何过滤:
set param_to_filter=%~1
FOR /F "skip=1" %%I in ('kubectl get pods') DO (
:: if %%I starts_with param_to_filter
echo %%I
)
感谢您的帮助
I have the next command that returns the following output
kubectl get pods
Output:
NAME READY STATUS RESTARTS AGE
svc-admin-async-6d4cd4989f-8lzcq 1/1 Running 0 4d7h
svc-admin-c795b488d-kpcsn 1/1 Running 0 3d2h
svc-admin-c795b488d-m9mjg 1/1 Running 0 7d6h
svc-bpm-inbox-77fffc4d89-cx7g7 1/1 Running 0 7d6h
svc-bpm-tasks-654695689d-spvt7 1/1 Running 0 3d17h
svc-bpm-tasks-654695689d-wrclq 1/1 Running 0 3d17h
svc-claim-78b7d8db99-9m6zs 1/1 Running 0 120m
svc-claim-78b7d8db99-qvq9m 1/1 Running 0 120m
I need to list only pods that starts with a given parameter. For example, if the param is svc-bpm-tasks, only should to show:
svc-bpm-tasks-654695689d-spvt7
svc-bpm-tasks-654695689d-wrclq
I have the following script, but I dont know how to filter:
set param_to_filter=%~1
FOR /F "skip=1" %%I in ('kubectl get pods') DO (
:: if %%I starts_with param_to_filter
echo %%I
)
Thanks for your help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
应该做你想做的。
Caret逃脱了管道,告诉
cmd
该管道是要执行的命令的一部分。findstr
找到/b
以字符串%〜1
开始的字符串,这是批处理的第一个参数。不再需要
跳过
,因为标题线不会从任何目标字符串开始。/i
是可选的,使匹配项不敏感。呢请勿在
代码块中使用
::
注释(括号的序列序列)实际上是一个损坏的标签,它混淆了cmd
。在代码块中使用REM
。修订 - 由于任何字符串均可用于
%1
,使用/l
指定l
iTeral比较以覆盖默认值/r
(REGEX匹配)/c:“ String”
匹配还补偿了字符串>%〜1
中空间的可能性。如果一个空间出现在%〜1
中(通过提供引用的参数“一个两个”
),则没有/c:
,Findstr
将搜索一个
或两个
。使用/c:
,搜索是一个 space 两个。should do what you want.
The caret escapes the pipe, telling
cmd
that the pipe is part of the command to be executed.findstr
finds any string that/b
begins with the string%~1
, which is the first parameter to the batch.The
skip
is no longer needed as the header line would not start with any of the target strings.The
/i
is optional, making the match case-insensitive.! Do not use
::
comments in acode block
(parenthesised sequence of lines) as it's actually a broken label, which confusescmd
. Userem
in code blocks.Amended - Since any string may be used for
%1
, using/L
to specifyL
iteral comparison to override the default/r
(regex match)The
/c:"string"
match also compensates for the possibility of a space in the string%~1
. If a space appears in%~1
(by supplying a quoted parameter"one two"
) then without the/c:
,findstr
would search forone
ORtwo
. With/c:
, the search is for oneSpacetwo.