awk within awk

Hi I am trying to execute below code

awk -F, 'BEGIN{print "STARTING"}$5 ~ /Match/ { if(NF==0) print "No Match"; else
{awk -F, 'BEGIN{
print "To: abc@123.com"
print "MIME-Version: 1.0"
print "Content-Type: text/html"
print "<html><table>"
print "<tr><td>Hi All,</td></tr>"
print "<tr><td>Below is the data</td></tr>"
print "<tr><td></td></tr>"
print "</table></html>"
print "<html><table border=1>"
print "<th>Data1</th><th>Data1</th><th>Data1</th><th>Data1</th><th>Data1e</th>"
}$5 ~ /Match/ {print "<tr>";for(i=1;i<=NF;i++){print "<td>" $i"</td>"};print "</tr>"}
END{
print "</table></html>"
print "<table><html>"
print "<tr><td>Thanks,</td></tr>"
print "<tr><td>All the best</td></tr>"
print "</table></html>"}' file1 | sendmail  -t}
}END{print "ENd of script"}' file1

But it gives error

awk: syntax error near line 2
awk: illegal statement near line 2
bash: print: command not found
bash: print: command not found
bash: print: command not found
bash: print: command not found
bash: print: command not found
bash: print: command not found
bash: print: command not found
bash: print: command not found
bash: print: command not found
bash: print: command not found
bash: ./testing.sh: line 14: syntax error near unexpected token `('
bash: ./testing.sh: line 14: `}$5 ~ /Match/ {print "<tr>";for(i=1;i<=NF;i++){print "<td>" $i"</td>"};print "</tr>"} '

Please help

Yes you cannot do that. awk is not an awk command

That's not doing anything for you. That `if' will never evaluate to true. A record cannot have at least 5 fields to meet the $5 ~ /Match/ expression and have 0 fields to evaluate true in the if(NF==0)

You are right Aia, i needed NR==0.
I modified the code

awk -F, 'BEGIN{
print "To: abc@123.com"
print "MIME-Version: 1.0"
print "Content-Type: text/html"
print "<html><table>"
print "<tr><td>Hi All,</td></tr>"
print "<tr><td>below is the data</td></tr>"
print "<tr><td></td></tr>"
print "</table></html>"
print "<html><table border=1>"
print "<th>Data1</th><th>Data1</th><th>Data1</th><th>Data1</th><th>Data1</th>"
}$5 ~ /Match/ { if (NR==0) print "No data"; else print "<tr>";for(i=1;i<=NF;i++){print "<td>" $i"</td>"};print "</tr>"}
END{
print "</table></html>"
print "<table><html>"
print "<tr><td>Thanks,</td></tr>"
print "<tr><td>All the best</td></tr>"
print "</table></html>"}' file | sendmail  -t

Basically What I want to do is, only if data is there send the mail, else do not send the mail. How can I achieve this?

No. NR==0 will NEVER be true. Please show us a sample of your input file and tell us in English what you are trying to extract from that file into the mail message you're trying to send.

The condition will not be effective inside awk since data is piped to sendmail regardless.

Maybe

#!/bin/bash

content=$(awk -F, '$5 ~ /Match/' file.txt)

if [[ -n "$content" ]]; then
    echo "$content" | \
    awk -F, '
        BEGIN{
            print "To: abc@123.com"
            print "MIME-Version: 1.0"
            print "Content-Type: text/html"
            print "<html><table>"
            print "<tr><td>Hi All,</td></tr>"
            print "<tr><td>below is the data</td></tr>"
            print "<tr><td></td></tr>"
            print "</table></html>"
            print "<html><table border=1>"
            print "<th>Data1</th><th>Data1</th><th>Data1</th><th>Data1</th><th>Data1</th>"
        } 
        { print "<tr>";for(i=1;i<=NF;i++){print "<td>" $i"</td>"};print "</tr>"}
        END{
            print "</table></html>"
            print "<table><html>"
            print "<tr><td>Thanks,</td></tr>"
            print "<tr><td>All the best</td></tr>"
            print "</table></html>"
        }' | sendmail -t 
fi

Untested

Raw file

Rent,City,Car,Amount,Name
Pending,Chicago,BMW,4k,jack
Not pending,New York,Mercedes,Mary

So daily I would filter the file manually where the column rent would have records which are Pending so file I would have

Rent,City,Car,Amount,Name
Pending,Chicago,BMW,4K,Jack

Now I need to check Daily on this file with column Name and send mails to those guys whose name appears

So what is happening with above code is, even if the rent is not pendingn, but since email is mentioned in awk command it sends blank mails to Non pending Users

I am sure even the manual efforts can be done through awk, but am not too sure

I don't see the text Match anywhere in your input file, so the part of the awk script that is generating your table will never do anything. The only time this code:

$5 ~ /Match/ {
        if (NR==0) print "No data"
        else print "<tr>"
        for(i=1;i<=NF;i++)
                print "<td>" $i"</td>"
        print "</tr>"
}

will do anything is if the 5th field on the line (i.e., the Name) contains the string Match .

There is absolutely no check in your code looking for the string Pending in the 1st comma separated field.

Your script is sending all messages to abc@123.com not to the person whose rent is pending. Your file does not contain an email address for anyone. How is your script supposed to generate an email address from the name in the 4th or 5th field in your input file?

Why don't all lines in your input file have 5 fields? Why is the name in field 4 instead of field 5 for lines that have a first field containing Not pending ?

The Mathcing code was just a general statement

$5 ~ /Match/ {
        if (NR==0) print "No data"
        else print "<tr>"
        for(i=1;i<=NF;i++)
                print "<td>" $i"</td>"
        print "</tr>"
}

So say I want to check if Jack has any rent pending and if Yes then, send an email

$5 ~ /Jack/ {
        if (NR==0) print "No data"
        else print "<tr>"
        for(i=1;i<=NF;i++)
                print "<td>" $i"</td>"
        print "</tr>"

His email address would be defined in the BEGIN section of awk

awk -F, '
        BEGIN{
            print "To: Jack@123.com"
}

So in reality, I have 10 users, and each users email address has been entered.

So I have 10 awk BEGIN, END Scripts each holdin the 10 users email addresses.

Requirement, only send email to the users who has rent pending

That doesn't sound too effective - 10 scripts to hold 10 different users' email addresses. Why not create a "config" file contaning lines of "user / address" pairs? Read that file, and afterwards your data file, sending out mails to users with pending rents.

Hi RudiC,

Not sure how to do that :confused::confused::confused:

As I said before, NR is the number of records read so far from all input files being processed by this invocation of awk . It will NEVER be 0. And there is absolutely nothing in this code that checks to see whether or not a payment is pending.

Furhtermore, $5 ~ /Jack/ will match Jackie and Jackson as well as Jack.

And each user will get email whether or not they have any pending transactions. I strongly agree with RudiC; you need to take a step back and look at the logic you want to use to solve your problem.

Your current approach is to create 10 different scripts to send email to 10 different users and then figure out if the user has any pending transactions. That does not seem to do what you say you want.

You haven't said anything at all about whether it is possible for one user to have more than one pending transaction. And, if they do, whether they should get one email with all of their pending transactions or one email for each pending transaction.

Is each user's email address just name@abc.com where name is the contents of the last (not necessarily the 5th) field in your input file?

Do you really want the headings:

Data1   Data1   Data1   Data1   Data1e

in the email you send to your users? (The last heading is Data1e in your first script and another Data1 in others.) Are the headings constants, or should they be pulled from the 1st line of your input file? Do you really want to address each user as All ? Why not user their name?

If each user is only getting a list of their transactions, why does the email to that user need to include a column in the table for that user's name? If each user is only getting a list of their PENDING transactions, why does the table need to include a column that will always say Pending ? If more than one transaction can be sent to a single user, should there be a Total line? Is there a due date associated with any of these transactions?

Thank you Don for looking into this, but I believe Aia has given the correct solution

#!/bin/bash

content=$(awk -F, '$5 ~ /Match/' file.txt)

if [[ -n "$content" ]]; then
    echo "$content" | \
    awk -F, '
        BEGIN{
            print "To: abc@123.com"
            print "MIME-Version: 1.0"
            print "Content-Type: text/html"
            print "<html><table>"
            print "<tr><td>Hi All,</td></tr>"
            print "<tr><td>below is the data</td></tr>"
            print "<tr><td></td></tr>"
            print "</table></html>"
            print "<html><table border=1>"
            print "<th>Data1</th><th>Data1</th><th>Data1</th><th>Data1</th><th>Data1</th>"
        } 
        { print "<tr>";for(i=1;i<=NF;i++){print "<td>" $i"</td>"};print "</tr>"}
        END{
            print "</table></html>"
            print "<table><html>"
            print "<tr><td>Thanks,</td></tr>"
            print "<tr><td>All the best</td></tr>"
            print "</table></html>"
        }' | sendmail -t 
fi

Thanks Aia, you are a GENIOUS