The number after -A switch tells how many lines we want to print. The switch -A (please note that it is capital A) instructs grep to print the lines after the matching line. It’s possible to make grep print out lines going before or after the matching lines. The command is grep -c Laptop * grep -c Laptop *įind lines coming before or after the lines with matching strings We can ask grep to find the number of lines containing the required string for each file. Let’s find all lines not containing “Laptop” string in our files: grep -v Laptop *įind a number of lines containing a string We can do it using grep, because it has a switch -v to do just that. Sometimes we need to find lines in a file NOT containing a particular string. Grep command is case sensitive by default, therefore the command grep laptop * does not return anything: grep laptop can make grep ignore case by using -i switch: grep -i laptop * grep -i laptop * Look at the output of command grep Laptop *: grep Laptop * We can also use wildcard character * if we want to perform a search in all files in the current directory. This grep command returns the lines containing “Laptop” string from both files and specifies the name of the file for each line: grep Laptop inventory.txt stock.txt If we search for a string in multiple files we can specify several files like this: Search for a string in multiple files using grep. The most basic usage of grep is the following grep Toshiba inventory.txtĪfter the grep commands itself we specify the string we want find (Toshiba) and then the filename (inventory.txt). Search for a string in a file using grep.
GREP LINUX INSTALL
Grep comes preinstalled on any modern Linux distribution, so we can start using it without having to install anything.īefore 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:
Grep utility is usually used to find lines in a file/files containing particular strings and substrings. Grep stands for Global Regular Expression Print.