Replace character in odd or even lines

Hello,

I'm here again asking for your precious help.

I'm writing some code to convert csv files to html.
I want to highlight header and also I want to have rows with alternate colors.

So far this is my work

###Let's format first line only with some color
   cat $fileIN".tmp1" | sed '1 s";"</td><td bgcolor='#AAFF00'>"g' > $fileIN".tmp2"
   awk 'NR==1 {print "<tr><td bgcolor='#AAFF00'>"$0"</td></tr>"} !(NR==1) {print $0}' $fileIN".tmp2" > $fileIN".tmp1"

   ### Add <tr><td> at the beginning of each record and </td></tr> at the end of each record with some color
   awk 'NR==1 {print $0} NR%2&&NR>1 {print "<tr><td bgcolor='#E0E0E0'>"$0"</td></tr>"} !(NR%2)&&NR>1 {print $0}' $fileIN".tmp1" > 
$fileIN".tmp2"
   awk 'NR==1 {print $0} !(NR%2)&&NR>1 {print "<tr><td bgcolor='#A0A0A0'>"$0"</td></tr>"} (NR%2)&&NR>1 {print $0}' $fileIN".tmp2" 
> $fileIN".tmp1"

   ###Let's format each odd  line but first with some color
   awk 'NR%2 {sub(/;/, "</td><td bgcolor='#E0E0E0'>")}{print}' $fileIN".tmp1" > $fileIN".tmp2"

   ###Let's format each even line but first with some color
   awk '!(NR%2) {sub(/;/, "</td><td bgcolor='#A0A0A0'>")}{print}' $fileIN".tmp2" > $fileIN".tmp3"

### Now format TOP and BOTTOM of file
echo $HEAD > $fileIN".tmp2"; cat $fileIN".tmp3" >> $fileIN".tmp2"; 
echo $TAIL >> $fileIN".tmp2";

Input

apples;pears;bananas;mango
21;4;5;0
6;86;1;1
7;9;10;3
0;43;9;10
11;26;19;10
4;7;1;2

Desired Output

<html><title>Untitled Document</title><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body><table border="0" cellspacing="0" cellpadding="0">
<tr><td bgcolor=#AAFF00>apples</td><td bgcolor=#AAFF00>pears</td><td bgcolor=#AAFF00>bananas</td><td bgcolor=#AAFF00>mango</td></tr>
<tr><td bgcolor=#A0A0A0>21</td><td bgcolor=#A0A0A0>4</td><td bgcolor=#A0A0A0>5</td><td bgcolor=#A0A0A0>0</td></tr>
<tr><td bgcolor=#E0E0E0>6</td><td bgcolor=#E0E0E0>86</td><td bgcolor=#E0E0E0>1</td><td bgcolor=#E0E0E0>1</td></tr>
<tr><td bgcolor=#A0A0A0>7</td><td bgcolor=#A0A0A0>9</td><td bgcolor=#A0A0A0>10</td><td bgcolor=#A0A0A0>3</td></tr>
<tr><td bgcolor=#E0E0E0>0</td><td bgcolor=#E0E0E0>43</td><td bgcolor=#E0E0E0>9</td><td bgcolor=#E0E0E0>10</td></tr>
<tr><td bgcolor=#A0A0A0>11</td><td bgcolor=#A0A0A0>26</td><td bgcolor=#A0A0A0>19</td><td bgcolor=#A0A0A0>10</td></tr>
<tr><td bgcolor=#E0E0E0>4</td><td bgcolor=#E0E0E0>7</td><td bgcolor=#E0E0E0>1</td><td bgcolor=#E0E0E0>2</td></tr>
</table></body></html>

My problem si that only first occurrence of ";" in each record is changed

<tr><td bgcolor=#A0A0A0>21</td><td bgcolor=#A0A0A0>4;5;0</td></tr>
<tr><td bgcolor=#E0E0E0>6</td><td bgcolor=#E0E0E0>86;1;1</td></tr>

There is someting wrong in my awk command

   ###Let's format each odd  line but first with some color
   awk 'NR%2 {sub(/;/, "</td><td bgcolor='#E0E0E0'>")}{print}' $fileIN".tmp1" > $fileIN".tmp2"

   ###Let's format each even line but first with some color
   awk '!(NR%2) {sub(/;/, "</td><td bgcolor='#A0A0A0'>")}{print}' $fileIN".tmp2" > $fileIN".tmp3"

Thanks a lot for your help.

Hint: gsub
sub() awk function replaces the first occurrences of the string.
gsub() awk function replaces all the occurrences of the string.

Cheers!
Ranga

Holy cow ! How can I be so .......:o

Thanks a lot !

to simplify the even/odd awk's into 1

awk -v q="'" 'gsub(/;/, "</td><td bgcolor=" q ((NR%2)?"#E0E0E0" ? "#A0A0A0" ) q ">")' $fileIN".tmp1" > ${fileIN}_evenOdd

Why don't you do the entire thing in one single awk script?

Because my awk knowledge is limited :slight_smile:
Complex awk commands are nice but more difficult to understand and modify by other people working with me.

---------- Post updated at 10:01 AM ---------- Previous update was at 09:59 AM ----------

Thanks, that's good.
Can you please explain what the q is ?

doing man awk yields:

       -v var=val
              Assign  the  value  val  to  the  variable var, before execution of the program begins.  Such variable values are
              available to the BEGIN rule of an AWK program.

I once wrote a ksh-to-HTML-script for documentation purposes. The main generation part was in awk and it might help you as a starting point for your own script. Mostly it translates special characters into their HTML equivalents and creates the alternating stripes you want:

awk 'BEGIN {
          iLineNr=0;
          iOutIdx=1;
          lColor=0;
     }
     {
          chOutStr="";
          iOutIdx=1;
          iInLen=length( $0 );
          chBuffer="";

          for( iOutIdx=1; iOutIdx <= iInLen; iOutIdx++ ) {
               chBuffer=substr( $0, iOutIdx, 1 );
               if( chBuffer == " " ) {
                    chOutStr=chOutStr "�";
               } else if( chBuffer == "\t" ) {
                    chOutStr=sprintf("%s%s", chOutStr, substr("��������", 1, 6*(8-(iOutIdx%8)) ) );
               } else if( chBuffer == "\x27" ) {
                    chOutStr=chOutStr "�";
               } else if( chBuffer == "<" ) {
                    chOutStr=chOutStr "<";
               } else if( chBuffer == ">" ) {
                    chOutStr=chOutStr ">";
               } else if( chBuffer == "&" ) {
                    chOutStr=chOutStr "&";
               } else if( chBuffer == "�" ) {
                    chOutStr=chOutStr "�";
               } else if( chBuffer == "�" ) {
                    chOutStr=chOutStr "�";
               } else if( chBuffer == "�" ) {
                    chOutStr=chOutStr "�";
               } else if( chBuffer == "�" ) {
                    chOutStr=chOutStr "�";
               } else if( chBuffer == "�" ) {
                    chOutStr=chOutStr "�";
               } else if( chBuffer == "�" ) {
                    chOutStr=chOutStr "�";
               } else if( chBuffer == "�" ) {
                    chOutStr=chOutStr "�";
               } else
                    chOutStr=chOutStr chBuffer;
          }
          if( chOutStr == "" )
               chOutStr = "�";
          iLineNr++;
          printf( "                    <tr");
          if( lColor == 1 ) {
               printf( " bgcolor=#BFBFBF" );
               lColor=0
          } else {
               printf( " bgcolor=#B0B0B0" );
               lColor=1
          }
          printf( ">\n" );
          printf( "                         <td align=right>" );
          if( iLineNr % 30 == 0 )
               printf( "<a href=#top>%d</a>", iLineNr );
          else
               printf( "%d", iLineNr );

          printf( "</td>\n" );
          printf( "                         <td>�</td>\n");
          printf( "                         <td><font size=-1><code>%s</code></font></td>\n", chOutStr);
          printf( "                    </tr>\n");
     }' file

I hope this helps.

bakunin

1 Like