Reading and filtering text
cat, less, head, tail, wc, grep, cut, sort, uniq and tr applied to a file: picking the right filter, the option that changes everything, and predicting the output on real data.
- 30 cards
- 5 topics
- Free
The outline, topic by topic
Each topic is weighted the way the exam weighs it.
- Concepts
- Questions
- Sort and link
- Scenarios
- Understand
A few cards from the deck
grep -v pattern: what is it?Show the answer
A filter that keeps only the lines that do not contain the pattern.
grep -v '^#' configdrops the comments. Not to be mixed up with-i(ignore case),-n(number the lines) and-c(count the lines without printing them).What does
cut -d: -f1,7give on this/etc/passwdline?
```text
lea:x:1000:1000:Lea:/home/lea:/bin/bash`lea:/bin/bashlea:/home/lealea:xx:/bin/bash
Show the answer
lea:/bin/bashThe fields are
lea,x,1000,1000,Lea,/home/leaand/bin/bash: the first is the name, the seventh the shell.cutputs the delimiter back between the kept fields.wc -lcounts the … of a file;head -n 3prints the … andtail -n 3the ….Show the answer
wc -lcounts the lines of a file;head -n 3prints the first three andtail -n 3the last three.With no option,
wcprints lines, words and bytes. With no-n,headandtailprint ten lines.cut -d: -f1,7: what is it?Show the answer
Splits each line at the colons and keeps fields 1 and 7.
-dsets the delimiter,-fpicks the fields, numbered from 1. On/etc/passwd, those are the login name and the shell.grep error journal.logdoes not find the line “ERROR disque plein” even though it is in the file. Which fix?- Reverse the filter:
grep -v error journal.log - Search in depth:
grep -r error journal.log - Count the lines:
grep -c error journal.log - Ignore case:
grep -i error journal.log
Show the answer
Ignore case:
grep -i error journal.loggrepis case-sensitive:errordoes not matchERROR.-vwould keep the lines without the pattern,-rwalks directories and-cwould only print a number.- Reverse the filter: