Script to remove line by line

Say I have an email with its message source. What I am trying to achieve is to run an anti-spam scan to determine which line in the message triggers the anti-spam program. The line that triggers the anti-spam program will produce an output of "Spam".

So, how can I write a script that will take out the last line, test the whole message, and then remove the last and 2nd last line, re-test the whole message, and the process will carry on until it returns the line that is spam.

Thanks heaps.

It would help to know what the command syntax that you are running to determine whether or not the file is spam, and more importantly if it indicates that it is spam via exit code, or message or some other mechanism.

Modulo some real information like that, guessing that the command reads the file and exits bad (non-zero) if the message is considered spam, then this might do what you want:

cp foo.txt foo.1

while [[ -s foo.1 ]]
do
    sed '$d' foo.1 >foo.2     # chop last line
    mv foo.2 foo.1
    if ! spam_test_command foo.1                             
    then
        break
    fi
done

It creates a temporary file that is one line less than the time before and invokes some magic spam command on the file. Assuming that if the command fails it means the file is spam, it will exit leaving the last tested email in foo.1.

If this isn't quite the situation, then post how you are testing for spam and someone will probably offer up a suggestion based on that.

1 Like

Thanks. You have given me a rough idea of how to script it from here. :slight_smile: