-1

I've been running a script to extract Exchange mail recipient addresses and update my local postfix relay_recipients table for years. Script comes from The Book of Postfix, has worked flawlessly until now.

The key line that is causing issues still works fine on one server, but gives me an empty result on the other.

cat /home/username/mailrecipients.txt extra_recipients | tr -d \" | tr , \\n | tr \; \\n | tr -d '\r' | awk -F\: '/(SMTP|smtp):/ {printf("%s\tOK\n",$2)}' | grep -vi --file=blacklist

The script combines some hardcoded addresses with the mail recipients files, strips out unneeded stuff, and generates a long list of email addresses with an OK and newline. Everything works perfectly up to the grep. On one server, piping the output to grep with inverse match of the patterns contained in blacklist file strips out the addresses I don't want and returns the rest correctly. On the other server, with the same version of grep, I get an empty result. The three files (blacklist, mailrecipients.txt, and extra_recipients) are identical on both servers.

Any ideas on what's happening here?

EDIT: Output of the awk command before grep:

email1@mycompany.com  OK
email2@mycompany.com  OK
email3@mycompany.com  OK
restricted@mycompany.com  OK

blacklist file: restricted

Expected result:

email1@mycompany.com  OK
email2@mycompany.com  OK
email3@mycompany.com  OK

One server returns this correctly, and the other server returns an empty set.

  • Welcome to SO, thanks for showing your efforts, please do add sample of input and expected output in your question also to make it more clear, thank you. – RavinderSingh13 Aug 11 '22 at 18:16
  • 1
    Thanks! I added input and expected result samples as requested. I then went back and retested on each server and at that point I finally noticed the empty line in the blacklist file. I could have sworn I had checked that before, and had spent a couple of hours testing each server individually. – Bryan Palmer Aug 11 '22 at 19:36
  • You don't need a whole bunch of other tools and pipelines when you're using awk, whatever your script is trying to do could be done in 1 simple awk script. If you post a new question with a [mcve] and tag it with awk we can help you with that. By the way, you're having to add extraneous backslashes because you aren't quoting your strings properly. `tr \; \\n` should be `tr ';' '\n'` for example, see https://mywiki.wooledge.org/Quotes. Not my downvote btw. – Ed Morton Aug 12 '22 at 23:28
  • Thanks Ed! The awk code was just pulled from the Postfix book, and it works fine. Maybe I'll refactor it eventually to make the strings quoted properly, but it is working as intended as quoted from the reference book. My issue was with the final grep step, but that was because there was an empty line, and checking for inverse match caused it to report an empty set instead of what was expected. – Bryan Palmer Aug 16 '22 at 15:38

1 Answers1

0

Ok, I feel bad now, but the reason it isn't working is because the "blacklist" file on one server had an empty line at the bottom of the file I didn't see. Having a blank line in the pattern match file caused the entire thing to work differently than expected.

Once I edited the blacklist file and removed the empty line, everything works correctly again.