0
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
sys:x:3:1:sys:/dev:/usr/sbin/nologin
games:x:5:2:games:/usr/games:/usr/sbin/nologin
mail:x:8:5:mail:/var/mail:/usr/sbin/nologin
www-data:x:33:3:www-data:/var/www:/usr/sbin/nologin
backup:x:34:2:backup:/var/backups:/usr/sbin/nologin
nobody:x:65534:1337:nobody:/nonexistent:/usr/sbin/nologin
syslog:x:101:1000::/home/syslog:/bin/false
whoopsie:x:109:99::/nonexistent:/bin/false
user:x:1000:1000:edco8700,,,,:/home/user:/bin/bash
sshd:x:116:1337::/var/run/sshd:/usr/sbin/nologin
ntp:x:117:99::/home/ntp:/bin/false
mysql:x:118:999:MySQL Server,,,:/nonexistent:/bin/false
vboxadd:x:999:1::/var/run/vboxadd:/bin/false

this is an /etc/passwd file I need to do this command on. So far I have:

cut -d: -f1,6 testPasswd.txt | grep ???

that will display all the usernames and the folder associated, but I'm stuck on how to find only the ones that start with m,w,s and print the whole line.

I've tried grep -o '^[mws]*' and different variations of it, but none have worked.

Any suggestions?

nateph
  • 81
  • 8
  • See: [The Stack Overflow Regular Expressions FAQ](http://stackoverflow.com/a/22944075/3776858) – Cyrus Aug 30 '16 at 19:39

2 Answers2

3

Easier to do with awk:

awk 'BEGIN{FS=OFS=":"} $1 ~ /^[mws]/{print $1, $6}' testPasswd.txt

sys:/dev
mail:/var/mail
www-data:/var/www
syslog:/home/syslog
whoopsie:/nonexistent
sshd:/var/run/sshd
mysql:/nonexistent
anubhava
  • 761,203
  • 64
  • 569
  • 643
3

Try variations of

cut -d: -f1,6 testPasswd.txt | grep '^m\|^w\|^s' 

Or to put it more concisely,

cut -d: -f1,6 testPasswd.txt | grep '^[mws]'

That's neater especially if you have a lot of patterns to match.

But of course the awk solution is much better if doing it without constraints.

Chem-man17
  • 1,700
  • 1
  • 12
  • 27