Lessons · Terminal · the pipe |
The pipe
a | b sends what a prints into b as its input. Small tools joined by pipes do what a program would take an hour to write.
Hone is a place to practise programming. This is one of its lessons, written out in full and free to read without an account.
What it is for
Count the errors in a log, find the biggest files, list the words in a file by frequency: each is a pipe of two or three commands.
How to think about it
Build a pipe left to right, running it after each step to see what the next command will receive.
Worked example
printf 'INFO ok\nERROR one\nERROR two\n' > log.txtA log with two errors.
cat log.txt | grep ERRORThe two ERROR lines.
grep ERROR log.txt | wc -l2: the count, by adding one more tool to the chain.
Your turn
Count the lines containing WARN in app.log.
grep WARN app.log wc -l
Try it at a real prompt
The trap
Piping into a command that takes file names, like rm or cp. They do not read input; xargs is the bridge.
Practise the pipe | on HoneA question on it now, a coding challenge where there is one, and it is remembered for review. Free, no email needed.