如何使用 awk 或 grep 过滤前 n 个字符中至少有 1 个数字的字符串?

How to awk or grep to filter for strings which have at least 1 number in the first n characters?

如何过滤前8个字符至少有1个数字,第9个字符有下划线的字符串?

我尝试过滤字母数字并不能保证前 8 个字符中至少存在一个数字: grep "^[a-z0-9]\{8\}_"

输入示例:

zjuscer3_prod_backend_1 5fa9a2774f13
prod_frontend_1 a55eb34aed85
rhg8ik8s_stag_frontend.1 74d419c1c15e
stag_backend_1 52ade8af8cca
syhvctf4_stag_notebook_1 b846d511c937

目标输出:

zjuscer3_prod_backend_1 5fa9a2774f13
rhg8ik8s_stag_frontend.1 74d419c1c15e
syhvctf4_stag_notebook_1 b846d511c937

怎么样

awk 'substr(,9,1) == "_" && substr(,1,8) ~ /[[:digit:]]/'

排除前8个字符没有数字的行,只保留第9位有下划线的行。使用 awk:

$ awk '!/^[^0-9]{8}/&&/^.{8}_/' foo.txt 
zjuscer3_prod_backend_1 5fa9a2774f13
rhg8ik8s_stag_frontend.1 74d419c1c15e
syhvctf4_stag_notebook_1 b846d511c937

使用 grep:

$ grep -Ev '^[^0-9]{8}' foo.txt | grep -E '^.{8}_'
zjuscer3_prod_backend_1 5fa9a2774f13
rhg8ik8s_stag_frontend.1 74d419c1c15e
syhvctf4_stag_notebook_1 b846d511c937

使用 sed:

$ sed -nE '/^[^0-9]{8}/!{/^.{8}_/p}' foo.txt 
zjuscer3_prod_backend_1 5fa9a2774f13
rhg8ik8s_stag_frontend.1 74d419c1c15e
syhvctf4_stag_notebook_1 b846d511c937

awk

已更新

awk '/^[[:alnum:]]{8}_/ && substr([=10=],1,8)  ~ /[[:digit:]]/' file
zjuscer3_prod_backend_1 5fa9a2774f13
rhg8ik8s_stag_frontend.1 74d419c1c15e
syhvctf4_stag_notebook_1 b846d511c937

另一个使用 awk 的变体可能是:

  • 使用 ^.{8}_
  • 在第 9 个字符匹配下划线
  • 在使用 negated character class ^[^0-9_]*[0-9]
  • 匹配 _ 之前先匹配数字

例如

awk '/^.{8}_/&&/^[^0-9_]*[0-9]/' file

输出

zjuscer3_prod_backend_1 5fa9a2774f13
rhg8ik8s_stag_frontend.1 74d419c1c15e
syhvctf4_stag_notebook_1 b846d511c937