Lesson 2 · Unit 02
Filters
grep, sort, uniq, cut, tr, and a first taste of sed and awk
What you practise
grep(-i,-v,-r,-n,-c),sort(-n,-r,-k),uniq -c,cut,tr, a first taste ofsed 's/a/b/'andawk '{print $1}'
Try a question from this lesson
Pick one
The lines between the two EOF markers become the file app.log. grep prints the lines that contain a pattern. What does it print here?
cat > app.log <<EOF
error: disk full
all good
Error: no network
EOF
grep error app.logCorrect answer
error: disk full
Why
grep matches exactly what you type, capitals included, so Error with a capital E is not a match. It prints whole lines, not just the matching word.
The idea
Concept cards are short on purpose. In the app they appear between questions, after you have met the idea.
Concept
sort and uniq
sort puts lines in order. It compares them as text unless you say otherwise, and as text 10 comes before 9, because 1 comes before 9.
uniq removes repeated lines, but only repeats that are next to each other. With -c it also counts each run.
sort names.txt # as text, A to Z
sort -n sizes.txt # as numbers
sort -r names.txt # reversed
sort -k 2 t.txt # by the 2nd field
sort f | uniq # drop repeats
sort f | uniq -c # and count them11 more exercises in the app
- 2× fill the blanks
- 4× type the answer
- 3× multiple choice
- 1× true or false
- 1× match the pairs
Bitpath is an iOS app in development. Join the waitlist and you get one email when it is ready to try.