Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

In Unix, the grep program performs regular-expression text searching.

There are many grep regular expression metacharacters that control how the search is performed. This is a complex topic and we defer it to the Intermediate Unix workshop (for a preview, see the grep command).

...

Tip

Note that less and grep both support case-insensitive matching, and displaying line numbers, but they use slightly different options:

  • Case insensitive matching:
    • less -I or less --IGNORE-CASE
    • grep -i or grep --ignore-case
  • Display line numbers:
    • less -N or less --LINE_NUMBERS
    • grep -n or grep --line-number

It would be great if all Linux command options that mean the same thing used the same options – and some do, but some don't.

...

A key to text manipulation is understanding Unix streams. Every command and Unix program has three "built-in" streams: standard input, standard output and standard error


Most programs/commands read input data from some source, then write output to some destination. A data source can be a file, but can also be standard input. Similarly, a data destination can be a file but can also be a stream such as standard output.

The power of the Linux command line is due in no small part to the power of piping. The pipe operator ( | ) connects one program's standard output to the next program's standard input.

piping.png

The key to the power of piping is that most Unix commands can accept input from standard input instead of from files. So, for example, these two expressions appear equivalent:

...

  1. In the 1st, the more command reads some input from the jabberwocky.txt file
    • then writes the output to standard output, which is displayed on your Terminal
    • it pauses at page boundaries (--More--) waiting for input on standard input
    • when it receives a space on on standard input it reads more input from jabberwocky.txt
    • then writes the output to standard output, which is displayed on your Terminal
  2. In the 2nd, the cat command reads its input from the jabberwocky.txt file
    • then writes its output to standard output
    • the pipe operator ( | ) then connects the standard output from cat to standard input
    • the more command then reads its input from standard input, instead of from a file
      • then writes its output to standard output, which is displayed on your Terminal
      • more continues its processing as described in #1
    • Note that the cat command "blocks" writing to its standard output until more says it's ready for more input
      • this "write until block" / "read when input wanted & available" behavior makes streams a very efficient means of inter-process communication.

...