- [Instructor] Welcome to the next video of section four,
Searching and Mining a Text Inside a File with Grep.
In the previous video, we learned how to use
regular expressions.
This video will help you learn how to locate
data items of a given specification from a pool of data.
Searching inside a file is an important use case
in text processing.
We may need to search through thousands of lines in a file
to find out some required data by using certain
specifications.
The G-R-E-P command is the magic unique utility
for searching in text.
It accepts regular expressions and can produce output
in various formats.
Additionally, it has numerous entering options.
Let's see how to use them:
To search for lines of text that contain the given pattern;
We can also read from standard NS files;
perform a search in multiple files
using a single grep invocation as follows;
We can highlight the word in the line
by using the minus minus color option as follows.
Usually, the grep command only interprets
some of these special characters in match underscore text.
To use the full set of regular expressions
as input arguments, the -E option should be added.
Which means an extended regular expression.
Or, we can use an extended regular expression enabled
grep command, egrep, for example.
In order to output only the matching portion of a text
in a file, use the -o option as follows.
In order to print all the lines, except the line
containing match underscore pattern, use...
The -v option added to grep inverts the match results.
Count the number of lines in which a matching string
or regx match appears in a file or text as follows.
It should be noted that -c counts only the number of
matching lines, not the number of times a match is made,
for example.
Even though there are six matching items,
it prints two, since there are only two matching lines.
Multiple matches in a single line are counted only once.
To count the number of matching items in a file,
use the following trick.
Print the number lines of the match string as follows.
If multiple files are used, it will also print the file name
with the result as follows.
Print the character or byte offset
at which a pattern matches as follows.
The character offset for a string in a line is
the counter from zero, starting with the first character.
In the proceeding example, it's not
the seventh offset position, that is,
not starts from the seventh character in the line,
that is gnu is not unix.
The -b option is always used with -o.
To search over multiple files and list which files
contain the pattern, we use the following.
The inverse of the -l argument is -L.
The -L argument returns a list of non-matching files.
We've seen the basic usage of the grep command,
but that's not it, the grep command comes with
even more features, let's go through those.
Recursively search many files.
To recursively search for a text over many
directories of descendants use the following command.
In this command . specifies the current directory.
The options -R and -r mean the same thing
when used with grep.
For example...
"test_function()" exists in line 16 of missseattle/test.c.
This is one of the most frequently used commands
by developers.
It's used to find files in the softcode where certain
texts exists.
Ignoring case of a pattern, the -i argument helps
match patterns to be evaluated without considering
the upper case of lower case.
For example,
grep, by matching multiple patterns.
Usually we specify single patterns for matching,
however, we can use arguments -e to specify
multiple patterns for matching as follows.
This will print the lines that contain
either of the patterns and output one line
in for each match.
For example...
There's also another way to specify multiple patterns.
We can use a pattern file for reading patters,
write patterns to match line-by-line and execute
grep with a -f argument as follows.
For example...
Including and excluding files in a grep search.
Grep can include or exclude files in which to search.
We can specify include files or exclude files by using
wild card patterns.
To search only for .c and .cpp files recursively
in a directory by excluding all other file types
use the following command.
Note, that sum {string one, string two, string three}
expands as sum string one, sum string two,
sum string three.
Exclude all readme files in the search as follows.
To exclude directories, use the --exclude-dir option.
To read a list of files to exclude from a file use the
- -exclude-from file.
Using grep with xargs with 0 byte suffix.
The xargs command is often used to provide a list of
file name as a command-line argument to another command.
When file names are used as command-line arguments it's
recommended to use a 0 byte terminator for the file names
instead of a space terminator.
Some of the file names can contain a space character
and it be misinterpreted as a terminator, and a single
file name may be broken to two file names.
For example, newfile.tags can be interpreted as
two file names, 'new' and 'file.text'
This problem can be avoided by using a 0 byte suffix.
We use xargs so as to accept the standard in-text from
commands such as grep and fine.
Such commands can output text to stand it out with a
zero byte suffix.
In order to specify that the input terminator for file names
is 0 byte, we should use the -0 with xargs.
Create some test files as follows.
In the following command sequence grep outputs file names
with 0 byte terminator/0.
Because of the -Z option with grep xargs, -0
reads the input and spearates file names with
a 0 byte terminator.
Usually -Z is used along with -l.
Silent output for grep.
Sometimes, instead of actually looking at the match strings,
we are only interested in whether there was a match or not.
For this we can use the quiet option, -q,
where the grep command does not write any output
to the standard output.
Instead it runs the command and returns an exit status
based on success or failure.
We know that a command returns 0 on success
and non-0 on failure.
Let's go through a script that makes use of grep
in a quiet mode.
Protesting whether a match text appears in the file or not.
Student and a file name.
Student_data.text has the command argument.
Printing lines before and after text matches.
Context-based printing is one of the nice features of grep.
Suppose a matching line for a given match text is found,
grep usually prints only the matching lines.
But we may need end lines after the matching line or,
end lines before the matching line, or both.
This can be performed by using context line control in grep.
Let's see how to do it.
In order to print three lines after a match
use the -A option.
In order to print three lines before the match
use the -B option.
Print three lines after and before the match
and use the -C option as follows.
If there are multiple matches then each section
is delimited by a line --.
Awesome!
We've successfully learned how to search and mine a text
inside a file with grep.
In the next video, we'll learn about cutting a file
column-wise with cut.