AWK script to verify line number with field


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"
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
everytime it will check line number -4 and 2nd field ( here its
-10) > 0 or not , if its > 0 then change the background color as red.
everytime it will check line number -5 and 2nd field ( here its
-csr) is a not null field or not. If its a notnull field then change the background color as red.
Please suggest.
Hi Cyrus I think now it's look okay. Can u please confirm
– John
3 hours ago
You can access the 2nd field with
$2
and use it in conditions. AWK supports some simple numeric and string operations with a syntax similar to C. E.g. if ($2 == 10)
checks whether 2nd field contains 10
.– Scheff
2 hours ago
$2
if ($2 == 10)
10
1 Answer
1
What do you think about this way? :
awk -v RS="[|n]" -v ORS="" '
BEGINprint "<HTML><TABLE border="1"><TH>Expected_code</TH><TH>Expected_Values</TH>n"
NR % 2print "<TR><TD>" $0 "</TD>"
!(NR%2)
ENDprint "</TABLE></BODY></HTML>"' roque.txt > outputfile;
With this way I just tried to avoid for
sentence.
for
Thanks Gioconda for your suggestion. As per my question I need to check line number 13 and 2nd field > 0 and line number 14 is not null or not by below if clause condition, but its not giving correct result.if((NR == 13 && $0 > 0) || (NR == 14 && $0 != null)
– John
1 hour ago
The first condition needs to be in
NR % 2
and the second in !(NR%2)
– Gioconda
40 mins ago
NR % 2
!(NR%2)
Yeah I did the same thing.. However everytime only line number 8 with 2nd field is getting red if its > 0.. Its not checking for line number 13 and 14. Could u please rewrite your code.
– John
37 mins ago
Line number 14, in second field too?
– Gioconda
6 mins ago
I just update the code, you must change your question with that new constraints.
– Gioconda
2 mins ago
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Prefix your code/data with four white spaces. Please take a look at editing-help.
– Cyrus
4 hours ago