2

I want to filter some of the mail IDs returned by mailq (Postfix). However, the format is not very "grep-friendly", since the information for a single mail spans multiple lines:

  9F701869D2     1356 Mon Aug 23 12:13:43  some@sender
  (host some.host[1.2.3.4] said: 450 4.1.1 some error message)
                                           some@recipient

  9437586CF4     3153 Sat Aug 21 09:36:40  some@other.sender
  (host some.host[1.2.3.4] said: 450 4.1.1 some error message)
                                           some@other.recipient

What's the easiest way to get, for example, all mail IDs where some particular error code occurred? Maybe by removing the (single) newlines, then greping and then cuting? Or by using some other, more suitable tool?

Please explain your answer. My main interest is not a copy-and-paste solution for my current problem (filtering by error code), but rather understanding how to easily parse such multi-line output.

Heinzi
  • 2,217
  • 5
  • 32
  • 52

4 Answers4

2

What about:

grep -C 1 said:

-C is context and returns one line before & after "said:". Also, for future reference, -A is for after, -B is for before.

grep -C 1 said: | grep ^[0-9] | cut -f1 -d " "

(for the list of IDs)

If you need something more complex, then you would have to use awk.

jftuga
  • 5,731
  • 4
  • 42
  • 51
1

apq is a concise way to parse mailq or postqueue:

https://github.com/alexjurkiewicz/apq

enter image description here

1

I would use Perl and the Postfix::Parse::Mailq module.
If you don't know Perl it won't be very easy to right the good script but feel free to ask help.
If you are interested on how the module parse the output check the source

radius
  • 9,633
  • 25
  • 45
0

As long as you can figure out something to use to isolate the sender or receiver address, mailq can be parsed using awk:

mailq | grep @ | awk {'print $7'}
DustWolf
  • 131
  • 4