John
John

Reputation: 23

AWK script to verify line number with field

I have below awk scripting which is to send email in a tabular format. The input to the awk script is a pipe delimited file.

awk 'BEGIN{ 
FS="|"
print  "<HTML>""<TABLE border="1"><TH>Expected_code</TH><TH>Expected_Values</TH>"
}

{
  printf "<TR>"
  for(i=1;i<=NF;i++)
  printf "<TD>%s</TD>", $i
  print "</TR>"
}

END{
print "</TABLE></BODY></HTML>"}
' query_results > file.html

Contents of the pipe delimited file:

ABC|500
XYZ|300
TET|200
SCT|10
ASA|csr

Looking at the above datasets I need to change the awk script in such a way that

  1. Every time it will check line number 4 and 2nd field ( here it's 10) > 0 or not , if its > 0 then change the background color as red.

  2. Every time it will check line number 5 and 2nd field ( here it's csr) is a not null field or not. If its a not null field then change the background color as red.

Upvotes: 0

Views: 117

Answers (1)

Joaquin
Joaquin

Reputation: 2091

What do you think about this way? :

awk -v RS="[|\n]" -v ORS="" '
BEGIN{print "<HTML><TABLE border=\"1\"><TH>Expected_code</TH><TH>Expected_Values</TH>\n"}
NR % 2{print "<TR><TD>" $0 "</TD>"}
!(NR%2){t="<TD>";if(( (NR == 8 || NR == 26) && $0 > 0) || ( (NR == 10 || NR == 28) && $0 != null) )t="<TD bgcolor=\"#FF0000\">"
print t $0 "</TD></TR>\n"}
END{print "</TABLE></BODY></HTML>"}'  query_results > outputfile;

With this way I just tried to avoid for sentence.

Upvotes: 1

Related Questions