grep is powerful, using its regular expressions you can do anything it terms of matching in a single line. Lets see some examples.
starting with grep and its options
grep follows the following format
$ grep [options] regex [file(s)]
- i: case insensitive.
- v: invert matching results.
- c: print the number of matches or non-matches if -v is used.
- l: print the filename of match instead of the line matched text.
- L: print the filenames that didnt matched text.
- n: print the line number of the matched text.
generate files with some data to work with grep
lets generate a file with some strings to work with our grep examples
$ ls /bin/ > ~/test_1.txt
The above command will generate a listing of all files inside /bin and will redirect the output at your home directory in a file named test_1.txt
echo "this is a test file" > ~/test_2.txt
The above command created a file named test_2.txt with the text “this is a test file”
Example 1: simple text search
We have to check if text ‘zip’ exists in files test_1.txt or test_2.txt, to do this we can run the following:
$ grep zip test_1.txt test_2.txt
We can see the occurances with lines that contain text ‘zip’ in file test_1.txt and none occurance in file test_2.txt
Example 2: Print the count of occurances
Many times we dont need the full output, we just need to know how many times a match exists, to do this we can run:
$ grep -c zip test_*txt
-c: this option tells grep to count number of occurances.
We can see that text ‘zip’ exists 21 times in file test_1.txt and 0 times on test_2.txt.
Note: on your computer the count of occurances might vary on file test_1.txt
Example 3: Print only file names that have occurances
To get only files with occurances we can run:
$ grep -l zip test_*txt
- l: this option tells grep to print only files that have the text ‘zip’
Which is the only file that contains the text ‘zip’ inside.
Example 4: Print only file names that dont have occurances
To get only files without any occurances of text ‘zip’ we can run:
$ grep -L zip test_*txt
- L: this option tells grep to print only files that dont match with a search
You might not know it, but you have used regular expressions to do the search.
Searching for text ‘zip’ you have asked grep to do the following:
- Search each line of files for a sequence of characters ‘zip’.
- The characters can start everywhere in the line.
- The characters can end everywhere in the line.
But regular expressions can be even more powerfull and produce even more narrow results based on our needs.
Example 5: Find lines that start with the text ‘zip’
To get only lines that start with the text ‘zip’ we can use the ‘^’ metacharacter.
$ grep ^zip test_1.txt
We can see that all lines start with text ‘zip’.
Example 6: Find all lines that do not start with text ‘zip’
To get all lines that do not start with text ‘zip’ we can use the -v option which inverts results.
$ grep -v ^zip test_1.txt | tail -n 20
the tail command here is used to limit the number of output to 20 lines.
We can see that there is no line that starts with ‘zip’.
Example 7: find all lines that end with text ‘cat’
To get all lines that end with cat, we can use the ‘$’ metacharacter.
$ grep cat$ test_1.txt
We can see that all lines end with ‘cat’ text.
End of part 1
This was the first part of my grep tutorial, i hope you found it very easy to start and you dont afraid to use grep, grep is powerfull and can be difficult to master, but its not so difficult todo simple things that can be the 70% of day to day work.