Using grep command in Linux
What is grep ?
Grep stands for Global Regular Expression Print. Grep utility is usually used to find lines in a file/files containing particular strings and substrings.
Grep comes preinstalled on any modern Linux distribution, so we can start using it without having to install anything.
Before we start looking at usage examples of grep utility, let’s create two files in our working directory (I named them inventory.txt and stock.txt )with the following content:
stock.txt:
ID Brand Type S/N 10 Lenovo Laptop 198756 11 Asus PC 665321 12 Acer PC 333777 13 Samsung Laptop 223588 14 Dell PC 125854 15 Dell Laptop 982354
inventory.txt:
ID Brand Type S/N 1 IBM Laptop 123456 2 Apple PC 654321 3 HP PC 333777 4 Sony Laptop 225588 5 Toshiba PC 123654 6 Toshiba Laptop 987654
Search for a string in a file using grep.
The most basic usage of grep is the following grep Toshiba inventory.txt
After the grep commands itself we specify the string we want find (Toshiba) and then the filename (inventory.txt).
orkhans@matrix:~/grep$ grep Toshiba inventory.txt 5 Toshiba PC 123654 6 Toshiba Laptop 987654
Search for a string in multiple files using grep.
If we search for a string in multiple files we can specify several files like this:
grep Laptop inventory.txt stock.txt
This grep command returns the lines containing “Laptop” string from both files and specifies the name of the file for each line:
orkhans@matrix:~/grep$ grep Laptop inventory.txt stock.txt inventory.txt:1 IBM Laptop 123456 inventory.txt:4 Sony Laptop 225588 inventory.txt:6 Toshiba Laptop 987654 stock.txt:10 Lenovo Laptop 198756 stock.txt:13 Samsung Laptop 223588 stock.txt:15 Dell Laptop 982354
We can also use wildcard character * if we want to perform a search in all files in the current directory.
Look at the output of command grep Laptop *:
orkhans@matrix:~/grep$ grep Laptop * inventory.txt:1 IBM Laptop 123456 inventory.txt:4 Sony Laptop 225588 inventory.txt:6 Toshiba Laptop 987654 stock.txt:10 Lenovo Laptop 198756 stock.txt:13 Samsung Laptop 223588 stock.txt:15 Dell Laptop 982354
Make grep command ignore case
grep command is case sensitive by default, therefore the command grep laptop * does not return anything:
orkhans@matrix:~/grep$ grep laptop * orkhans@matrix:~/grep$
We can make grep ignore case by using -i switch: grep -i laptop *
orkhans@matrix:~/grep$ grep -i laptop * inventory.txt:1 IBM Laptop 123456 inventory.txt:4 Sony Laptop 225588 inventory.txt:6 Toshiba Laptop 987654 stock.txt:10 Lenovo Laptop 198756 stock.txt:13 Samsung Laptop 223588 stock.txt:15 Dell Laptop 982354
Inverse search using grep
Sometimes we need to find lines in a file NOT containing a particular string. We can do it using grep, because it has a switch -v to do just that.
Let’s find all lines not containing “Laptop” string in our files:
orkhans@matrix:~/grep$ grep -v Laptop * inventory.txt:ID Brand Type S/N inventory.txt:2 Apple PC 654321 inventory.txt:3 HP PC 333777 inventory.txt:5 Toshiba PC 123654 stock.txt:ID Brand Type S/N stock.txt:11 Asus PC 665321 stock.txt:12 Acer PC 333777 stock.txt:14 Dell PC 125854
Find a number of lines containing a string
We can ask grep to find the number of lines containing the required string for each file. Switch -c instructs grep to do that.
The command is grep -c Laptop *
orkhans@matrix:~/grep$ grep -c Laptop * inventory.txt:3 stock.txt:3
Find lines coming before or after the lines with matching strings
It’s possible to make grep print out lines going before or after the matching lines.
The switch -A (please note that it is capital A) instructs grep to print the lines after the matching line. The number after -A switch tells how many lines we want to print.
This command will output the matching line with the two following lines:
orkhans@matrix:~/grep$ grep -A 2 Acer * stock.txt:12 Acer PC 333777 stock.txt-13 Samsung Laptop 223588 stock.txt-14 Dell PC 125854
The switch -B (please note that it is capital B) instructs grep to print the lines before the matching line. The number after -B switch tells how many lines we want to print:
orkhans@matrix:~/grep$ grep -B 2 Sony * inventory.txt-2 Apple PC 654321 inventory.txt-3 HP PC 333777 inventory.txt:4 Sony Laptop 225588
Search for a regex pattern using grep
We can use grep to search for a regex pattern rather than plain string. We should provide regex pattern in double quotes.
orkhans@matrix:~/grep$ grep "A.*3" * inventory.txt:2 Apple PC 654321 stock.txt:11 Asus PC 665321 stock.txt:12 Acer PC 333777
Conclusion
These were some basic usage examples of grep. Grep is a very useful and flexible utility, especially because of regex support. Regex allows us to build very complex patterns, which helps a lot when dealing with large text files. You can learn more about regex here.
Thank you for reading.