Getting data from a flat file based on condition

Nice approach corona :b: didn't come in my mind :wink:

I think we need some actual sample data. We're just assuming our code works with your data correctly but we don't actually know that.

Note that you have to set the FS to a comma:

awk -F, '{..}'
1 Like

Actual data is somwething like below:

 
Case 1:Mail should come
Report
,,,,,,,Report generating
Column 1,column 2,column 3,column 4,column 5,column 6,column 7
1,2,3,4,5,0,0
1,3,5,6,7,1,1
1,3,5,7,8,123,432
 
Case 2:Mail should not come
Report
,,,,,,,Report generating
Column 1,column 2,column 3,column 4,column 5,column 6,column 7
1,2,2,23,5,0,0
1,6,4,6,7,0,0
1,8,5,1,8,0,0
 
Case3: Mail should come
Report
,,,,,,,Report generating
Column 1,column 2,column 3,column 4,column 5,column 6,column 7
1,2,3,4,5,0,0
1,3,5,6,7,1,1
1,3,5,7,8,0,0

Works for me when I remember to set -F,

$ awk -F, '{ T += $6 + $7 } END { exit(!T) }' mail1 ; echo $?
0
$ awk -F, '{ T += $6 + $7 } END { exit(!T) }' mail2 ; echo $?
0
$ awk -F, '{ T += $6 + $7 } END { exit(!T) }' nomail1 ; echo $?
1
$

This is what it is writing when i am running in debug mode.
In this case both cloumns 6 and 7are 0 in all the rows.

 
+ awk -F, '{ T += $6 + $7 } END { exit(!T) }' /home/home01/file.csv
+ sendmail -t
+ awk -F, $'\nBEGIN{\n c=split("1,2,3,4,5,6,7", col)\n print "To: abc@gmail.com"\n print "Subject: REPORT"\n print "MIME-Version: 1.0"\n print "Content-Type: text/html"\n print "Content-Disposition: inline\\n"\n print "<HTML><TABLE border=1>"\n print "<TH>Column1</TH><TH>Column2</TH><TH>Column3</TH>"\n print "<TH>Column4</TH><TH>Column5</TH>"\n print "<TH>Column6</TH><TH>Column7</TH><TH>"\n}\nNR>4 {\n printf "<TR>"\n for(i=1;i<=c;i++) printf "<TD>%s</TD>", $col\n print "</TR>"\n}\nEND{\n  print "</TABLE></BODY></HTML>"\n} ' /home/home01/file.csv

It works fine here. Post your actual, real data, as an attachment if you can. We won't be able to tell why it's not working without it.

1 Like

Thanks a lot..It worked....
last question..if instead of 0 column value is either 0 or 0.00 then what should be the logic.
Please guide.

T += $6 or what have you instead of $6 + $7...

Sorry i didnt see your reply and edited my last post.
I want to check now 0.00 both in cloumns.
Please guide.

Isn't that what we were doing before?

Why don't you try and see what happens...

1 Like

No..initially column has 0 value.Now if i want to check 0.00 then this logic doesnt seem to working with decimal values.
Please guide.

works for me with gawk with no changes to the code. Please show a sample representative file.
What OS are you on?

Hi,

gawk not working.
Sample:

 
Case 1:Mail should come
Report
,,,,,,,Report generating
Column 1,column 2,column 3,column 4,column 5,column 6,column 7
1,2,3,4,5,0.00,0.00
1,3,5,6,7,0.01,0.01
1,3,5,7,8,0.00,0.00

Case 2:Mail should not come
Report
,,,,,,,Report generating
Column 1,column 2,column 3,column 4,column 5,column 6,column 7
1,2,2,23,5,0.00,0.00
1,6,4,6,7,0.00,0.00
1,8,5,1,8,0.00,0.00

Case3: Mail should come
Report
,,,,,,,Report generating
Column 1,column 2,column 3,column 4,column 5,column 6,column 7
1,2,3,4,5,0.00,0.00
1,3,5,6,7,0.01,0.01
1,3,5,7,8,0.00,0.00

I am using Linux GNU/Linux

In what way is it 'not working'?

Show exactly what you did, letter for letter, as well as the input data, not a mockup.

I'll let the OP elaborate, but I think the OP is confused about the '0.01' in the sample above....