May 2007
Beginner
628 pages
15h 46m
English
You need to extract one or more fields from each line of output.
Use cut if there are delimiters you can easily pick out, even if they are different for the beginning and end of the field you need:
# Here's an easy one, what users, home directories and shells do # we have on this NetBSD system $ cut -d':' -f1,6,7 /etc/passwd root:/root:/bin/csh toor:/root:/bin/sh daemon:/:/sbin/nologin operator:/usr/guest/operator:/sbin/nologin bin:/:/sbin/nologin games:/usr/games:/sbin/nologin postfix:/var/spool/postfix:/sbin/nologin named:/var/chroot/named:/sbin/nologin ntpd:/var/chroot/ntpd:/sbin/nologin sshd:/var/chroot/sshd:/sbin/nologin smmsp:/nonexistent:/sbin/nologin uucp:/var/spool/uucppublic:/usr/libexec/uucp/uucico nobody:/nonexistent:/sbin/nologin jp:/home/jp:/usr/pkg/bin/bash # What is the most popular shell on the system? $ cut -d':' -f7 /etc/passwd | sort | uniq -c | sort -rn 10 /sbin/nologin 2 /usr/pkg/bin/bash 1 /bin/csh 1 /bin/sh 1 /usr/libexec/uucp/uucico # Now let's see the first two directory levels $ cut -d':' -f6 /etc/passwd | cut -d'/' -f1-3 | sort -u / /home/jp /nonexistent /root /usr/games /usr/guest /var/chroot /var/spool
Use awk to split on multiples of whitespace, or if you need to rearrange the
order of the output fields. Note the → denotes a tab character in the
output. The default is space but you can change that using $OFS:
# Users, home directories and shells, but swap the last two # and use a tab delimiter $ awk 'BEGIN ...
Read now
Unlock full access