Combining commands using pipes

A pipe is a way to connect the output of one program to the input of another program without any temporary file; a pipeline is a connection of two or more programs through pipes. The symbol for a pipe is |. For example:

who | wc -l Count users
who | grep mary | wc -l Count how many times Mary is logged in
cat file1 file2 file3 | spell | less Concatenate three files, run the spelling checker on
  the output and then display the results one page at
  a time with the less program.

Let's play with pipes and processes. The command last prints out a list of users that have logged in to the system since the last date the log file has been kept. For example, the following shows the partial output of last on a system.

gcook    pts/86       132.178.175.169  Mon Apr  1 10:25 - 12:41  (02:16)    
znickel  pts/85       et238-1164.boise Mon Apr  1 10:23 - 11:21  (00:58)    
mlukes   pts/82       obsidian.boisest Mon Apr  1 10:22 - 15:01  (04:39)    
lroutled pts/81       mg122-9.boisesta Mon Apr  1 10:20 - 11:47  (01:27)    
aolson   pts/76       mg122-6.boisesta Mon Apr  1 10:19 - 11:45  (01:26)    
dornelas pts/62       node19.boisestat Mon Apr  1 10:17 - 14:14  (03:56)    
dornelas pts/61       node19.boisestat Mon Apr  1 10:03 - 10:39  (00:35)    
dornelas pts/60       node19.boisestat Mon Apr  1 10:01 - 14:14  (04:12)    
rgeetha  pts/59       65-129-50-248.bo Mon Apr  1 09:37 - 11:38  (02:00)    
rgeetha  pts/49       65-129-50-248.bo Mon Apr  1 09:37 - 15:38  (06:01)    
mmartin  pts/45       75-92-191-73.war Mon Apr  1 09:32 - 11:46  (02:13)    
mvail    pts/43       69-92-71-108.cpe Mon Apr  1 09:19 - 11:21  (02:02)    
mvail    pts/39       69-92-71-108.cpe Mon Apr  1 09:19 - 11:22  (02:03)    
whieb    pts/25       masquerade.micro Mon Apr  1 09:15 - 11:20  (02:04)    
tford    pts/43       216.190.60.34    Mon Apr  1 07:58 - 07:58  (00:00)    
aolson   pts/39       cls-busn-206a.bo Mon Apr  1 07:36 - 08:44  (01:08)    
mvail    pts/25       69-92-71-108.cpe Mon Apr  1 07:35 - 08:38  (01:03)    
aibrahim pts/39       65-129-56-197.bo Mon Apr  1 05:00 - 05:00  (00:00)    
aibrahim pts/25       65-129-56-197.bo Mon Apr  1 04:59 - 05:14  (00:15)    
...
wtmp begins Mon Apr  1 04:59:35 2013

Suppose we want to make a list of all the users who have been on the system and arrange the list by how often they have logged in to the system. So the only useful information for us is the first column of the output. We will use the filter awk to extract the first column as follows:

last | awk '{print $1}'

Next we want to sort this list of names so that duplicates are brought together.

last | awk '{print $1}' | sort

Next we will use the command uniq -c that eliminates duplicates from a list of words, replacing each set of duplicates by one instance of the word prefixed by a count of how many instances of that word were found in the list.

last | awk '{print $1}' | sort | uniq -c

Now we have a list of users prefixed with how many times each user has logged in to the system. Next we sort this list by the numeric count in reverse order (so that larger counts show up first).

last | awk '{print $1}' | sort | uniq -c | sort -rn

Try this command on your system and see what results you get! If you want to learn more about pipes and filters, see the book The UNIX Programming Environment [1].