Technical Briefs
ROHAN help Files
![]()
grep
The grep command is used to search for all the lines in a file or group of files that contain a specified pattern. The syntax is:
rohan% grep "search string" file(s)where file(s) is the name of a file or a group of files to search.
Notice: If your search string contains punctuation, special characters or spaces, it is necessary to enclose it in double quotes, so that grep will interpret the characters properly.
For example, if you want to find out if the file named index.html contains the words this is a test, the syntax is:
rohan% grep "this is a test" index.htmlIf you want to search for the same words in several files, the syntax is:
rohan% grep "this is a test" index.html test.html me.htmlOr, if you want to search your entire directory, the syntax is:
rohan% grep "this is a test" *Notice: If grep does not find what you're looking for, it simply dies and brings you back to the rohan% prompt.
When grep finds the pattern, it lists the lines on which the pattern is found, or, if you are looking in multiple files, grep lists the filename followed by the line in the file where the pattern is located. Usually, it is helpful to type an option before the pattern to limit the search. The following options are commonly used:
- -l - lists only the filenames where the string was found
-i - upper and lowercase letters are treated the same.
-w - only search for complete words
-n - puts line numbers in front of the lines where the string was found.
-c - gives you the total number of matching lines it found.
-v - displays the lines where the string is NOT found.
The syntax for using an option is:
rohan% grep -option "search string" filenamegrep is a very powerful tool because it enables you to not only search for specific words, but it also allows for regular expressions. The use of regular expressions allows you to search for multiple items with similar, but not identical names. For example:
rohan% grep "\<G[a-z]*me\>" *This would find every word in your directory that starts with a capital G followed by zero or more lowercase letters and then ends with me. The following symbols are commonly used with grep regular expressions:
. match any single character except newline * match zero or more of the preceding characters ^ match the beginning of a line $ match the end of a line \< match the beginning of a word \> match the end of a word [ ] match one of the enclosed characters [^ ] match any character that is not enclosed \ take the following symbol literally
![]()