I copied the text as /tmp/test1 and deleted the 'g' and saved as /tmp/test2. In the example of the OP's question, I think the following options work nicely, with the second best matching how I understand the question: grep -Pzo "abc(.|\n)*efg" /tmp/tes* To change the pattern type, you may also use -G/ -basic-regexp (default), -F/ -fixed-strings, -E/ -extended-regexp, -P/ -perl-regexp, -f file, and other.I relied heavily on pcregrep, but with newer grep you do not need to install pcregrep for many of its features. q/ -quiet/ -silent Do not output matched lines exit with status 0 when there is a match. threads Number of grep worker threads to use. l/ -files-with-matches/ -name-only Show only the names of files. no-index Search files in the current directory that is not managed by Git. all-match When giving multiple pattern expressions, this flag is specified to limit the match to files that have lines to match all of them. You may also combine patterns with Boolean expressions such as -and, -or and -not. Here is the syntax using git grep with multiple patterns: git grep -all-match -no-index -l -e string1 -e string2 -e string3 file So - if you want to find multiple regexps or strings in a line or paragraph or file then don't use grep, use awk. How about across a whole file? Again can't be done in grep and trivial in awk (this time I'm using GNU awk for multi-char RS for conciseness but it's not much more code in any awk or you can pick a control-char you know won't be in the input for the RS to do the same): awk -v RS='^$' '/R1/ & /R2/' Now, what if you wanted to match 2 regexps in a paragraph rather than a line? Can't be done in grep, trivial in awk: awk -v RS='' '/R1/ & /R2/' Which again are poor choices whereas with awk you simply use a string operator instead of regexp operator: awk 'index($0,S1) & index($0.S2)' Or again use 2 greps and a pipe: grep -F 'S1' file | grep -F 'S2' Now, what if you actually wanted to match literal strings S1 and S2 instead of regexps R1 and R2? You simply can't do that in one call to grep, you have to either write code to escape all RE metachars before calling grep: S1=$(sed 's///g s/\^/\\^/g' <<< 'R1') While in awk it'd be the concise, obvious, simple, efficient: awk '/R1/ & /R2/ & /R3/' Putting that aside, what if you wanted to extend your solution to match 3 regexps R1, R2, and R3. You'd have to use 2 greps and a pipe for that: $ echo 'theatre' | grep 'the' | grep 'heat'Īnd of course if you had actually required them to be separate you can always write in awk the same regexp as you used in grep and there are alternative awk solutions that don't involve repeating the regexps in every possible sequence. Lets say you want to find lines that contain the and heat: $ echo 'theatre' | grep 'the.*heat|heat.*the' To match 2 regexps R1 and R2 in grep you'd think it would be: grep 'R1.*R2|R2.*R1'īut what if R2 overlaps with or is a subset of R1? That grep command simply would not work while the awk command would. Don't try to use grep for this, use awk instead.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |