How to grep Match Two Strings in One Line, or Anywhere in File or Command Output
The command line grep tool is incredibly powerful and useful for searching for matches in files, sorting text and data, finding strings in large files, and so much more. One common situation many command line users may find themselves in, is seeking to grep match for two different strings in a single line.
You can use grep in the Terminal in MacOS for this, or any other unix-based command line prompt, whether that’s Linux, Windows WSL, any version of MacOS / Mac OS X, or even a rooted iOS/iPadOS if you’re into that kind of thing.
Use grep to Match Two Strings on the Same Line
From the command line, try the following syntax to grep match two strings in the same line:
grep "string1" /path/to/file |grep "string2"
For example, this may look like the following:
grep "error" /var/log/messages |grep "critical"
Using grep to Match Two Strings Anywhere in a File or Command Output
Another common situation is to need to use grep to match two different strings contained anywhere in a file or in the output of another command. You can use the -e flag for this purpose, like so:
grep -e String1 -e String2
So for example, for command output, you could use the following command string, in this case matching “CurrentCapacity” and “MaxCapacity”:
ioreg -l| grep -e CurrentCapacity -e MaxCapacity
The command line is intended for advanced users, but grep is a fairly forgiving command and if you’re new to the command line, is a reasonable command to explore and investigate.
And remember, you can also exclude strings from grep if you want to further refine your matching and command output.
If you have another approach to matching two strings in a file or matching multiple strings in command output, share with us in the comments below.
Amazon cheaper ($250) but they REfurbs.
First and last refurb I bought was a iMac 21″ had bad memory spot and occasionally had to reboot so was NOT fixed. and it died a premature death. so NO bargain.
Another way that only has one invocation of grep can be used if you know the order of the strings:
grep ‘string1.*string2’ pathToFile
The period matches any single character, and the asterisk says to repeat the previous pattern (“.”) zero or more times. Remember this if either of those characters are in your search strings ā they will have to be “escaped” with back-slashes, like “grep ‘\.pdf'”.
Note that using single quotes provides more protection of your strings from the shell than double quotes do. If you have dollar signs, semicolons, asterisks, question marks, brackets, or periods in your search strings (for example) the shell will try to interpret them if you use double quotes.
Also, consider using “fgrep” (for “fixed-string” grep) if your strings are literal, meaning it has no metacharacters to interpret, like “*”. fgrep is considerably faster than grep, but it can’t do abstract pattern matching. fgrep is a good way to avoid accidentally including metacharacters; you CAN do “fgrep ‘.pdf'”, for example.
Finally, manual pages are your friend! “man grep” or “man fgrep” will tell you all the gory details.