If we check the output above, we can see that “test/app/readme.md” is in the list, too. The following command will print the same output: $ grep -R -include=*. Test/log/app_20200301.log:DATETIME - SQLException has OccurredĪs we can see, the file test/app/readme.md appears in the output as well.Īlternatively, we can also use one single –include option and let the GLOB expression contain multiple extensions. Test/log/app_20200401.log:DATETIME - ClassCastException has Occurred Test/log/app.log:DATETIME - NullPointerException has Occurred Test/app/change_log.log:Fix the NullPointerException Problem when calling external APIs Test/app/readme.md: - Exceptions are well handled Now, let’s search for the word “Exception” on *.log and *.md files: $ grep -R -include=*.log -include=*.md 'Exception' test –include=*.log is an example of the –include=GLOB option, which tells grep to only search files whose basename matches the given GLOB expressionĪlso, we can use multiple –include=GLOB options to ask the grep command to search on files that match multiple extensions.That is, it’s going to search the given pattern in files in any subdirectory under test I’m sure that this tutorial would have helped you to get a good understanding of what is grep command in Unix and how it is used in various conditions. Match all lines that contain the word hello in upper-case or lower-case.Match all lines that start with a digit following zero or more spaces.Match all lines that do not contain a vowel.Match all lines that contain any of the letters ‘a’, ‘b’, ‘c’, ‘d’ or ‘e’.Match all lines that start with ‘hello’.-c: displays the count of the matching patterns.-v: displays the lines not containing the specified pattern.-n: displays the lines containing the pattern along with the line numbers.-i: performs a case-insensitive search. grep is a short form for Global Regular Expression Print. The grep command supports a number of options for additional controls on the matching: grep command in Linux searches for a pattern of characters in a file. #5) Repetition Modifier: A ‘*’ after a character or group of characters is used to allow matching zero or more instances of the preceding pattern. “” will match all lines that do not contain x, y or z. A carat can be used at the beginning of the range to specify a negative range. E.g.“” will match all lines that contain a digit. A hyphen can be used while specifying a range to shorten a set of consecutive characters. #4) Character Range: A set of characters enclosed in a ‘’ pair specify a range of characters to be matched.Įxample: “” will match all lines that contain a vowel. #3) Escaped Characters: Any of the special characters can be matched as a regular character by escaping them with a ‘\’.Įxample: “\$\*” will match the lines that contain the string “$*” #2) Wildcard Character: ‘.’ Is used to match any character.Įxample: “^.$” will match all lines with any single character. The strings “\” are used to anchor the pattern to the start and end of a word respectively. #1) Anchor Characters: ‘^’ and ‘$’ at the beginning and end of the pattern are used to anchor the pattern to the start of the line, and to the end of the line respectively.Įxample: “^Name” matches all lines that start with the string “Name”. Special characters are used to define the matching rules and positions. A regular expression is a string of characters that is used to specify a pattern matching rule. The pattern is specified as a regular expression.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |