If you want to search for special characters (for example: *, dot) in the content you have to escape the special character in the regular expression. Example 8.Escaping the special character (\) The line which has more than one space between hi and hello did not get matched in the above command. “hi \?hello” matches hi and hello with single space (hi hello) and no space (hihello). The special character “?” matches zero or one occurrence of the previous character. “hihello” will be matched by * as shown below. However, * character matches zero or more occurrence. If there is no space between hi and hello it wont match that. In the above example, the grep pattern matches for the pattern ‘hi’, followed by one or more space character, followed by “hello”. So you have to escape when you want to use it with the grep command. The character “+” comes under extended regular expression. If there is no space then it will not match. ” \+” matches at least one or more space character. The special character “\+” matches one or more occurrence of the previous character. In the above example it matches for kernel and colon symbol followed by any number of spaces/no space and “.” matches any single character. Messages.4:Oct 28 06:29:49 cloneme kernel: ACPI: PM-Timer IO Port: 0x1008 Messages.4:Jul 12 17:01:02 cloneme kernel: ACPI: PCI interrupt for device 0000:00:11.0 disabled The following example searches for a pattern “kernel: *” i.e kernel: and zero or more occurrence of space character. For example, the pattern ‘1*’ matches zero or more ‘1’. The special character “*” matches zero or more occurrence of the previous character. In case if you want to search for a word which has only 4 character you can give grep -w “….” where single dot represents any single character. i.e hello, cello etc., $ grep ".ello" input Now let us search for a word which has any single character followed by ello. Let us take the input file which has the content as follows. The special meta-character “.” (dot) matches any character except the end of the line character. The above commands displays the count of the empty lines available in the messages and anaconda.log files. Using ^ and $ character you can find out the empty lines available in a file. Just like ^ matches the beginning of the line only if it is the first character, $ matches the end of the line only if it is the last character in a regular expression. Oct 28 06:29:54 cloneme kernel: Kernel log daemon terminating.įrom the above output you can come to know when all the kernel log has got terminated. Jul 12 17:01:09 cloneme kernel: Kernel log daemon terminating. The following command will help you to get all the lines which ends with the word “terminating”. ^N matches line beginning with N.Ĭharacter $ matches the expression at the end of a line. The ^ matches the expression in the beginning of a line, only if it is the first character in a regular expression. Nov 10 13:25:46 gs123 ntpd: synchronized to LOCAL(0), stratum 10 Nov 10 01:17:17 gs123 ntpd: synchronized to LOCAL(0), stratum 10 i.e All the messages logged on November 10. In the following example, it displays all the line which starts with the Nov 10. In grep command, caret Symbol ^ matches the expression at the start of a line. Let us take the file /var/log/messages file which will be used in our examples. The future part 2 article will cover advanced regular expression examples in grep. This part 1 article covers grep examples for simple regular expressions. This articles is part of a 2 article series. Regular expressions search for the patterns on each line of the file. You can also use regular expressions with grep command when you want to search for a text containing a particular pattern. Please refer our earlier article for 15 practical grep command examples. Grep command is used to search for a specific string in a file. Most of the Linux commands and programming languages use regular expression. Regular expressions are used to search and manipulate the text, based on the patterns.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |