Show contents
Chapter 4 of 7 · 20 min
Redirections, pipes and filters
By the end of this chapter, you'll be able to
- Explain standard input, standard output and standard error
- Redirect a command's output to a file
- Chain commands with the | pipe
- Filter, sort and count lines with grep, sort, uniq, cut and wc
Three streams per command
Every command started by the shell has three text streams:
- standard input (stdin), what it reads: by default, the keyboard;
- standard output (stdout), what it displays: by default, the screen;
- standard error (stderr), for its error messages: the screen too.
Redirections change their destination: > sends standard output to a file (replacing it), >> appends to it, < reads input from a file, and 2> redirects errors. The special file /dev/null is a black hole: whatever you send there disappears.
The week's file
printf "monday,Kaolack,millet,12\nmonday,Thies,groundnut,8\ntuesday,Kaolack,groundnut,15\ntuesday,Thies,millet,4\nwednesday,Kaolack,cowpea,6\nwednesday,Thies,groundnut,9\nthursday,Kaolack,millet,7\n" > sales.csv
cat sales.csv
monday,Kaolack,millet,12
monday,Thies,groundnut,8
tuesday,Kaolack,groundnut,15
tuesday,Thies,millet,4
wednesday,Kaolack,cowpea,6
wednesday,Thies,groundnut,9
thursday,Kaolack,millet,7
Each line gives the day, the market, the product and the number of bags sold, separated by commas: this is the CSV format.
Filtering with grep
grep pattern file only displays the lines that contain the pattern. Useful options: -i ignores case, -v inverts (the lines that do not contain the pattern), -c counts instead of displaying, -n numbers the lines.
grep Kaolack sales.csv
grep -c groundnut sales.csv
monday,Kaolack,millet,12
tuesday,Kaolack,groundnut,15
wednesday,Kaolack,cowpea,6
thursday,Kaolack,millet,7
3
The pipe: connecting commands
The pipe | sends a command's standard output into the standard input of the next one. cut -d , -f 3 splits each line on commas (-d, the delimiter) and keeps the 3rd field (-f). sort sorts the lines, and uniq -c groups consecutive identical lines, counting them:
cut -d , -f 3 sales.csv | sort | uniq -c | sort -rn
3 millet
3 groundnut
1 cowpea
No intermediate file: the data flows from one command to the next.
Text description of the animation
- cut extracts the 3rd field of each line: a list of products remains.
- sort places identical lines next to each other.
- uniq -c merges each consecutive group and writes its size in front.
- sort -rn sorts by number (-n), in descending order (-r).
- No intermediate file: the data flows from one command to the next.
This ranking counts sales lines, not bags. For the number of bags, you would need to add up the 4th field: that is a job for tools like awk, or a small Python script. The terminal excels at filtering and counting; for calculations, you switch tools.
By default, sort sorts like a dictionary, character by character: 12 comes before 4. With -n, it compares numbers. With -t , -k 4, it sorts on the 4th field, separated by commas:
sort -t , -k 4 -n sales.csv | tail -n 1
tuesday,Kaolack,groundnut,15
Separating errors
Error messages do not go through the pipe: they go to standard error. You can send them elsewhere with 2>:
cat sales.csv missing.csv 2> errors.txt | wc -l
cat errors.txt
7
cat: missing.csv: No such file or directory
Your turn
Mini-lab: querying the sales fileTerminal simulator
Terminal
Click “Run”: the commands run in a fresh terminal, in your home folder /home/awa. Type “help” for the list of commands.Teaching simulator: it reproduces the essential Linux commands in your browser, with nothing to install · Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
Worked exercise
Worked exercise: the products sold in Thies
From sales.csv, display the list of different products sold at the Thies market, in alphabetical order, one per line, with no duplicates.
Hint 1
Hint 2
sort -u sorts and removes duplicates in a single command (equivalent to sort | uniq).Show the solution and common mistakes
grep Thies sales.csv | cut -d , -f 3 | sort -u
groundnut
millet
Each command has a role: grep filters lines, cut keeps a column, sort -u sorts and removes duplicates.
Common mistakes:
- Using
uniqwithoutsort:uniqonly merges neighbouring identical lines, distant duplicates would remain. - Forgetting
-d ,incut: by default it splits on tabs, and returns each line whole.
Summary
- 1Every command has a standard input, a standard output and a standard error.
- 2> replaces a file, >> appends to it, < reads a file, 2> redirects errors.
- 3The | pipe connects a command's output to the next command's input.
- 4grep filters, cut splits, sort sorts (-n for numbers), uniq -c counts identical neighbouring lines.
- 5Never redirect to the file you are reading: it would be emptied.
Your progress is saved on this device. Create a free account to keep your progress on all your devices.