extract UID from /etc/passwd using RegEx [duplicate]
-
08-04-2021 - |
Question
I would like to extract UID from /etc/passwd file which looks like this-
www-data:x:33:33:www-data:/var/www:/bin/sh
The problem I'm facing is that even if I match first 33, the second 33 (GID) is also getting matched. How I match first occurrence only?
So far I've this- [\d]+
Solution
You need to use a possessive-quantifier:
Try
[\d]+?
OTHER TIPS
That x
in the second field used to contain the crypted password. Nowadays you can pretty much be sure that it is x
for all entries so you can use the following:
$ grep x:33 /etc/passwd
See shadow password for details on that x
.
You didn't tell us what you were trying to do actually so I'll take a guess that you might want to look at the man id
man page.
If it's only uid that you're interested in then you can use cut
:
$ cut -f 3 -d: /etc/passwd
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow