Using the following etcpasswd file daemonx22Daemonsbinbinbas
Using the following /etc/passwd file
daemon:x:2:2:Daemon:/sbin:/bin/bash
ftp:x:40:49:FTP account:/srv/ftp:/bin/bash
daemonuser:x:50:59:nouser/bin/false:/home/nouser:/bin/bash
gdm:x:106:111:Gnome Display Mgr daemon:/var/lib/gdm:/bin/false
haldaemon:x:101:102:User for haldaemon:/var/run/hald:/bin/false
lp:x:4:7:Printing daemon:/var/spool/lpd:/bin/bash
mail:x:8:12:Mailer daemon:/var/spool/clientmqueue:/bin/false
root:x:0:0:root:/root:/bin/bash
sshd:x:71:65:SSH daemon:/var/lib/sshd:/bin/false
olivert:x:1001:1003:Tom Oliver:/home/olivert:/bin/csh
smiths:x:1049:1000:Sue Williams:/export/home/smiths:/bin/csh
northj:x:1003:1003:Jim jones-North:/home/northj:/bin/csh
denniss:x:1005:1003:Sue Dennis:/home/denniss:/bin/bash
smitha:x:1050:1001:Amy Smith:/export/home/smitha:/bin/bash
jonesc:x:1053:1001:Cathy Jones:/export/home/jonesc:/bin/ksh
smithd:x:1055:1001:Dan Smith Jr:/export/home/smithd:/bin/csh
What single sed command can be used to output matches from /etc/passwd that have Smith or Jones in their description (5th field) to a file called smith_jones.txt?
sed -n \'/^([^:]+:){4}[^ ]+ (Smith|Jones)/w smith_jones.txt\' /etc/passwd
The above regex selects the correct lines as an egrep but doesnt seem to work with sed. Any idea why or how to fix?
Solution
Hello,
I would strongly resommend using awk in place of sed as awk is better suited for extracting information from files in the column mode.
Below solution works as per your requirement
awk -F : \'/(Smith|Jones)/ {print $5}\' /etc/passwd > smith_jones.txt
Now if you still need the solution using sed command then let me know.
