-
Notifications
You must be signed in to change notification settings - Fork 0
bash4
Let's say you have a large file and want to find occurrences of some specific keyword or pattern. For that, we have grep and sed, which make use of regular expressions. For our purposes, we will be making simple patterns, as regular expressions are beyond the scope of this talk.
So assume we have a log file called log.txt and we want to find where the phrase "rsvp_event" shows up. To do so, we will use grep, and pass in the pattern and the file as arguments
$ grep 'rsvp_event' log.txt
We should be seeing all lines that contain the phrase we're looking for.
Grep also gives us an opportunity to look at pipes, which let's us chain together commands. Try to visualize each command or program as a black box that takes in some input (in the form of options and arguments) and gives us back an output. Normally, the input comes from stdin or standard in, which is whatever we type into the terminal, and the output is direct to stdout or standard out, which is what is printed on our terminal after we run the command. Using pipes, it is possible to redirect the output of one command into the input of another.
$ ls Documents | grep 'Scripts'
First, we list the contents of the Documents directory. Then we take that output, and pipe it to grep, which searches for any file or directory that has "Scripts" in its name. The pipe operator is represented by the vertical slash |. Finally, the result of the grep command is output to stdout, and we see the final product.