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

Time:10-29

How to filter for strings which have at least 1 number in the first 8 characters and underscore in the 9th character?

My attempt of filtering for alphanumerics does not guarantee at least one numeric is present in the first 8 characters: grep "^[a-z0-9]\{8\}_"

Example of input:

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

Target output:

zjuscer3_prod_backend_1 5fa9a2774f13
rhg8ik8s_stag_frontend.1 74d419c1c15e
syhvctf4_stag_notebook_1 b846d511c937

CodePudding user response:

How about

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

CodePudding user response:

Exclude lines without a digit in first 8 characters, and keep only lines with underscore in 9th position. With awk:

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

With 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

With 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
  • Related