过滤元素

发布于 2025-02-12 00:22:23 字数 1219 浏览 1 评论 0原文

我有下一个命令,该命令返回以下输出

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烂人 2025-02-19 00:22:23
FOR /F %%I in ('kubectl get pods^|findstr /i /b /L /C:"%~1"') DO (

应该做你想做的。

Caret逃脱了管道,告诉cmd该管道是要执行的命令的一部分。

findstr找到/b以字符串%〜1开始的字符串,这是批处理的第一个参数。

不再需要跳过,因为标题线不会从任何目标字符串开始。

/i是可选的,使匹配项不敏感。

呢请勿在代码块中使用::注释(括号的序列序列)实际上是一个损坏的标签,它混淆了cmd。在代码块中使用REM

修订 - 由于任何字符串均可用于%1,使用/l指定l iTeral比较以覆盖默认值/r(REGEX匹配)

/c:“ String”匹配还补偿了字符串>%〜1中空间的可能性。如果一个空间出现在%〜1中(通过提供引用的参数“一个两个”),则没有/c:Findstr将搜索一个两个。使用/c:,搜索是一个 space 两个

FOR /F %%I in ('kubectl get pods^|findstr /i /b /L /C:"%~1"') DO (

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 a code block (parenthesised sequence of lines) as it's actually a broken label, which confuses cmd. Use rem in code blocks.

Amended - Since any string may be used for %1, using /L to specify Literal 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 for one OR two. With /c:, the search is for oneSpacetwo.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文