> Hi all,
> How can I use grep to find an exact pattern match, for example if I were to
> grep the first field of the /etc/passwd file for "m",
> awk -F: '{print $}' /etc/passwd | grep m
> I get every userid that contains "m" in the passwd file, I'd like to
> matching any occurrence of only "m" in this case?
Each line in the password file has this format:
account:password:UID:GID:GECOS:directory:shell
See http://www.rt.com/man/passwd.5.html for details.
So, there are 7 fields separated by ":"s. Here's examples of how to use
awk to find patterns in them by testing the specific fields $1 through $7:
To find an "account" that CONTAINS "m":
awk -F: '$1 ~ /m/' /etc/passwd
To find an "account" that STARTS WITH "m":
awk -F: '$1 ~ /^m/' /etc/passwd
To find an "account" that ENDS WITH "m":
awk -F: '$1 ~ /m$/' /etc/passwd
To find an "account" that IS EXACTLY "m", either:
awk -F: '$1 ~ /^m$/' /etc/passwd
awk -F: '$1 == "m"' /etc/passwd
To find a "GID" that IS EXACTLY "100", either:
awk -F: '$4 ~ /^100$/' /etc/passwd
awk -F: '$4 == "100"' /etc/passwd
And so on...
Hope that helps,
Ed.