Activity - shell pipelines
Assignment
Let’s use the /etc/passwd file as an example to practice the use of shell pipelines. The passwd file stores basic information of each user account on a Linux/Unix machine.
I have an example passwd file stored at the shared workspace for our activity today. Once you log into the plank server, take a further look at the file to see what information is stored and familiarize yourself with the file format:
$ less /thayerfs/courses/22spring/cosc050/workspace/passwd
Press q
to quit the viewing of the less
command.
Write pipelines to:
- output the list of user full names, sorted alphabetically (remove duplicated occurrences of identical names)
- count the number of users with ‘James’ as the first name.
- count the number of different shells used by all users.
- output the number of users using each type of shell in the descending order.
Solutions
We can define a variable to represent the file:
$ passfile=/thayerfs/courses/22spring/cosc050/workspace/passwd
1. cut -d : -f 5 "$passfile" | sort -u
2. cut -d : -f 5 "$passfile" | sort -u | cut -d ' ' -f 1 | grep -c James
3. cut -d : -f 7 "$passfile" | sort -u | sed '/^$/d' | wc -l
4. cut -d : -f 7 "$passfile" | sed "/^$/d" | sort | uniq -c | sort -nr