Help with File processing - Adding predefined text to particular record based on condition

I am generating a output:

Name Count_1 Count_2
abc       12       12
def        15      14
ghi        16       16
jkl         18       18
mno      7          5

I am sending the output in html email, I want to add the code:
<font color="red"> NAME COLUMN record </font>
for the Name column records when the Count_1 & Count_2 are different. If they are equal no need to add the HTML tags. ( just to differentiate from others)

For eg:
In the above example, I have to add the tag to def and mno records.

awk is meant for stuff like this. one-liner:

awk 'NR==1 { print } NR>1 { if($2 != $3) printf("<font color="red">%s</font>\n", $0); else print; }' < infile

with more explanation:

awk '
# If we're on the first line, just print it unmodified.
NR==1 { print }
# If we're on other lines:
 NR>1 {
        # If the second record doesn't match the third, print the entire line($0) surrounded by tags.
        if($2 != $3) printf("<font color="red">%s</font>\n", $0);
        else print;        # Otherwise, print the entire line unmodified.
}' < infile
1 Like
#!/usr/bin/ksh
while read mName mCnt1 mCnt2; do
  if [[ "${mCnt1}" != "${mCnt2}" ]]; then
    echo '<font color="red">'${mName}'</font>' ${mCnt1} ${mCnt2}
  else
    echo ${mName} ${mCnt1} ${mCnt2}
  fi
done < Inp_File
1 Like

It worked... but it was making $2 and $3 also red.

After processing it was appending as:

<font color=red> def         15      14</font>

What do you want, then? Your <font color="red"> NAME COLUMN record </font> seemed to imply you wanted all three columns red...

---------- Post updated at 10:20 AM ---------- Previous update was at 10:18 AM ----------

If just the first column, then:

awk 'NR==1 { print } NR>1 { if($2 != $3) $1 = "<font color="red">" $1 "</font>"; print; }' < infile

The same I need in $2>$3 condition, so I put :

awk 'NR==1 { print } NR>1 { if($2 -gt $3) printf("<font color="red">%s</font>\n", $0); else print; }' < infile

It is not working... All the records are effected for this... but there are only some which satisfy this condition.

awk doesn't use -gt.

awk 'NR==1 { print } NR>1 { if($2 > $3) printf("<font color="red">%s</font>\n", $0); else print; }' < infile

Error:
Syntax Error The source line is 1.
The error context is
NR==1 { print } NR>1 { if($2 >>> [ <<<
awk: 0602-502 The statement cannot be correctly parsed. The source line is 1.
awk: 0602-502 The statement cannot be correctly parsed. The source line is 1.
awk: 0602-541 There are 2 missing ] characters.

But worked by replacing just -gt with >

Thanks!

That was a typo I made in highlighting the change to red. You saw it during the 13 seconds before I fixed it :stuck_out_tongue:

Glad it works.