>_ IzzyLab

Hands-on task

This task is step 8 of the course “First steps with text”

How many people logged in

easy~5 min

/root/data/access.log is a log of actions on a website. Each line is an action and a user name, for example login anna — anna logged in, logout anna — she logged out, view anna — she looked at something. The same people logged in several times.

How many different people logged in at least once (the login action)? Write a single number to /root/answer.txt. Try to get it in one line — with a pipeline of several commands.

What you'll practise:
  • pipe |
  • pipeline
  • sort -u
  • wc -l
  • grep

Topic breakdown

The power of the command line is in pipelines. Each utility does one small job, and | joins them: the output of the left command becomes the input of the right one. A pipeline can have as many commands as you like.

grep bash /etc/passwd | wc -l              # how many accounts use the bash shell
cat /etc/group | sort | head -n 3          # the first three groups alphabetically

The sort -u flag (unique) comes in handy: it sorts lines and keeps each one only once — the same as sort | uniq.

A pipeline is easy to build step by step: run the first command, look at the output, add | next-command and look again.

What's checked

  1. answer.txt holds a single number and nothing else
  2. The number equals the count of different people who logged in

What it looks like