1

I have the following input file:

-Queue ID-  --Size-- ----Arrival Time---- -Sender/Recipient-------
0F2F77F472  4343854 Fri Oct  7 10:13:41  email@example.com
(host redacted.host.com[x.x.x.x] said: 452 4.2.2 Over quota (in reply to end of DATA command))
                                         recipient@example.com

03A017F486   992790 Fri Oct  7 13:09:44  email@redacted.com
(host host.redacted.net[y.y.y.y] said: 452-4.2.2 The email account that you tried to reach is over quota. Please direct 452-4.2.2 the recipient to 452 4.2.2  redacted (in reply to RCPT TO command))
                                         redacted@example.net

In my AWK script, I wrote something like this:

BEGIN {
  RS = "\n\n"
}

{
  ... do something ...
}

The header would get processed as well, so in my pipeline, before passing to the AWK, I do:

... | tail +2 | awk -f script

I acknowledged that to skip the very first line in AWK, mostly, is by doing this simple trick:

{
  if (NR > 1) {
    ...
  }
}

It should work but the RS = "\n\n" that I set in the BEGIN block, would make the first record be ignored (AWK interprets the header as part of the first record).

Tests:

$ awk 'BEGIN { RS = "\n\n" } { print NF }' sample
28
41

$ awk 'BEGIN { RS = "\n\n" } NR > 1 { print NF }' sample
41

Is there any way that I can achieve the goal without relying on any external tool?


GNU Awk 5.0.1, API: 2.0 (GNU MPFR 4.0.2, GNU MP 6.2.0)
1
  • Why the downvotes?
    – annahri
    Oct 12, 2022 at 2:05

1 Answer 1

4

Using any awk:

$ awk 'NR==1{RS=""; next} {print NF}' file
22
38

When you use RS = "\n\n" the behavior is undefined by POSIX and so then you need to use GNU awk or any other awk which specifically supports multi-char RS (read the man page for your awk). If, on the other hand, you use RS = "" that IS defined by POSIX as puting awk into paragraph mode where each record is separated from the next by blank lines.

Not setting RS until after the first line is read lets us easily read and discard that line.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.