Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

Help with data formatting

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 01-26-2013
karumudi7 karumudi7 is offline
Registered User
 
Join Date: Mar 2011
Last Activity: 26 April 2015, 11:09 PM EDT
Location: India
Posts: 119
Thanks: 23
Thanked 1 Time in 1 Post
IBM Help with data formatting

Hi,

I am generating the following output from my script.


Code:
Country,A,B,C,D,E,F
INDIA    ,3755019,774604,484749,329838,7333612,442031
CHINA       ,3716520,889197,530899,379754,6198475,355768
JAPAN       ,52038,30462,231224,147275,1272,498
USA,9494,1130,0,0,15303,451
UK,3680478,1085154,999262,645190,4453831,421336
CANADA      ,2,1,0,0,0,0
SINGAPORE         ,3548689,715416,1073298,733718,3520766,304011

I want to format this into html table code:


Code:
<table>
<tr><td>Country</td><td>A</td><td>B</td><td>C</td><td>D</td><td>E</td><td>F</td></tr>
<tr><td>INDIA    </td><td>3755019</td><td>774604</td><td>484749</td><td>329838</td><td>7333612</td><td>442031</td></tr>
<tr><td>CHINA       </td><td>3716520</td><td>889197</td><td>530899</td><td>379754</td><td>6198475</td><td>355768</td></tr>
<tr><td>JAPAN       </td><td>52038</td><td>30462</td><td>231224</td><td>147275</td><td>1272</td><td>498</td></tr>
<tr><td>USA</td><td>9494</td><td>1130</td><td>0</td><td>0</td><td>15303</td><td>451</td></tr>
<tr><td>UK</td><td>3680478</td><td>1085154</td><td>999262</td><td>645190</td><td>4453831</td><td>421336</td></tr>
<tr><td>CANADA      </td><td>2</td><td>1</td><td>0</td><td>0</td><td>0</td><td>0</td></tr>
<tr><td>SINGAPORE         </td><td>3548689</td><td>715416</td><td>1073298</td><td>733718</td><td>3520766</td><td>304011</td></tr>
</table>

Also Bold the first column (Country names) and first row (Column names).
But here I have to handle cases if the output from the initial script gives one more extra country.

I want to use this final output for UNIX mail command with HTML. So the body of the message will display a table for this.

Thanks.
Sponsored Links
    #2  
Old Unix and Linux 01-26-2013
RudiC RudiC is online now Forum Advisor  
Registered User
 
Join Date: Jul 2012
Last Activity: 28 May 2015, 1:45 PM EDT
Location: Aachen, Germany
Posts: 5,987
Thanks: 108
Thanked 1,605 Times in 1,507 Posts
Try this as a starting point. I'm not sure if the long OFS and ORS separators will work on every awk implementation, so your mileage may vary:
Code:
awk  'BEGIN  {printf  "<table>\n<tr><td>"; ORS="</td></tr>\n<tr><td>"}
      END    {ORS="\n";print "</table>"}
      NR==1  {for (i=1;i<=NF;i++) $i="<b>"$i"</b>"}
      NR>1   {$1="<b>"$1"</b>" } 
      1
     ' FS="," OFS="</td><td>"  file
<table>
<tr><td><b>Country</b></td><td><b>A</b></td><td><b>B</b></td><td><b>C</b></td><td><b>D</b></td><td><b>E</b></td><td><b>F</b></td></tr>
<tr><td><b>INDIA    </b></td><td>3755019</td><td>774604</td><td>484749</td><td>329838</td><td>7333612</td><td>442031</td></tr>
<tr><td><b>CHINA       </b></td><td>3716520</td><td>889197</td><td>530899</td><td>379754</td><td>6198475</td><td>355768</td></tr>
<tr><td><b>JAPAN       </b></td><td>52038</td><td>30462</td><td>231224</td><td>147275</td><td>1272</td><td>498</td></tr>
<tr><td><b>USA</b></td><td>9494</td><td>1130</td><td>0</td><td>0</td><td>15303</td><td>451</td></tr>
<tr><td><b>UK</b></td><td>3680478</td><td>1085154</td><td>999262</td><td>645190</td><td>4453831</td><td>421336</td></tr>
<tr><td><b>CANADA      </b></td><td>2</td><td>1</td><td>0</td><td>0</td><td>0</td><td>0</td></tr>
<tr><td><b>SINGAPORE         </b></td><td>3548689</td><td>715416</td><td>1073298</td><td>733718</td><td>3520766</td><td>304011</td></tr>
<tr><td></table>

Not easy to get rid of the last line's "<tr><td>" ... I'll work on it.

Corrected for the syntax error (closing }).

Last edited by RudiC; 01-26-2013 at 07:41 AM.. Reason: correction of typo in script
Sponsored Links
    #3  
Old Unix and Linux 01-26-2013
karumudi7 karumudi7 is offline
Registered User
 
Join Date: Mar 2011
Last Activity: 26 April 2015, 11:09 PM EDT
Location: India
Posts: 119
Thanks: 23
Thanked 1 Time in 1 Post
Quote:
Originally Posted by RudiC View Post
Try this as a starting point. I'm not sure if the long OFS and ORS separators will work on every awk implementation, so your mileage may vary:
Code:
awk 'BEGIN{printf  "<table>\n<tr><td>"; ORS="</td></tr>\n<tr><td>"} END{ORS="\n";print "</table>"}
     NR==1    {$0="<b>"$0"</b>"}
     NR>1     {$1="<b>"$1"</b>"}
     1
    ' FS="," OFS="</td><td>"  file
<table>
<tr><td><b>Country,A,B,C,D,E,F</b></td></tr>
<tr><td><b>INDIA    </b></td><td>3755019</td><td>774604</td><td>484749</td><td>329838</td><td>7333612</td><td>442031</td></tr>
<tr><td><b>CHINA       </b></td><td>3716520</td><td>889197</td><td>530899</td><td>379754</td><td>6198475</td><td>355768</td></tr>
<tr><td><b>JAPAN       </b></td><td>52038</td><td>30462</td><td>231224</td><td>147275</td><td>1272</td><td>498</td></tr>
<tr><td><b>USA</b></td><td>9494</td><td>1130</td><td>0</td><td>0</td><td>15303</td><td>451</td></tr>
<tr><td><b>UK</b></td><td>3680478</td><td>1085154</td><td>999262</td><td>645190</td><td>4453831</td><td>421336</td></tr>
<tr><td><b>CANADA      </b></td><td>2</td><td>1</td><td>0</td><td>0</td><td>0</td><td>0</td></tr>
<tr><td><b>SINGAPORE         </b></td><td>3548689</td><td>715416</td><td>1073298</td><td>733718</td><td>3520766</td><td>304011</td></tr>
<tr><td></table>

Not easy to get rid of the last line's "<tr><td>" ... I'll work on it.

First line of the output is getting comma separated.
    #4  
Old Unix and Linux 01-26-2013
RudiC RudiC is online now Forum Advisor  
Registered User
 
Join Date: Jul 2012
Last Activity: 28 May 2015, 1:45 PM EDT
Location: Aachen, Germany
Posts: 5,987
Thanks: 108
Thanked 1,605 Times in 1,507 Posts
Yes, already noticed and changed in post #2. Pls try again.

The easiest way to correct the last line is to append a small sed script:
Code:
awk ... | sed 's:^.*\(</table>\):\1:'

Sponsored Links
    #5  
Old Unix and Linux 01-26-2013
karumudi7 karumudi7 is offline
Registered User
 
Join Date: Mar 2011
Last Activity: 26 April 2015, 11:09 PM EDT
Location: India
Posts: 119
Thanks: 23
Thanked 1 Time in 1 Post

Code:
Syntax Error The source line is 6.
 The error context is
                     >>>   <<<
 awk: 0602-502 The statement cannot be correctly parsed. The source line is 6.
        awk: 0602-540 There is a missing } character.

Sponsored Links
    #6  
Old Unix and Linux 01-26-2013
RudiC RudiC is online now Forum Advisor  
Registered User
 
Join Date: Jul 2012
Last Activity: 28 May 2015, 1:45 PM EDT
Location: Aachen, Germany
Posts: 5,987
Thanks: 108
Thanked 1,605 Times in 1,507 Posts
Don't know how I spoiled that: append a } to the line NR>1...
Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
formatting the data mohan705 UNIX for Advanced & Expert Users 2 01-16-2012 04:16 AM
help with data formatting varman Shell Programming and Scripting 8 01-10-2012 10:23 PM
Script for data formatting shekhar_v4 Shell Programming and Scripting 6 01-27-2009 12:34 AM
Formatting Data nmeliasp UNIX for Dummies Questions & Answers 3 07-24-2008 07:53 PM



All times are GMT -4. The time now is 02:06 PM.