The grep is the best command line tool for searching text in files. It also useful for the searching files containing specific files recursively. Grep has multiple command line options to make this tool great. Generally all the system administrator must uses these commands daily in there working. In this tutorial, you will know, How do i use grep command in Linux for searching text in files.
How to search text with grep:
Search for the text “rahul” in /etc/passwd file. It will display all matching lines containing searched text as result.
Use can also use piped input to grep
Use grep with case sensitive search
Use -i option to tell grep to ignore case during search. It means it will search for Rahul, RAHUL, raHUl etc.
Use grep to search multiple words
Use multiple strings to search in file with grep command with pipe seprated
Use grep to search in multiple files
Grep can search in multiple files as well. Search “rahul” in file1, file2 file3 etc.
Match search for whole word only
Use -w to search patterns matches whole word only. Like below command will not match rahul123, hellorahul, rahul456 etc.
Count total lines the word matched
Use -c to print total number of lines the searched string matched in files.
Print matched content with line number
Use -n to print matched content with line number of matched content .
Match whole line only
Using -x prints content only matched whole lines
Invert search with grep
The -v option only select lines those non-matching with searched string.
Search recursively in multiple files
grep commands also allows to search in file hierarchy recursively. Use -r to search recursively with grep command.
Print matched content filename only
Using -l prints the matched filename only. its useful with recursively search where lots of search content shows.
The post Using grep Command in Linux with Examples appeared first on TecAdmin.net.