grep

We are going to dive right in as follows:

reader@ubuntu:~/scripts/chapter_10$ vim grep-file.txt
reader@ubuntu:~/scripts/chapter_10$ cat grep-file.txt
We can use this regular file for testing grep.
Regular expressions are pretty cool
Did you ever realise that in the UK they say colour,
but in the USA they use color (and realize)!
Also, New Zealand is pretty far away.
reader@ubuntu:~/scripts/chapter_10$ grep 'cool' grep-file.txt
Regular expressions are pretty cool
reader@ubuntu:~/scripts/chapter_10$ cat grep-file.txt | grep 'USA'
but in the USA they use color (and realize)!

First of all, let's explore the basic functionality of grep, before we move on to regular expressions. What grep does is really simple, as stated in man grep: print lines matching a pattern.

In the preceding example, we created a file with some sentences. Some of these start with capital letters; they mostly end differently; and they use some words that are similar, but not really the same. These and more characteristics will be used in further examples.

To start, we use grep to match a single word (the search is case-sensitive by default), and print that. grep has two operating modes:

  • grep <pattern> <file>
  • grep <pattern> (which needs input in the form of a pipe, or |)

The first operating mode lets you specify a filename from which you want to specify the lines that need to be printed, if they match the pattern you specify. The grep 'cool' grep-file.txt command is an example of this.

There is another way of using grep: in streams. A stream is something in transit to your Terminal, but which can be changed while on the move. In this case, a cat of the file would normally print all lines to your Terminal.

However, with the pipe symbol (|) we redirect the output of cat to grep; in this case, we only need to specify the pattern to match. Any line that does not match will be discarded, and will not be shown in your Terminal.

As you can see, the full syntax for this is cat grep-file.txt | grep 'USA'.

Piping is a form of redirection that we will further discuss in Chapter 12, Using Pipes and Redirection in Scripts. For now, keep in mind that by using the pipe, the output of cat is used as input for grep, in the same manner as the filename is used as input. While discussing grep, we will (for now) use the method explained first, which does not use redirection.

Because the words cool and USA are only found in a single line, both instances of grep print just that line. But if a word is found in multiple lines, all of them are printed in the order grep encounters them (which is normally from top to bottom):

reader@ubuntu:~/scripts/chapter_10$ grep 'use' grep-file.txt 
We can use this regular file for testing grep.
but in the USA they use color (and realize)!

With grep, it is possible to specify that instead of the default case-sensitive approach, we would like the search to be case-insensitive. This is, for example, a great way of finding errors in a log file. Some programs use the word error, others ERROR, and we've even come across the occasional Error. All of these results can be returned by supplying the -i flag to grep:

reader@ubuntu:~/scripts/chapter_10$ grep 'regular' grep-file.txt 
We can use this regular file for testing grep.
reader@ubuntu:~/scripts/chapter_10$ grep -i 'regular' grep-file.txt
We can use this regular file for testing grep.
Regular expressions are pretty cool

By supplying -i, we see now that both 'regular' and 'Regular' have been matched, and their lines have been printed.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.87.161