-1

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]+

user837208
  • 2,487
  • 7
  • 37
  • 54

2 Answers2

0

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
holygeek
  • 15,653
  • 1
  • 40
  • 50
0

You need to use a possessive-quantifier:

Try

[\d]+?
wadesworld
  • 13,535
  • 14
  • 60
  • 93