Reputation: 25
I have a UNIX password file and I need to find, using grep, the entries where:
the first 4 characters from first name and first 4 characters from the last name are inside the username.
This is my attempt:
grep -iE '^[^:]*([^:]{4})[^:]*([^:]{4})([^:]*:){4}(\1|\2)[^: ]* (\1|\2)'
And it does actually matches in some lines like these:
ldapsync:x:1118:65534:LDAP Synchronization,,,:/home/system/ldapsync:/bin/tcsh
johnkene:x:1943:1056:John Kennedy:/home/user/johnkene:/bin/tcsh
But it doesn't match in lines like these:
camad15:x:2674:1000:CAMAD2015 CAMAD2015:/home/user/camad15:/bin/sh
Upvotes: 1
Views: 4767
Reputation: 785541
This job suits more to awk
than grep
because of structure of each record delimited by a character.
You may use this awk
:
awk -F: '{
split($5, a, / /)
f4 = tolower(substr(a[1], 1, 4))
l4 = tolower(substr(a[2], 1, 4))
}
index($1, f4) && index($1, l4)' /etc/passwd
ldapsync:x:1118:65534:LDAP Synchronization,,,:/home/system/ldapsync:/bin/tcsh
camad15:x:2674:1000:CAMAD2015 CAMAD2015:/home/user/camad15:/bin/sh
Upvotes: 3