Linux grep multiple lines

Regex (grep) for multi-line search needed [duplicate]

I’m running a grep to find any *.sql file that has the word select followed by the word customerName followed by the word from . This select statement can span many lines and can contain tabs and newlines. I’ve tried a few variations on the following:

$ grep -liIr --include="*.sql" --exclude-dir="\.svn*" --regexp="select[a-zA-Z0- 9+\n\r]*customerName[a-zA-Z0-9+\n\r]*from" 

The grep you’ve indicated here runs forever because you have not specified any files to search at the end of the command. The ‘—include’ is a filter of the files named and doesn’t actually provide you any files to be filtered.

3 Answers 3

Without the need to install the grep variant pcregrep , you can do a multiline search with grep.

-P activate perl-regexp for grep (a powerful extension of regular expressions)

-z Treat the input as a set of lines, each terminated by a zero byte (the ASCII NUL character) instead of a newline. That is, grep knows where the ends of the lines are, but sees the input as one big line. Beware this also adds a trailing NUL char if used with -o , see comments.

-o print only matching. Because we’re using -z , the whole file is like a single big line, so if there is a match, the entire file would be printed; this way it won’t do that.

(?s) activate PCRE_DOTALL , which means that . finds any character or newline

\N find anything except newline, even with PCRE_DOTALL activated

.*? find . in non-greedy mode, that is, stops as soon as possible.

\1 backreference to the first group ( \s* ). This is a try to find the same indentation of method.

As you can imagine, this search prints the main method in a C ( *.c ) source file.

Источник

How do I grep for multiple patterns on multiple lines?

and I want to extract entire block that starts from «begin» till «end». with awk we can do like awk ‘/begin/,/end/’ text . How to do with grep?

2 Answers 2

Updated 18-Nov-2016 (since grep behavior is changed: grep with -P parameter now doesn’t support ^ and $ anchors [on Ubuntu 16.04 with kernel v:4.4.0-21-generic])(wrong (non-)fix)

$ grep -Pzo "begin(.|\n)*\nend" file begin Some text goes here. end 

note: for other commands just replace the ‘^’ & ‘$’ anchors with new-line anchor ‘\n’ ______________________________

Читайте также:  Установка remmina kali linux

If you want don’t include the patterns «begin» and «end» in result, use grep with Lookbehind and Lookahead support.

Also you can use \K notify instead of Lookbehind assertion.

grep -Pzo "^begin$\n\K(.|\n)*(?=\n^end$)" file 

\K option ignore everything before pattern matching and ignore pattern itself.
\n used for avoid printing empty lines from output.

Or as @AvinashRaj suggests there are simple easy grep as following:

grep -Pzo "(?s)^begin$.*?^end$" file grep -Pzo "^begin\$[\s\S]*?^end$" file 

(?s) tells grep to allow the dot to match newline characters.
[\s\S] matches any character that is either whitespace or non-whitespace.

And their output without including «begin» and «end» is as following:

grep -Pzo "^begin$\n\K[\s\S]*?(?=\n^end$)" file # or grep -Pzo "(?<=^begin$\n)[\s\S]*?(?=\n^end$)" grep -Pzo "(?s)(?<=^begin$\n).*?(?=\n^end$)" file 

see the full test of all commands here (out of dated as grep behavior with -P parameter is changed)

Note:

^ point the beginning of a line and $ point the end of a line. these added to the around of "begin" and "end" to matching them if they are alone in a line.
In two commands I escaped $ because it also using for "Command Substitution"( $(command) ) that allows the output of a command to replace the command name.

From man grep:

-o, --only-matching Print only the matched (non-empty) parts of a matching line, with each such part on a separate output line. -P, --perl-regexp Interpret PATTERN as a Perl compatible regular expression (PCRE) -z, --null-data Treat the input as a set of lines, each terminated by a zero byte (the ASCII NUL character) instead of a newline. Like the -Z or --null option, this option can be used with commands like sort -z to process arbitrary file names. 

Источник

How to grep multiple strings or patterns in Linux

grep is a powerful tool that lets you search for strings, patterns, and words in files and text outputs from commands. It’s a great way to customize your search depending on your needs. Grep also offers a variety of options to make your search more efficient.

The name, grep, derives from the command used to perform a similar operation, using the Unix/Linux text editor ed: g/re/p

There are a variety of grep utilities that can help you search through your files, including grep, egrep, and fgrep. Out of these, fgrep is often the best option due to its speed and its focus on strings and words. However, if you prefer, you can also type grep to get the same results. Ultimately, it’s up to you which of these utilities you choose to use.

Читайте также:  Linux terminal переименовать файл

In this article, we will show you how to use grep to search multiple strings and patterns at once in Linux.

Grep Multiple Strings

If you want to search multiple patterns or strings in a particular file, use the grep functionality to sort within a file with the help of more than one input word in the command.

You can use the | symbol to grep multiple strings or patterns. grep use | symbol to separate two patterns in a command.

But before you actually use it, remember to escape it so that grep doesn’t mistakenly recognize it as part of the pattern. In other words, you have to use | instead of | in a grep command.

sudo grep [options] 'pattern1|pattern2|. ' /path/to/fileCode language: JavaScript (javascript)

What we’ve just did is asking grep to look for multiple patterns (pattern1, pattern2, …) in a file.

Alternatively, we can pass (pipe) the output of another command to grep to search it for required strings by running the following commands.

command | grep [options] 'pattern1|pattern2|. 'Code language: JavaScript (javascript)

In both cases, grep will output all the lines that might contain any of the listed patterns.

Search Multiple Strings in a file

Suppose you want to search for multiple strings in a single file located in /home/nl/test.txt , and you want to do it in a single run, use the command below.

sudo grep 'string1|string2|string3' /home/nl/test.txtCode language: JavaScript (javascript)

Alternatively, you can navigate into /home/nl and do the grep search. In this case, you can use only the filename in the command as you’re already in that directory.

sudo grep 'string1|string2|string3' /home/nl/test.txtCode language: JavaScript (javascript)

The syntax above only allows searching for raw strings. If you want to search for regex patterns, use -E or -e option. See the examples below to see what I mean.

sudo grep -E 'string1|string2|string3' /home/nl/test.txt # OR sudo grep -e string1 -e string2 -e string3 /home/nl/test.txtCode language: PHP (php)

Grep Exact Matches

If you want to find an exact match in a file, you can use the –w flag to sort the results before output. - means grep will only match the whole words.

Читайте также:  Check busy ports linux

For example, consider searching without –w , this command will bring all results that matches one of the given words. But when you use -w flag, searching will be limited as input words only match the first string. The second word is not highlighted because “–w” allows accurate matching with the pattern. Let’s see and example to see what it really means:

Grep Exact Match from a file

The first command brings all related data together in one place, while the second command helps you find exactly what you’re looking for with pinpoint accuracy.

Grep Both Uppercase and Lowercase

If you want to ignore case while searching for multiple strings and patterns, use -i option.

sudo grep -i 'string1|string2|string3' /home/nl/test.txtCode language: JavaScript (javascript)

Count Occurences

Sometimes, you would want to get the number of occurences of your search string in a file, use -c (count) option. For instance, if you want to know about the errors occurring in the system. The detail is recorded in the logs file. Knowing that how many times an error has happened would be very useful.

sudo grep -c 'string1|string2|string3' /home/nl/test.txtCode language: JavaScript (javascript)

Grep in multiple files

You can use wildcard characters to search for specific file types, such as *.txt files, to find multiple strings in multiple files.

sudo grep 'string1|string2|string3' /home/nl/*.txtCode language: JavaScript (javascript)

The above command will only look for specified search strings in all .txt files in /home/nl directory but not in any of its subdirectories. If you also want to look for multiple strings recursively in subdirectories, use -R flag in your command.

sudo grep -R 'string1|string2|string3' /home/nl/*.txtCode language: JavaScript (javascript)

This command will help you find your search strings in .txt files located in all subfolders under the /home/nl directory.

We hope that the information above helped you learn how to use grep to search for multiple patterns. We’ve also covered other software installation for Linux, such as How to install CMake, Airflow, Cura and ADB/fastboot on CentOS, in case you’re interested. If you have any suggestion, please feel free to leave a comment below.

Источник

Оцените статью
Adblock
detector