如何为某些字符串过滤 NAME 下的所有内容?
How to filter everything under NAME for certain strings?
这是我当前使用的示例 test.txt 文件:
NAME NAMESPACE REVISION UPDATED STATUS CHART APP VERSION
app-education-content-creator-1651375038 ci 1 2022-05-01 03:28:36.824230864 +0000 UTC deployed prodigy-application-0.3.11 1.0.0
app-education-content-creator-preview-2255856586 ci 1 2022-05-02 03:51:24.017628998 +0000 UTC deployed prodigy-application-0.3.11 1.0.0
app-education-content-creator-preview-2255904996 ci 1 2022-05-02 03:58:50.959635169 +0000 UTC deployed prodigy-application-0.3.11 1.0.0
ci-external-secrets ci 2 2021-09-13 19:58:32.975711779 +0000 UTC deployed kubernetes-external-secrets-3.1.0 3.1.0
ci-flux ci 7 2022-01-04 18:37:49.503585809 +0000 UTC deployed flux-1.11.2 1.24.1
ci-helm-operator ci 5 2021-08-24 04:12:49.836776337 +0000 UTC deployed helm-operator-1.2.0 1.2.0
cortex-preview-850-game-cortex-server ci 1 2022-05-04 12:51:35.725983628 +0000 UTC deployed prodigy-application-0.3.11 1.0.0
cortex-preview-853-game-cortex-server ci 1 2022-05-18 19:22:11.062215547 +0000 UTC deployed prodigy-application-0.4.14 1.0.0
istio-gateways ci 4 2022-04-27 20:01:21.569001421 +0000 UTC deployed istio-gateways-0.1.0
launcher-preview-90-game-launcher ci 1 2022-05-18 18:34:27.09314326 +0000 UTC deployed prodigy-application-0.3.11 1.0.0
loader-preview-415-game-launcher ci 1 2022-05-17 15:42:23.055718169 +0000 UTC deployed prodigy-application-0.3.11 1.0.0
loader-preview-415-game-loader ci 1 2022-05-17 15:42:16.490564899 +0000 UTC deployed prodigy-application-0.3.11 1.0.0
loader-preview-415-passthrough ci 1 2022-05-17 15:42:20.406590056 +0000 UTC deployed istio-passthrough-0.1.4 1.8.3
loader-preview-454-game-launcher ci 1 2022-04-29 13:56:41.144878095 +0000 UTC deployed prodigy-application-0.3.11 1.0.0
loader-preview-454-game-loader ci 1 2022-04-29 13:56:35.559380733 +0000 UTC deployed prodigy-application-0.3.11 1.0.0
loader-preview-454-passthrough ci 1 2022-04-29 13:56:38.359154911 +0000 UTC deployed istio-passthrough-0.1.4 1.8.3
loader-preview-458-game-launcher ci 1 2022-05-17 22:14:26.959236354 +0000 UTC deployed prodigy-application-0.3.11 1.0.0
loader-preview-458-game-loader ci 1 2022-05-17 22:14:20.956443867 +0000 UTC deployed prodigy-application-0.4.14 1.0.0
loader-preview-458-passthrough ci 1 2022-05-17 22:14:24.043742858 +0000 UTC deployed istio-passthrough-0.1.4 1.8.3
math-preview-5305-feature-flag-api ci 1 2022-04-25 21:36:18.331495893 +0000 UTC deployed prodigy-application-0.3.11 1.0.0
math-preview-5305-feature-flag-client ci 1 2022-04-25 21:36:20.665011277 +0000 UTC deployed prodigy-application-0.3.11 1.0.0
math-preview-5305-flags-redis ci 1 2022-04-25 21:35:45.851677929 +0000 UTC deployed redis-15.4.2 6.2.5
math-preview-5305-game-client ci 1 2022-04-25 21:36:28.290028875 +0000 UTC deployed prodigy-application-0.3.11 1.0.0
math-preview-5305-game-cortex-server ci 1 2022-04-
目前这行代码输出所有早于 X 天的 helm 版本名称(在下面的测试用例中 X 是 3):
getOldPreviews() {
secondsUntilStale=
helmReleaseList=
echo " "
echo "$helmReleaseList" |
awk 'f;/NAME/{f=1}' |
awk -v today="$today" -v expiryTime="$secondsUntilStale" '{
getPreviewDateEpoch = "gdate -d \""" ""\" +%s"
getPreviewDateEpoch | getline previewCreationDate
if(today - previewCreationDate > expiryTime){
print
}
}'
}
X为3时的测试用例:
Namespace is: ci
Days until stale: 3
Helm list file path: test.txt
Filtered previews older than 3 days
app-education-content-creator-1651375038
app-education-content-creator-preview-2255856586
app-education-content-creator-preview-2255904996
ci-external-secrets
ci-flux
ci-helm-operator
cortex-preview-850-game-cortex-server
istio-gateways
loader-preview-454-game-launcher
loader-preview-454-game-loader
loader-preview-454-passthrough
math-preview-5305-feature-flag-api
math-preview-5305-feature-flag-client
math-preview-5305-flags-redis
math-preview-5305-game-client
math-preview-5305-game-cortex-server
如何才能过滤 X 天数和某些版本,例如带有“preview-”、“-preview-”的任何内容?
您可以像这样过滤行
awk -v pattern="preview" -v today="$today" -v expiryTime="$secondsUntilStale" '
~ pattern {
... this is exactly the same as your code ...
}
'
string ~ pattern
是 awk 的显式 regex-matching 运算符。
这也会提高性能,因为您需要调用 date
次。
这部分是做一些假设:
如果你有 GNU awk 或 mawk,有内置的时间函数,所以你不需要调用 date
gawk -v pattern="preview" -v X=3 '
# convert "YYYY-mm-dd", "HH:MM:SS.sssss" to an epoch value
function timestamp (date, time) {
gsub(/-/, " ", date)
sub(/\.[0-9+]$/, "", time)
gsub(/:/, " ", time)
return mktime(date " " time)
}
BEGIN {
# "X" days ago
expiryTime = systime() - X * 86400
}
# skipping the header line, for lines not matching the pattern
# and where the timestamp is older than X days ago, print the Name
NR > 1 && !~ pattern && timestamp(, ) < expiryTime {print }
' releaseListFile
截至当前时间,输出:
app-education-content-creator-1651375038
ci-external-secrets
ci-flux
ci-helm-operator
istio-gateways
这是我当前使用的示例 test.txt 文件:
NAME NAMESPACE REVISION UPDATED STATUS CHART APP VERSION
app-education-content-creator-1651375038 ci 1 2022-05-01 03:28:36.824230864 +0000 UTC deployed prodigy-application-0.3.11 1.0.0
app-education-content-creator-preview-2255856586 ci 1 2022-05-02 03:51:24.017628998 +0000 UTC deployed prodigy-application-0.3.11 1.0.0
app-education-content-creator-preview-2255904996 ci 1 2022-05-02 03:58:50.959635169 +0000 UTC deployed prodigy-application-0.3.11 1.0.0
ci-external-secrets ci 2 2021-09-13 19:58:32.975711779 +0000 UTC deployed kubernetes-external-secrets-3.1.0 3.1.0
ci-flux ci 7 2022-01-04 18:37:49.503585809 +0000 UTC deployed flux-1.11.2 1.24.1
ci-helm-operator ci 5 2021-08-24 04:12:49.836776337 +0000 UTC deployed helm-operator-1.2.0 1.2.0
cortex-preview-850-game-cortex-server ci 1 2022-05-04 12:51:35.725983628 +0000 UTC deployed prodigy-application-0.3.11 1.0.0
cortex-preview-853-game-cortex-server ci 1 2022-05-18 19:22:11.062215547 +0000 UTC deployed prodigy-application-0.4.14 1.0.0
istio-gateways ci 4 2022-04-27 20:01:21.569001421 +0000 UTC deployed istio-gateways-0.1.0
launcher-preview-90-game-launcher ci 1 2022-05-18 18:34:27.09314326 +0000 UTC deployed prodigy-application-0.3.11 1.0.0
loader-preview-415-game-launcher ci 1 2022-05-17 15:42:23.055718169 +0000 UTC deployed prodigy-application-0.3.11 1.0.0
loader-preview-415-game-loader ci 1 2022-05-17 15:42:16.490564899 +0000 UTC deployed prodigy-application-0.3.11 1.0.0
loader-preview-415-passthrough ci 1 2022-05-17 15:42:20.406590056 +0000 UTC deployed istio-passthrough-0.1.4 1.8.3
loader-preview-454-game-launcher ci 1 2022-04-29 13:56:41.144878095 +0000 UTC deployed prodigy-application-0.3.11 1.0.0
loader-preview-454-game-loader ci 1 2022-04-29 13:56:35.559380733 +0000 UTC deployed prodigy-application-0.3.11 1.0.0
loader-preview-454-passthrough ci 1 2022-04-29 13:56:38.359154911 +0000 UTC deployed istio-passthrough-0.1.4 1.8.3
loader-preview-458-game-launcher ci 1 2022-05-17 22:14:26.959236354 +0000 UTC deployed prodigy-application-0.3.11 1.0.0
loader-preview-458-game-loader ci 1 2022-05-17 22:14:20.956443867 +0000 UTC deployed prodigy-application-0.4.14 1.0.0
loader-preview-458-passthrough ci 1 2022-05-17 22:14:24.043742858 +0000 UTC deployed istio-passthrough-0.1.4 1.8.3
math-preview-5305-feature-flag-api ci 1 2022-04-25 21:36:18.331495893 +0000 UTC deployed prodigy-application-0.3.11 1.0.0
math-preview-5305-feature-flag-client ci 1 2022-04-25 21:36:20.665011277 +0000 UTC deployed prodigy-application-0.3.11 1.0.0
math-preview-5305-flags-redis ci 1 2022-04-25 21:35:45.851677929 +0000 UTC deployed redis-15.4.2 6.2.5
math-preview-5305-game-client ci 1 2022-04-25 21:36:28.290028875 +0000 UTC deployed prodigy-application-0.3.11 1.0.0
math-preview-5305-game-cortex-server ci 1 2022-04-
目前这行代码输出所有早于 X 天的 helm 版本名称(在下面的测试用例中 X 是 3):
getOldPreviews() {
secondsUntilStale=
helmReleaseList=
echo " "
echo "$helmReleaseList" |
awk 'f;/NAME/{f=1}' |
awk -v today="$today" -v expiryTime="$secondsUntilStale" '{
getPreviewDateEpoch = "gdate -d \""" ""\" +%s"
getPreviewDateEpoch | getline previewCreationDate
if(today - previewCreationDate > expiryTime){
print
}
}'
}
X为3时的测试用例:
Namespace is: ci
Days until stale: 3
Helm list file path: test.txt
Filtered previews older than 3 days
app-education-content-creator-1651375038
app-education-content-creator-preview-2255856586
app-education-content-creator-preview-2255904996
ci-external-secrets
ci-flux
ci-helm-operator
cortex-preview-850-game-cortex-server
istio-gateways
loader-preview-454-game-launcher
loader-preview-454-game-loader
loader-preview-454-passthrough
math-preview-5305-feature-flag-api
math-preview-5305-feature-flag-client
math-preview-5305-flags-redis
math-preview-5305-game-client
math-preview-5305-game-cortex-server
如何才能过滤 X 天数和某些版本,例如带有“preview-”、“-preview-”的任何内容?
您可以像这样过滤行
awk -v pattern="preview" -v today="$today" -v expiryTime="$secondsUntilStale" '
~ pattern {
... this is exactly the same as your code ...
}
'
string ~ pattern
是 awk 的显式 regex-matching 运算符。
这也会提高性能,因为您需要调用 date
次。
这部分是做一些假设:
如果你有 GNU awk 或 mawk,有内置的时间函数,所以你不需要调用 date
gawk -v pattern="preview" -v X=3 '
# convert "YYYY-mm-dd", "HH:MM:SS.sssss" to an epoch value
function timestamp (date, time) {
gsub(/-/, " ", date)
sub(/\.[0-9+]$/, "", time)
gsub(/:/, " ", time)
return mktime(date " " time)
}
BEGIN {
# "X" days ago
expiryTime = systime() - X * 86400
}
# skipping the header line, for lines not matching the pattern
# and where the timestamp is older than X days ago, print the Name
NR > 1 && !~ pattern && timestamp(, ) < expiryTime {print }
' releaseListFile
截至当前时间,输出:
app-education-content-creator-1651375038
ci-external-secrets
ci-flux
ci-helm-operator
istio-gateways