Replace character in odd or even lines


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Replace character in odd or even lines
# 1  
Old 04-11-2016
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
Code:
###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
Code:
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
Code:
<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
Code:
<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
Code:
   ###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.
# 2  
Old 04-11-2016
gsub

Code:
Hint: gsub

Code:
sub() awk function replaces the first occurrences of the string.

Code:
gsub() awk function replaces all the occurrences of the string.

Cheers!
Ranga

Quote:
Originally Posted by emare
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
Code:
###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
Code:
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
Code:
<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
Code:
<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
Code:
   ###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.
# 3  
Old 04-11-2016
Holy cow ! How can I be so .......Smilie

Thanks a lot !
# 4  
Old 04-11-2016
to simplify the even/odd awk's into 1
Code:
awk -v q="'" 'gsub(/;/, "</td><td bgcolor=" q ((NR%2)?"#E0E0E0" ? "#A0A0A0" ) q ">")' $fileIN".tmp1" > ${fileIN}_evenOdd

# 5  
Old 04-11-2016
Why don't you do the entire thing in one single awk script?
# 6  
Old 04-12-2016
Quote:
Originally Posted by RudiC
Why don't you do the entire thing in one single awk script?
Because my awk knowledge is limited Smilie
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 ----------

Quote:
Originally Posted by vgersh99
to simplify the even/odd awk's into 1
Code:
awk -v q="'" 'gsub(/;/, "</td><td bgcolor=" q ((NR%2)?"#E0E0E0" ? "#A0A0A0" ) q ">")' $fileIN".tmp1" > ${fileIN}_evenOdd

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

Last edited by emare; 04-12-2016 at 09:53 AM..
# 7  
Old 04-12-2016
doing man awk yields:
Code:
       -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.

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search a character and replace it with multiple lines

This is for AIX 6.1, I've a flat file and the format is like this DECLARE some statements; BEGIN some statements; END; I've to search BEGIN and replace it with the following 4 lines BEGIN For x in 1..1 LOOP BEGIN Similarly I've to search END and replace it with the... (7 Replies)
Discussion started by: Mukul Sharma
7 Replies

2. Shell Programming and Scripting

Sum product of even/odd lines

Hi, I have a text file like this 6.0000E-02 0.00000E+00 0.0000 0.00000E+00 0.0000 7.0000E-02 5.00000E-10 1.0000 5.00000E-10 1.0000 8.0000E-02 3.00000E-09 0.4082 3.00000E-09 0.4082 9.0000E-02 3.50000E-09 0.3780 3.50000E-09 0.3780 1.0000E-01 1.00000E-09... (2 Replies)
Discussion started by: f_o_555
2 Replies

3. Shell Programming and Scripting

Insert at the beginning of odd lines

Hello people, I am trying with sed to insert some text at the beginning of each odd line of a file but no luck. Can you please help. Awk is also suitable but I am not very familiar with it. Thank you in advance for any help. (7 Replies)
Discussion started by: drbiloukos
7 Replies

4. Emergency UNIX and Linux Support

Replace nth position character of all the lines in file

I want to replace 150th character of all the lines in a file using sed or awk... searched the forums but didn't find exact answer (9 Replies)
Discussion started by: greenworld123
9 Replies

5. Shell Programming and Scripting

How to search for pattern in odd lines?

Hi friends, I am looking for sed command/script that would search for a given fixed pattern on odd lines and then if it matches, prints the matching pattern and the next line. For example, in the example below, i am looking for pattern 0 and 1011 on odd lines. ########## start of example file... (10 Replies)
Discussion started by: kaaliakahn
10 Replies

6. Shell Programming and Scripting

replace/delete odd occurance

how to delete/replace the the odd occurance of a pattern say newline character "\n" a,b,c,d e,f g, h, i, j, k, l m,n 1, 2, 3, 4, 5, 6 1, 3, 4, 5, 6, 7 the output should be a,b,c,de,f g, h, i, j, k, lm,n 1, 2, 3, 4,5, 6 1, 3, 4, 5, 6, 7 (4 Replies)
Discussion started by: ratheeshjulk
4 Replies

7. Shell Programming and Scripting

print ODD lines

i want to print ODD lines like first ,third then fifth and so on 234,567,ABC,KJL 234,565,ABD,KJL 234,568,ABE,KJL 234,560,ABF,KJL 234,563,ABG,KJL 234,562,ABH,KJL O/P will be like 234,567,ABC,KJL ----->first liine 234,568,ABE,KJL ----->third line 234,563,ABG,KJL ----->fifth line... (6 Replies)
Discussion started by: aaysa123
6 Replies

8. UNIX for Dummies Questions & Answers

Odd Control Character issue ^A

Sorry to bug you, but my sed is failing me, I have a file auto generated from abinitio, it has a string of chars ending with a line break, and then it has added a ^A character, I can remove this is vi by using the following %s/^A//g (where ^A is ctrl v and control A), however when I try to sed... (1 Reply)
Discussion started by: badg3r
1 Replies

9. UNIX for Dummies Questions & Answers

odd character (^M) from .sh when chagining to .ksh

// AIX 5.3 I am trying to use .sh after changing it to .ksh Obviously, it doesn't like the file extension change. I am seeing a lot of odd characters (^M) like below: Init_Part2 ()^M^M {^M^M AWTRACE "AW SET"^M^M set | grep -e CFG_ -e OUTDIR_ENV^M^M AWTRACE "AW SET"^M^M ^M^M if ;... (2 Replies)
Discussion started by: Daniel Gate
2 Replies

10. Shell Programming and Scripting

Replace a perticular character of all lines of a file

Hi all, I am new to UNIX, so sorry if my question seem stupid to u. well i want to replace the first character of first 30 lines of a file, only if the first character is h. and in anothe script i want to replace a particular string/character say hello/h of a file.Condition: It should... (1 Reply)
Discussion started by: abovais
1 Replies
Login or Register to Ask a Question