Merge cells in all rows of a HTML table dynamically.


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Merge cells in all rows of a HTML table dynamically.
# 1  
Old 01-17-2018
Merge cells in all rows of a HTML table dynamically.

Hello All,

I have visited many pages in Unix.com and could find out one solution for merging the HTML cells in the 1st row.

(Unable to post the complete URL as I should not as per website rules).

But, however I try, I couldn't achieve this merging to happen for all other rows of HTML table.

Could someone help me in this regard please....

Awaiting your response!!

---------- Post updated at 07:29 PM ---------- Previous update was at 07:27 PM ----------

Code:
awk '
        BEGIN {
		FS=","
                print "To: to@email.com"
                print "MIME-Version: 1.0"
                print "Content-Type: text/html"
                print "Subject: Email Subject"
                print "<html><body>"
                print "<table border=1 cellspacing=2 cellpadding=2>"
        }
        NR == FNR {
                if ( $0 !~ /^#/ && $0 !~ /^S/ )
                        C[$1]++
                next
        }
        !/^#/ && /^S/ {
                print "<tr>"
                for ( i = 1; i <= NF; i++ )
                        print "<td><b>" $i "</b></td>"
                print "</tr>"
        }
        !/^#/ && !/^S/ {
                print "<tr>"
                for ( i = 1; i <= NF; i++ )
                {
                        if ( i == 1 && !( $i in R ) )
                        {
                                print "<td rowspan=" C[$i] ">" $i "</td>"
                                R[$i]
                        }
                        if ( i > 1 )
                                 print "<td>" $i "</td>"
                }
                print "</tr>"
        }
        END {
                print "</table></body></html>"
        }
' inputfile inputfile | /usr/sbin/sendmail -t

This is the sample code which i have tried...
# 2  
Old 01-18-2018
Quote:
Originally Posted by Mounika
(Unable to post the complete URL as I should not as per website rules).
Just write link as text. This is just a security measure against spammers and we (the moderators) will change it to a link if it is a legit reference. You will be allowed to post links yourself once you cross the threshold of a certain post count.

Quote:
Originally Posted by Mounika
But, however I try, I couldn't achieve this merging to happen for all other rows of HTML table.
To be honest i haven't completely understood what you want to do and what exactly the script you showed doesn't do. Please describe (as exactly as possible) what you want to do and ideally provide a meaningful example of some input and what the result you expect it to be transformed into should be.

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
# 3  
Old 01-18-2018
Actually, I have a comma delimited .csv file with 7 columns. Rows can be any.

Using the below code, I framed the HTML part of the .csv file and am able to send it to the mail.

Code:
awk 'BEGIN{
FS=","
print  "MIME-Version: 1.0"
print  "Content-Type: text/html"
print  "Content-Disposition: inline"
print  "<HTML><BODY>""<TABLE border="1"><TH>Header 1</TH><TH>Header 2</TH><TH>Header 3</TH><TH>Header 4</TH><TH>Header 5</TH><TH>Header 6</TH><TH>Header 7</TH>" 
}
{
printf "<TR>"
for(i=1;i<=NF;i++)
{
printf "<TD>%s</TD>", $i
}
print "</TR>"
 }
END{
print "</TABLE></BODY></HTML>"
 }
' inputfile.csv > inputfile.html

Now, when I checked the mail, some of the columns for adjacent rows in the output has equal data.

Example:

My output is like -

Code:
12345,XYZ,123,PPP,01,AAAAAAAAAA,DEFGDEFG
12345,XYZ,234,ABC,02,AAAAAAAAAA,DEFGDEFG
12345,XYZ,567,ABC,05,AAAAAAAAAA,HHHHHHH
45167,XYZ,809,ABC,09,BBBBBBBBBB,HHHHHHH
45167,XYZ,222,ABC,04,BBBBBBBBBB,DEFGDEFG

With this said my sample output, I need to merge all the adjacent rows which has equal values as we do in the excel sheet before sending the data in the mail so that it looks a bit tidy.

With the code which I have pasted with my question, I was able to merge the equal values of adjacent rows in the first column alone. But, its not merging (or combining) the equal values in other rows and columns.

Where am I missing that part and is there any way I can achieve this.

I hope now my problem is clear.

Moderator's Comments:
Mod Comment edit by bakunin: please use CODE-tags for data too. Thank you.

Last edited by bakunin; 01-18-2018 at 01:39 AM..
# 4  
Old 01-18-2018
Quote:
Originally Posted by Mounika
some of the columns for adjacent rows in the output has equal data.

Code:
12345,XYZ,123,PPP,01,AAAAAAAAAA,DEFGDEFG
12345,XYZ,234,ABC,02,AAAAAAAAAA,DEFGDEFG
12345,XYZ,567,ABC,05,AAAAAAAAAA,HHHHHHH
45167,XYZ,809,ABC,09,BBBBBBBBBB,HHHHHHH
45167,XYZ,222,ABC,04,BBBBBBBBBB,DEFGDEFG

With this said my sample output, I need to merge all the adjacent rows which has equal values as we do in the excel sheet before sending the data in the mail so that it looks a bit tidy.
OK, understood, but what is considered "equal values"? Is (in the above example) line 1 equal to line 2 because fields 6 and 7 match? Or because fields 1 and 2 match and therefore line 3 is also considered equal? Are the lines in your example all considered different because they all differ somewhere and only completely duplicated lines are "equal" as per your definition?

Please keep in mind that we do not know what you do. What is probably obvious to you isn't obvious at all for us because we lack all the "implied knowledge" you have about your work.

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
# 5  
Old 01-18-2018
Thank you for your response!!

Considering the sample output below:
Code:
12345,XYZ,123,PPP,01,AAAAAAAAAA,DEFGDEFG
12345,XYZ,234,ABC,02,AAAAAAAAAA,DEFGDEFG
12345,XYZ,567,ABC,05,AAAAAAAAAA,HHHHHHH
45167,XYZ,809,ABC,09,BBBBBBBBBB,HHHHHHH
45167,XYZ,222,ABC,04,BBBBBBBBBB,DEFGDEFG

In the first column we have 12345 value present in first 3 rows. So for the first 3 rows, first column should be a merged value showing single merged value 12345 instead of 3. In other words equivalent to rowspan in HTML or Merge in Excel sheet.

considering last 2 rows, instead of it displaying 45167 value twice it should only display it once with 4th and 5th rows first column as a merged cell.

If you consider second column, all the values in that column for all rows is XYZ. So, instead of showing the same value XYZ 5 times, it should show XYZ as a single value with all 5 cells as a single merged cell.

Similarly for others.
Code:
12345	XYZ	123	PPP	1	AAAAAAAAAA	DEFGDEFG
12345	XYZ	234	ABC	2	AAAAAAAAAA	DEFGDEFG
12345	XYZ	567	ABC	5	AAAAAAAAAA	HHHHHHH
45167	XYZ	809	ABC	9	BBBBBBBBBB	HHHHHHH
45167	XYZ	222	ABC	4	BBBBBBBBBB	DEFGDEFG

Say this is the initial HTML format I got in mail

The same should be converted to below.

Code:
12345	XYZ	123	PPP	1	AAAAAAAAAA	DEFGDEFG
		234	ABC	2		
		567		5		HHHHHHH
45167		809		9	BBBBBBBBBB	
		222		4		DEFGDEFG

I hope this gives a clear picture.

Last edited by Mounika; 01-18-2018 at 01:56 AM.. Reason: Provided modified output which is needed.
# 6  
Old 01-18-2018
Would
Code:
awk -F, '{for (i=1; i<=NF;i++) if ($i == T[i]) $i = ""; else T[i] = $i} 1' OFS=, file
12345,XYZ,123,PPP,01,AAAAAAAAAA,DEFGDEFG
,,234,ABC,02,,
,,567,,05,,HHHHHHH
45167,,809,,09,BBBBBBBBBB,
,,222,,04,,DEFGDEFG

come close to what you need? Replace the OFS with "\t" to get to your tabulated output.
This User Gave Thanks to RudiC For This Post:
# 7  
Old 01-18-2018
Quote:
Originally Posted by RudiC
Would
Code:
awk -F, '{for (i=1; i<=NF;i++) if ($i == T[i]) $i = ""; else T[i] = $i} 1' OFS=, file
12345,XYZ,123,PPP,01,AAAAAAAAAA,DEFGDEFG
,,234,ABC,02,,
,,567,,05,,HHHHHHH
45167,,809,,09,BBBBBBBBBB,
,,222,,04,,DEFGDEFG

come close to what you need? Replace the OFS with "\t" to get to your tabulated output.
Thank you for the response!!

But the thing here is, when I convert the output to HTML format it will show me blank cells instead of merged cells.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Merge Multiple html files into one

Hi all I have written some code to write my output in html. As i have multiple servers, need to generate single html file. but my code is generating html file for each server. I have merged the files using below code. cat /home/*_FinalData.html > /home/MergedFinalData.html But how to... (1 Reply)
Discussion started by: Snehasish
1 Replies

2. UNIX for Beginners Questions & Answers

Remove duplicates in a dataframe (table) keeping all the different cells of just one of the columns

Hello all, I need to filter a dataframe composed of several columns of data to remove the duplicates according to one of the columns. I did it with pandas. In the main time, I need that the last column that contains all different data ( not redundant) is conserved in the output like this: A ... (5 Replies)
Discussion started by: pedro88
5 Replies

3. Shell Programming and Scripting

Parameterizing to dynamically generate the extract file from Oracle table using Shell Script

I have below 2 requirements for parameterize the generate the extract file from Oracle table using Shell Script. Could you please help me by modifying the script and show me how to execute it. First Requirement: I have a requirement where I need to parameterize to generate one... (0 Replies)
Discussion started by: hareshvikram
0 Replies

4. UNIX for Beginners Questions & Answers

Putting query result dynamically to one cell of table from shell

I have to send a data in mail table format.only one cell need to get dynamically from query. my code is like (echo '<table boarder="1"> echo '<tr><td>stock</td><td>/path</td><td>.........</td></tr>' echo '</table> )sendmail.. in ......... I am trying to get query result.By putting query... (2 Replies)
Discussion started by: meera_123
2 Replies

5. Programming

Perl script to merge cells in column1 which has same strings, for all sheets in a excel workbook

Perl script to merge cells ---------- Post updated at 12:59 AM ---------- Previous update was at 12:54 AM ---------- I am using below code to read files from a dir and print to excel. open(my $in, '<', $file) or die "Could not open file: $!"; my $rowCount = 0; my $colCount = 0;... (11 Replies)
Discussion started by: Jack_Bruce
11 Replies

6. Shell Programming and Scripting

extract complex data from html table rows

I have bash, awk, and sed available on my portable device. I need to extract 10 fields from each table row from a web page that looks like this: </tr> <tr> <td>28 Apr</td> <td><a... (6 Replies)
Discussion started by: rickgtx
6 Replies

7. Shell Programming and Scripting

Merge two cells in excel via UNIX?

Hi UNIX Gods! Is it possible to merge two cells in .csv file using unix commands? Imagine that this is my present csv file opened via excel: Gate Reports| | fatal alerts | 200 | is is possible to make it look like this using unix? Gate Reports | fatal... (1 Reply)
Discussion started by: 4dirk1
1 Replies

8. Programming

How do I change html style dynamically

I've got the following form element in a template driven web page... <INPUT type="text" class="normal" id="LastName" name="LastName" value="{LastName}"> The stylesheet description is simply... input.normal { width: 250; } I want to change the background colour of the input box after the user... (0 Replies)
Discussion started by: JerryHone
0 Replies

9. Shell Programming and Scripting

How to merge rows into columns ????

Hi guz I want to merge multiple rows into a multiple columns based on the first column. The file has symbol // I want to break the symbool // and I nedd exactlynew column at that point the output will be like this please guyz help in this isssue!!!!! merging rows into columns ... (4 Replies)
Discussion started by: bogu0001
4 Replies

10. Shell Programming and Scripting

Deleting table cells in a script

I'd like to use sed or awk to do this but I'm weak on both along with RE. Looking for a way with sed or awk to count for the 7th table data within a table row and if the condition is met to delete "<td>and everything in between </td>". Since the table header start on a specific line each time, that... (15 Replies)
Discussion started by: phpfreak
15 Replies
Login or Register to Ask a Question