University information

Build and Audit a Unix Shell Counting Pipeline

In this guide +

A pipeline can produce a tidy answer while counting the wrong thing. This workshop starts with six fictional labels and asks you to predict every intermediate result. The goal is to explain the total, not simply to recognise a familiar command. Work on paper or in a disposable directory with a Unix-like shell and the usual text-processing utilities.

Define what one record means

Software Carpentry’s pipes-and-filters lesson describes combining command output into another command’s input. Sorting, counting and redirection can be composed, but the order determines the result.

Here, one nonempty line is one observation. Capital A and lower-case a would be different labels, but the supplied file uses only capital letters. There is no header and no blank line. Write these assumptions down before calculating; they are part of the exercise rather than properties guaranteed for every text file.

A
B
A
C
B
A

Save the six lines as labels.txt if working locally. Preserve that input. Writing an output over the input would remove the evidence needed to explain an unexpected result.

Predict the intermediate streams

Original six-record counting fixture
Stage Expected content Check
Input A,B,A,C,B,A Six records
Alphabetical order A,A,A,B,B,C Same six records
Adjacent groups counted 3 A;2 B;1 C Three label groups
Descending frequency 3 A;2 B;1 C Counts sum to six

The sorted stream changes order but should not change membership. The counting stage changes the representation from individual observations to a label and its frequency. Six input lines therefore become three summary lines. A shorter output is expected; it is not evidence that observations were discarded.

Build the pipeline and explain it

sort labels.txt | uniq -c | sort -nr
3 A
2 B
1 C

Spacing around the counts may differ by utility; the logical result is A three times, B twice and C once. The first sort brings identical labels together. The next stage counts each adjacent group. The final numeric reverse sort orders the counts from largest to smallest. Reconcile three plus two plus one with the original six lines.

For this tiny fixture, the first and final ordered displays happen to look straightforward. That should not hide the different jobs of the two sorts. One groups labels; the other ranks numerical frequencies. Removing either stage changes the reasoning even when a particular input accidentally produces a plausible answer.

Diagnose a tempting shortcut

Imagine applying the adjacent-counting stage directly to the unsorted input. None of the repeated labels are next to one another, so it would report separate one-line groups. The output could still sum to six while failing the stated requirement of one frequency per label. A total check is necessary but insufficient: also check that each distinct label appears once.

Now add a fourth A as a clearly marked extension. Predict A=4, B=2, C=1 and a total of seven before rerunning. Restore the original file afterward if you want to compare results. Do not alter the input silently and then describe both outputs as answers to the same fixture.

Prepare a reviewable handover

Provide the input, the command, the three logical frequencies and the reconciliation total. Include the assumptions about case, blank lines and headers. The CSV validation workshop shows why defining records matters when a file has more structure than one label per line.

The reproducible-analysis workshop provides a useful companion exercise in preserving declared inputs. More topics are available in the self-study workshop collection. No online dataset or account is needed for this example.

Questions about pipeline checks

Why are there only three output rows?

They summarise three distinct labels. Their frequencies still account for all six observations.

Does a correct total prove the grouped answer is right?

No. Separate adjacent groups can produce the correct total while repeating a label. Check uniqueness of summary labels as well.

Should I add a header to the supplied file?

Not without changing the declared contract and command. In this exercise every line is data, so a header would become another counted label.

Official reference

Software Carpentry: Pipes and Filters, checked 17 October 2026. The labels and reconciliation table are original.

Related guides

View the worked-example diagram
Six input labels are sorted into adjacent groups, counted as3,2,1 and reconciled to six observations.
Original explanatory diagram; consult the linked official guidance for current requirements.

Privacy choices

Allow Google Ads measurement cookies and use of ad-click information to attribute visits to our public information pages? This optional measurement stays off until you accept. Separate analytics, personalised advertising and enhanced conversions remain off; we do not configure form-field collection. This new attribution permission is not covered by your earlier choice. You can refuse or change it at any time.

Read our privacy policy