Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I am trying grep, egrep, fgrep, but am not getting the results I need.
I need to search our sendmail server, which archives a copy of every mail coming in and out. So it stores many thousands of emails! They are named according to a date stamp, but that's irrelevant. What I need is to find all emails between certain days, that have BOTH jsmith and twalker in them (we are trying to trace all emails these two have had with each other).
Right now, using the greps, I can get files returned that have EITHER, but I only want returns where BOTH are in the file.
I think you'll need a little script; something like this:
Code:
#!/bin/bash
for file in $(find /path/to/emails -type f); do
grep "jsmith" "$file" &> /dev/null && grep "twalker" "$file" &> /dev/null && echo "$file"
done
Make sure to fill in the correct path to your mails and modify the options for find to narrow down the results.
Then save this in a text file "findmails.sh" and execute with "sh findmails.sh". It will return the filenames of the files that have both "jsmith" and "twalker" in them.
grep works line by line---ie it returns the LINE which contains the keyword. In your case, the two keywords can be on different lines.
One crude way to do this is to have grep return enough lines of context so that the address fields are all included. Looking at a typical header, it seems that ~ 10 lines should work. So, something like this should work:
Well being the IT Administrator of a company, I am often asked to do things that I frown upon. However, this is just one of those "I never got an email saying this" type things. So both parties have asked me to find if the email was ever sent.
Your grep with the pipe is close to what I want, but wouldn't you know it, the second email address is often a "cc" which puts it on a separate line in the file, so I never get any mathces for both...
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.