HTML Conversion of text file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting HTML Conversion of text file
# 8  
Old 07-22-2014
Code:
BEGIN { L=0; H=0 }

/=======/ {
        H++;
        INDATA=1;
        L=0;
        next
}

# Titles in T[H]
{       T[H]=$0;        }

# Lines in D[X,line], line count in D[X,0]
INDATA {        D[H-1,++L]=$0   ;       D[H-1,0]=L      }

END {
        # Remove trailing blank lines, find some text here appendices
        for(N=0; N<H; N++)
        {
                do { D[N,0]--; } while(D[N,D[N,0]] == "");

                if(D[N,D[N,0]-1] == "")
                {
                        SOME[N]=D[N,D[N,0]];
                        D[N,0] -= 2;
                }
        }

        printf("<table>\n<trbody>\n<tr>\n");
        for(N=0; N<H; N++) print "\t<td><pre>" T[N] "</pre></td>";

        print "</tr>\n<tr>";

        for(N=0; N<H; N++)
        {
                print "<td>";
                for(M=1; M<=D[N,0]; M++) print "\t<pre>" D[N,M] "</pre>";
                print "</td>";
        }

        print "</tr>\n<tr>";

        for(N=0; N<H; N++)
        {
                print "\t<td><pre>" SOME[N] "</pre></td>"
        }

        printf("</tr>\n</tbody>\n</table>\n");
}

Code:
<table>
<trbody>
<tr>
        <td><pre>Header 1</pre></td>
        <td><pre>Header 2</pre></td>
        <td><pre>Header 3</pre></td>
</tr>
<tr>
<td>
        <pre>Name:***</pre>
        <pre>Age:***</pre>
        <pre>Address:***</pre>
        <pre>Work Phone:***</pre>
        <pre>Email:***</pre>
        <pre>Mobile:***</pre>
        <pre>Country:***</pre>
        <pre>City:***</pre>
        <pre>Pincode:***</pre>
</td>
<td>
        <pre>Name:***</pre>
        <pre>Age:***</pre>
        <pre>Address:***</pre>
        <pre>Work Phone:***</pre>
        <pre>Email:***</pre>
        <pre>Mobile:***</pre>
        <pre>Country:***</pre>
        <pre>City:***</pre>
        <pre>Pincode:***</pre>
</td>
<td>
        <pre>Name:***</pre>
        <pre>Age:***</pre>
        <pre>Address:***</pre>
        <pre>Work Phone:***</pre>
        <pre>Email:***</pre>
        <pre>Mobile:***</pre>
        <pre>Country:***</pre>
        <pre>City:***</pre>
        <pre>Pincode:***</pre>
</td>
</tr>
<tr>
        <td><pre>some text here ****</pre></td>
        <td><pre>some text here ****</pre></td>
        <td><pre></pre></td>
</tr>
</tbody>
</table>

This User Gave Thanks to Corona688 For This Post:
# 9  
Old 07-23-2014
Thanks for the really quick reply, Corona. I have several lines in the "some text here" paragraph. Number of lines in that may vary. The above code is putting only the last line into the row. The only thing constant is my number of lines before "some text here" (before blank line) ie 9. Probably what I need is to pick up 9 lines after header and put it into a row and rest of the text it into another row.
# 10  
Old 07-24-2014
Hi.

I like the flexibility of awk, but I find that the code tends to be very specific, and not as general as I would like -- as General George Patton said I don't like to pay for the same real estate twice. meaning that I don't like to solve similar problems all over again.

So I look for tools that can help more generally. In this case, I found one that transforms text into HTML, and it knows how to recognize tables: txt2html.

However, the input is structured in blocks vertically:
Code:
a
b
c

and I think it is more useful to have them appear horizontallly:
Code:
a b c

and that's an easy job (in this situation) for csplit to create a number of files, and then paste to align them side-by-side. Then we can augment the pieces with sed to conform to one of the table type formats.

Here is the script and result:
Code:
#!/usr/bin/env bash

# @(#) s3	Demonstrate transform text to table, txt2html
# t2t: http://www.scholnick.net/t2t
# txt2tags: http://txt2tags.sourceforge.net

# Utility functions: print-as-echo, print-line-with-visual-space, debug.
# export PATH="/usr/local/bin:/usr/bin:/bin"
LC_ALL=C ; LANG=C ; export LC_ALL LANG
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C csplit paste txt2html

FILE=${1-data2}

pl " Sample of input data file $FILE, sizes of split files:"
head -3 $FILE ; tail -3 $FILE

# Remove debris, split data into separate files, combine side-to-side
rm -f xx*
csplit -z -k $FILE '/^Header/' {*}
paste xx* > f1

pl " Results, txt2html (adding markup):"
( pe ; pe ) > f2
sed 's/^/| /;s/	/ | /g;s/$/|/' f1 >> f2
( pe ; pe ) >> f2
rm -f f2.html
txt2html --make_tables f2 > f2.html
ls -lgG f2.html

rm -f xx*
exit 0

producing:
Code:
./s3

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian 5.0.8 (lenny, workstation) 
bash GNU bash 3.2.39
csplit (GNU coreutils) 6.10
paste (GNU coreutils) 6.10
txt2html /usr/bin/txt2html version: 2.51

-----
 Sample of input data file data2, sizes of split files:
Header 1
=======
Name:***

Text line 1
Text line 2
175
152
140

-----
 Results, txt2html (adding markup):
-rw-r--r-- 1 1323 Jul 24 11:37 f2.html

See the attachment f2.html and man pages.

That was fairly straight-forward. If one can use perl, then a host of other possibilities arise. The approach can be similar, with statements storing data in rows, but more custimization can be done. I find that perl code is far more likely to be generalizable, and so can handle option processing, which is not a strength of awk (although it can be done).

Best wishes ... cheers, drl
This User Gave Thanks to drl For This Post:
# 11  
Old 07-29-2014
In the future, please post representative data. Or better yet, if possible, real data. Simplified data only requires simplified programs.
This User Gave Thanks to Corona688 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Format text file to html

Hi Experts, Anybody out there figure out on how to achieve in shell scripts or tools. I have done googling to find solutions but no luck. I have thousands of .txt files to batch process, please see the below sample text content after -------- start here --------. What I want to achieve is to... (10 Replies)
Discussion started by: lxdorney
10 Replies

2. Shell Programming and Scripting

Conversion of a text file to html

hi i hav a file called focus. which is the output file. i want to convert this file to html file and mail it. help with code and explanatio. i am basic in unix (7 Replies)
Discussion started by: wasim999
7 Replies

3. Shell Programming and Scripting

html to csv conversion

thanks for allowing me to join your forum i have a html file with three columns ------------Last visit date , URL and link,,,,,,,, how can i convert the same into csv so that i can output into database... the mechine is linux...i made a little googling and got idea that there is ways for... (2 Replies)
Discussion started by: certteam
2 Replies

4. Shell Programming and Scripting

Conversion of spaces Text file into CSV format file

Input file (each line is separaed by spaces )given below: Name Domain Contact Phone Email Location ----------------------- ------------------------------------------------ ------- -----... (18 Replies)
Discussion started by: sreenath1037
18 Replies

5. Shell Programming and Scripting

shell or perl script needed for ldif file to text file conversion

This is the ldf file dn: sdcsmsisdn=1000000049,sdcsDatabase=subscriberCache,dc=example,dc=com objectClass: sdcsSubscriber objectClass: top postalCode: 29600 sdcsServiceLevel: 10 sdcsCustomerType: 14 givenName: Adelia sdcsBlackListAll: FALSE sdcsOwnerType: T-Mobile sn: Actionteam... (1 Reply)
Discussion started by: LinuxFriend
1 Replies

6. Shell Programming and Scripting

outputting a text file in html

is there anyway i can paste/cat a text file into a html paragraph <p> function html_header { cat <<END <html> <head><title>${1}</title></head> <body> <p> ${2} </p> END } function html_footer { cat <<END </body> </html> END (2 Replies)
Discussion started by: magnia
2 Replies

7. Shell Programming and Scripting

Converting a text file to HTML

Hi, I need to convert a text file formatted like this ("tshark -z conv,ip" output) to HTML: ===================================================================================================== IPv4 Conversations Filter:<No Filter> | <- ... (4 Replies)
Discussion started by: ph0enix
4 Replies

8. UNIX for Dummies Questions & Answers

How do I extract text only from html file without HTML tag

I have a html file called myfile. If I simply put "cat myfile.html" in UNIX, it shows all the html tags like <a href=r/26><img src="http://www>. But I want to extract only text part. Same problem happens in "type" command in MS-DOS. I know you can do it by opening it in Internet Explorer,... (4 Replies)
Discussion started by: los111
4 Replies

9. UNIX for Dummies Questions & Answers

Binary data to text file conversion

Dear Sir; i want to know how the binary data convert to text file or readablw format (ASCII).If possible pl. help me for the software and where it is available for download. i.e. (1 Reply)
Discussion started by: auro123
1 Replies

10. UNIX for Dummies Questions & Answers

ISAM FILE CONVERSION TO TEXT

I am having a problem. There is one ISAM file available in SCO UNIX environment. I want to convert it to simple comma delimited text file. Actual problem is with the size of the file. For the time being, size is 1.3 GB and I have to grab the subset of the data after every five minutes e.g. in... (3 Replies)
Discussion started by: a.waqar
3 Replies
Login or Register to Ask a Question