Issue
I have tomcat access logs in multiple files. All the files are under the same directory. I am in MacOS.
Like this in one of the files:
POST /context HTTP/1.1 200 266 20 <url>.com
GET /context1 HTTP/1.1 200 266 20 <url>.in
POST /context2 HTTP/1.1 200 266 20 <url>.de
Now I want to grep/search and print the lines which matches with "context" and ".com".
This has to be done over the multiple files in the directory.
I tried this grep "/context" . | grep ".com"
but this does not work over a directory.
Solution
If the .com
always follows /context
, try
grep 'context.*\.com' *
(or a better wildcard if you have other files; maybe try tomcat*.log
instead of *
?)
If the patterns could be in any order, you could use
grep -E 'context.*\.com|\.com.*context' *
(The -E
option switches to a different regex dialect which lets you use |
for "or". You could also remove the -E
option and use \|
but I think this is clumsy and confusing.)
... or switch to Awk.
awk '/context/ && /\.com/' *
Answered By - tripleee Answer Checked By - Mary Flores (WPSolving Volunteer)