Grep multiple strings in a file12/11/2023 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. If you’re looking for some tricks to using grep to match (or not match!) strings, then you should check out this article over at The Geek Stuff.Don't try to use grep for this, use awk instead. You can chain this with the pipe as many times as you need to.įinally, if you’re feeling really swish, then you might want to know how many matches there are rather than list the files: grep -l "COURSE: Course 1" * | xargs grep -l "COURSE: Course 2" | wc -l grep -l "COURSE: Course 1" * | xargs grep -l "COURSE: Course 2"īy piping the file names from the first result into the argument of the next grep – it was easy to find the files that had both courses booked. The courses aren’t on the same line so it’s not so straight forward but there’s a very simple way to find those pesky files. The problem I now had was that I wanted to know exactly how many had booked BOTH courses. I could then find out which ones were booking Course 2: grep -l "COURSE: Course 2" * After saving all of the files in a directory it was a case of running this command: grep -l "COURSE: Course 1" * Luckily, I know that the courses are prefixed with the word COURSE: and the names. One of the challenges that I recently faced was to list a bunch of text files only with a specific phrase.įor the sake of context, I was looking through emails of course bookings. Grep is most definitely not the most exciting tool to talk about.
0 Comments
Leave a Reply.AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |