Filtering text streams
The filter commands: small programs that read a text stream, change it, and write it out again — cut, sort, uniq, tr, sed and the rest — plus the flags on each that the exam actually asks about.
Lesson 2 of 8 in objective 103. GNU and Unix commands, part of LPIC-1 Exam 101-500.
Slicing and joining
cut takes columns out of each line: cut -d: -f1 /etc/passwd takes the first colon-separated field, and cut -c1-10 takes characters by position. It cannot handle runs of spaces as one delimiter, which is why awk is so often reached for instead. paste does the reverse, joining files side by side; join merges two sorted files on a shared field, like a database join.
head and tail take the first or last lines — ten by default, -n to change it. tail -f follows a file as it grows and is the single most-used command in this list on a live system. nl numbers lines, wc counts them (-l lines, -w words, -c bytes), and split cuts a large file into pieces.
Logs are rotated and compressed, and none of these filters can read a .gz on its own, so each compressor ships a cat-style front end that decompresses to standard output and writes nothing to disk: zcat for gzip, bzcat for bzip2, xzcat for xz. zcat access.log.gz | grep 404 | wc -l is the whole idiom, and the point of it is what does NOT happen — the compressed file is left alone and no uncompressed copy appears beside it, which matters on the nearly full partition that made the log worth compressing. gunzip -c and gzip -dc are the same thing spelled with options, and zgrep, zless and zdiff wrap the common filters the same way.
Sorting, deduplicating, translating
sort orders lines: -n sorts numerically rather than as text (so 10 comes after 9), -r reverses, -k selects a field, -u drops duplicates as it goes. uniq collapses ADJACENT duplicate lines only — which is why it is nearly always preceded by sort — and uniq -c prefixes each with a count, uniq -d shows only the repeats.
tr translates or deletes characters, reading standard input only: tr a-z A-Z upcases, tr -d '\r' strips carriage returns, tr -s squeezes runs into one. od dumps a file as octal, hex or characters, which is how you see what is actually in a file that looks empty.
sed, the stream editor
sed applies an editing script to every line as it passes. The substitution form is the one to know cold: sed 's/old/new/' replaces the first match on each line, sed 's/old/new/g' replaces every match, and a number in that position replaces the nth. Addresses restrict where a command applies — sed '2d' deletes line 2, sed '/error/d' deletes matching lines, sed -n '5,10p' prints a range and nothing else, because -n suppresses the automatic printing.
sed -i edits the file in place instead of writing to standard output. It is the flag that turns a safe experiment into a change, and on the exam it is the answer whenever a question says the file itself must be modified.
Worth carrying in
- cut -d: -f1
- First colon-delimited field of each line.
- sort -n -k2
- Numeric sort on the second field. -u also drops duplicates.
- uniq -c
- Collapse adjacent duplicates and count them. Sort first.
- tr a-z A-Z
- Translate characters. -d deletes, -s squeezes repeats.
- wc -l
- Count lines. -w words, -c bytes.
- tail -f
- Follow a growing file.
- sed 's/a/b/g'
- Substitute every occurrence on each line. Without g, only the first.
- sed -n '5,10p'
- Print a line range only; -n suppresses the default output.
- sed -i
- Edit the file in place.
- od -c
- Dump a file as characters, revealing what you cannot see.
- zcat file.gz
- Decompress to standard output, leaving the file compressed. bzcat and xzcat for the other two.
What the exam does with this
uniqonly removes ADJACENT duplicates. A question showing unsorted input withuniqalone is testing this.- The g flag on a
sedsubstitution is per line, not per file — without it, one replacement per line. - sort without -n compares as text, so 100 sorts before 9.
- Objective
- 103. GNU and Unix commands
- Share of the exam
- 43.33% (the whole objective)
- Questions in this lesson
- 10
- Signed for by a person
- 0
Partly checked. None of the 10 questions here has been read against the cited source by a person. 10 questions have been checked against their cited clause by an automated pass — which is not the same thing, and is not a signature.
Only questions a person has signed for are used in mock exams here. That is the whole difference between the two kinds of checking above.
How these questions are written — where each question comes from, what the verification ledger records, and what happens when one is found wrong.
Drill this lesson
A lesson is one sitting: the trainer draws a short run from these questions alone and spaces the ones you get wrong.
Practise Filtering text streams
Questions in this lesson
- A file contains one plain integer per line. Sorting it with plain `sort` produces the order 1, 10, 2, 20, 3. Which option makes sort order the numbers by value? machine-checked
- Which command reads standard input and writes it out with every lowercase letter converted to uppercase? machine-checked
- A text file contains several blank lines. Which command numbers only the non-empty lines by default? machine-checked
- Two files each contain 100 lines. You want line 1 of the first file next to line 1 of the second, line 2 next to line 2, and so on, separated by a tab. Which command does this? machine-checked
- In config.txt some lines contain the word `foo` more than once. Which command prints the file with EVERY occurrence on every line replaced by `bar`, leaving the file itself unchanged? machine-checked
- Select the TWO true statements about the uniq command. machine-checked
- Type the complete command that displays the last 20 lines of the file /var/log/syslog. Pass the file as an argument, not through a redirect. machine-checked
- Type the complete command that reports ONLY the number of lines in the file report.txt, passing the file as an argument. machine-checked
- The file /etc/passwd has colon-separated fields. Type the complete command that prints only the third field (the UID) of every line, using cut and passing the file as an argument. machine-checked
- A partition is nearly full and you have been sent access.log.gz. You need to read its contents on stdout without leaving an uncompressed copy on disk and without destroying the compressed file. Which command does that? machine-checked
Practise Filtering text streams