With grep we can decide to get all words matching the string regardless of the string case by using the i option as follows. Notice that when we previously searched for a string, the search string had to be in the same case as the string in the file otherwise the search would not work. #Grep output file name full#The command when executed on the test.txt file won't output anything because there is no full word mail in it. We use the w option with grep to match full words as shown grep -w "mail" test.txt We get all lines with mail string highlighted, however, we may need to match full words. When we run the following command, grep "mail" test.txt The output is all occurrences of gmail in all files in the current directory. We could also search all files in a directory by using the * option. The output of this command is all occurrences of search string and for each occurrences the file name is written at the beginning of the each line in the output. We can also use grep to search for a string in multiple files by writing, grep "search string" file1.txt file2.txt The output is all lines without the string worker. Inversely we could get lines in the file that don't match the string worker by using the v option as shown below, grep -v "worker" test.txt The output of thus command is all occurrences of the word worker highlighted. To search the test.txt file for the string worker we could write grep "worker" test.txt The basic use of grep is searching for strings in a file. Throughout this article we shall run the grep commands on the file below. Once a match is found it is printed as output. This text patterns is referred to as a regular expression. We use it to search for a specific pattern of characters in a file. Grep is an acronym for global regular expression print. The grep command is one of the most useful commands in a linux system for searching for text or patterns in a given file.
0 Comments
Leave a Reply. |
AuthorSheila ArchivesCategories |