Home > Net >  Regex match last three integers and one character before that integers
Regex match last three integers and one character before that integers

Time:10-16

I have been trying this for a lot of time,but my search is failing..I have below test data

mhie0104:x:104:600:Martinescu Horia:/home/scs/gr911/mhie0104:/bin/bash
mlie0105:x:105:600:Martinescu Laurentiu:/home/scs/gr911/mlie0105:/bin/bash
mmie0106:x:106:600:Martinescu Marius:/home/scs/gr911/mmie0106:/bin/bash
mnie0107:x:107:600:Martinescu Nicolae:/home/scs/gr911/mnie0107:/bin/bash
mpiel110:x:110:600:Malinescu Paul:/home/scs/gr911/mpie110:/bin/bash

I am trying to find out users,who has exact three digits at the end ..So below is what i did

awk -F: '$1 ~ /*[a-z]//d{3}/'

My understanding of using above regex is :
"*" at the begining should match any characters
[a-z] it should match any character string just before digits
Finally three digits

I also tried with below variation

awk -F: '$1 ~ /*?//d{3}/'

So what i need from above test data is

 mpiel110:x:110:600:Malinescu Paul:/home/scs/gr911/mpie110:/bin/bash

CodePudding user response:

1st solution: If you want to see only last 4 characters of 1st field where 4th last character is NOT digit then you can try following code.

awk -F':' '$1 ~ /[^0-9][0-9]{3}$/'  Input_file

Explanation:

  • Simply making field separator as : for all the line of Input_file.
  • Then checking condition with 1st field /[^0-9][0-9]{3}$/ if 4 letter from last is anything apart from digit and last 3 are digit then print that line.


2nd solution: In case you want to check if none of characters(from starting of 1st field except last 3 characters) should contain digit and last 3 characters should be digits then try following code.

awk -F':' '
substr($1,1,length($1)-3)!~/[0-9]/ && int(substr($1,length($1)-2))~/^[0-9]{3}$/
'  Input_file

Explanation:

  • First thing first making field separator as : for this awk program.
  • using substr function of awk to get sub string and using substr($1,1,length($1)-3)!~/[0-9]/ condition I am checking if everything of 1st field apart from last 3 characters is NOT having digit.
  • Then checking another condition int(substr($1,length($1)-2))~/^[0-9]{3}$/ where last 3 characters are of 3 digits.
  • If both of the conditions are TRUE then print that line.

CodePudding user response:

You can't use this kind of notation : \d

This is perl type regex.

Solution:

$ awk -F: '$1 ~ /[a-zA-Z][0-9]{3}$/' file
mpiel110:x:110:600:Malinescu Paul:/home/scs/gr911/mpie110:/bin/bash
  • Related