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.

  1. Concepts
  2. Questions
  3. Sort and link
  4. Scenarios
  5. 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 '^#' config drops 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,7 give on this /etc/passwd line?

    ```text
    lea:x:1000:1000:Lea:/home/lea:/bin/bash
    `

    • lea:/bin/bash
    • lea:/home/lea
    • lea:x
    • x:/bin/bash
    Show the answer

    lea:/bin/bash

    The fields are lea, x, 1000, 1000, Lea, /home/lea and /bin/bash: the first is the name, the seventh the shell. cut puts the delimiter back between the kept fields.

  • wc -l counts the … of a file; head -n 3 prints the … and tail -n 3 the ….

    Show the answer

    wc -l counts the lines of a file; head -n 3 prints the first three and tail -n 3 the last three.

    With no option, wc prints lines, words and bytes. With no -n, head and tail print ten lines.

  • cut -d: -f1,7: what is it?

    Show the answer

    Splits each line at the colons and keeps fields 1 and 7.

    -d sets the delimiter, -f picks the fields, numbered from 1. On /etc/passwd, those are the login name and the shell.

  • grep error journal.log does 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.log

    grep is case-sensitive: error does not match ERROR. -v would keep the lines without the pattern, -r walks directories and -c would only print a number.