Loading…
Warp is now open-source Learn more
Loading…
| Command | Explanation |
|---|---|
| grep -Pzo '(?s)from.*to' <file_name> | This is the simplest way to use grep to match across multiple lines in a file. |
| ggrep -Pzo '(?s)from.*to' <file_name> | Use this when -P is not supported by grep after installing ggrep with brew install grep. |
| pcre2grep -M 'from(\n|.)*to' <file_name> | pcre2grep can simplify the command further by only requiring the -M flag. |
If you are searching to match across multiple words on the same line, the grep command takes the form:
$ grep ‘from.*to’ <file_name>For example:

Which uses regular expression syntax to match lines that contain all words complete until complete on the same line. This is because . means all characters while \ means as many as possible.
To multiline match with grep, the command becomes much more complicated:
# if your machine supports grep -P
$ grep -Pzo ‘(?s)from.*to’
# using ggrep instead
$ ggrep -Pzo ‘(?s)from.*to’For example:

If your machine does not support grep -P, you can install ggrep from homebrew-core using brew and the command:
$ brew install grepThis will then become available as ggrep.
The parameters for this are:
If you want to simply print out file names that have lines that have matches with the regular expression then you can alter the -o flag to -l which will list all matching file names.
$ grep -Pzo '(?s)success.*failure' process_output.txt
# or
$ ggrep -Pzo '(?s)success.*failure' process_output.txtFor example:

$ grep -Pzo '(?s)scheduled.*complete' process_output.txt
# or
$ ggrep -Pzo '(?s)scheduled.*complete' process_output.txtFor example:

$ grep -Pzo '(?s)failure.*complete' process_output.txt
# or
$ ggrep -Pzo '(?s)failure.*complete' process_output.txtFor example:

An alternative would be to take advantage of the pcre2grep extension which would simplify the command by adding the flag -M
$ pcre2grep -M 'from(\n|.)*to' <file_name>Where the -M or --multiline flags allow patterns to match more than one line. This is an alternative that packs inbuilt support for Perl Compatible regular expression and is usually already preinstalled in your system alongside grep. Otherwise, this can be installed using your package manager.
Alternatively, you can also use the (?s) trick from before to turn on PCRE\_DOTALL and make the dot character match new lines as well. Which simplifies the command to:
$ pcre2grep -M 'from(\n|.)*to' <file_name>When using grep across multiple lines it is important to be aware that the command will get both the first instance of the from word and will get everything up until the last instance of the to word. This will likely affect the output you expected, especially when there may be multiple instances of from or to in your document. Alternatively, tools such as awk or sed will start from the first instance of from but finish at the first instance of to.
It is important to know that the “strings” following the grep command will match the document based on the rules of regular expression. This means that simply typing in fail will also match failure. To match only specific words when matching across multiple lines you can use regular expression tools to match one words. For example:
$ grep -Pzo ‘(?s)\bfail\b.*\n.*\bsuccess\b’grep commands are also case sensitive but you can control this using the i flag to ignore case.
As always if you want to find out more about how to use the grep tool you can use:
$ man grepWhich will print out all the options with explanations. Or:
$ grep --helpWhich will print out a short page of all the available options.
Alternatively, tools such as awk and sed make can make this command much simpler to implement. For awk the command would be:
$ awk ‘/from/,/to/’ <file_name>where from is the first word or regular expression you are searching for and to is the final work you are looking for.
In sed the command is similar and takes the form:
$ sed -n ‘/from/,/to/p’ <file_name>As with the prior example, from is the first word or regular expression and to is the final word or regular expression you are looking for.
Efficiently count lines or occurrences in a file.
By default, grep is case sensitive
Learn how to filter and format the output of commands and logs using the grep, awk, uniq, head, and tail commands.
Excluding unwanted key terms or directories when using grep
Learn how to use grep to search for words and phrases within a directory and all its subdirectories, a specific directory, all files, and other variations.
How to filter lines and extract specific information from the output of commands or text files based on string patterns and regular expressions with grep.
$ grep ‘from.*to’ <file_name># if your machine supports grep -P
$ grep -Pzo ‘(?s)from.*to’
# using ggrep instead
$ ggrep -Pzo ‘(?s)from.*to’$ brew install grep$ grep -Pzo '(?s)success.*failure' process_output.txt
# or
$ ggrep -Pzo '(?s)success.*failure' process_output.txt$ grep -Pzo '(?s)scheduled.*complete' process_output.txt
# or
$ ggrep -Pzo '(?s)scheduled.*complete' process_output.txt$ grep -Pzo '(?s)failure.*complete' process_output.txt
# or
$ ggrep -Pzo '(?s)failure.*complete' process_output.txt$ pcre2grep -M 'from(\n|.)*to' <file_name>$ pcre2grep -M 'from(\n|.)*to' <file_name>$ grep -Pzo ‘(?s)\bfail\b.*\n.*\bsuccess\b’$ man grep$ grep --help$ awk ‘/from/,/to/’ <file_name>$ sed -n ‘/from/,/to/p’ <file_name>