1

I'm trying to extract all lines of text from mail log file that contain specific string (mail address). I know using grep will allow me to search for the address [email protected], but how do I get it to print out the entire line that is containing [email protected]? That way I could easily scour vast ammounts of log files for precise information regarding from and to fields in order to find out with whom did the [email protected] have interactions.

Thank you in advance.

1
  • Grep prints out the whole line containing the pattern. Please make more clear what you want. Post a sample text maybe.
    – Pilot6
    Jul 16, 2015 at 13:25

3 Answers 3

1
grep "mail\.addres@example\.org" filename.log

will print all lines containing [email protected]

4
  • Faster :P and +1
    – A.B.
    Jul 16, 2015 at 13:30
  • @A.B. But I do not see what the problem was
    – Pilot6
    Jul 16, 2015 at 13:31
  • me too :), but the answer is simple
    – A.B.
    Jul 16, 2015 at 13:32
  • great, I can not even begin to think how did I manage to miss that :) solved!
    – Mephius
    Jul 17, 2015 at 6:00
0

Simple answer, using awk

awk '/mail\.addres@example\.org/' file.log
0

Clearly quite late to the party here, sounds similar to what I have been learning. I use sudo because I don't access things as root but that's personal preference I guess:

sudo grep "[email protected]" /path/to/file/mail.log

The output being plastered in the terminal (not ideal). I did find however that using redirection >:

> test.txt

Was a good idea. Yields the result with the command, bypass the output to the terminal window and put it straight into a file for me to open with notepad++ later. Something I did come across during my learning was the difference between one AND two angle brackets which was pretty handy:

>       # Overwrites destination file with current output
>>      # Adds current output to tail of file

Very handy when I don't want to keep creating new files for each search I do.

3
  • 1
    You don't need sudo (ie you don't need root privilege) to read most files, though I guess you might need it to read a mail log. Of course, running as root routinely is discouraged.
    – Zanna
    May 18, 2018 at 15:56
  • The problem with not using sudo is when I do need permissions, I was finding my commands were failing which was a pain in the behind. Most of what I am working on requires permissions so I find it a good habit. But, yeah worth pointing out for others reading this at a later date. May 21, 2018 at 7:31
  • oh... using sudo for every command is nearly equivalent to running as root (i.e. a bad idea)... principle of least privilege
    – Zanna
    May 21, 2018 at 7:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .