HTML Conversion of text file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting HTML Conversion of text file
# 1  
Old 07-20-2014
Code HTML Conversion of text file

Hi,

I have following text file. I want to convert it into the below HTML format. Kindly help.

Input Text File
Code:
Header 1
=======
Name:***
Age:***
Address:***
Work Phone:***
Email:***
Mobile:***
Country:***
City:***
Pincode:***

some text here ****

Header 2
=======
Name:***
Age:***
Address:***
Work Phone:***
Email:***
Mobile:***
Country:***
City:***
Pincode:***

some text here ****


Header 3
=======
Name:***
Age:***
Address:***
Work Phone:***
Email:***
Mobile:***
Country:***
City:***
Pincode:***

some text here ****

I want to convert the above plain text file to a html file with a table with a header row containing my headers from text file and the rest of the details in the corresponding columns. Here I have "====" as the delimiter for my headers. The ** represents my data which could be several lines of text.
Image

HTML Code:
<p>
    <p>
    </p>
</p>
<table border="1" cellpadding="0" width="1184">
    <tbody>
        <tr>
            <td valign="top">
                <pre>Header 1 </pre>
            </td>
            <td valign="top">
                <pre>Header 2</pre>
            </td>
            <td valign="top">
                <pre>Header 3</pre>
            </td>
            <td valign="top">
                <pre>Header 4</pre>
            </td>
        </tr>
        <tr>
            <td valign="top">
                <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>Pin code:***</pre>
                <pre><p> </p></pre>
                <pre>some text here ****</pre>
            </td>
            <td valign="top">
                <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>Pin code:***</pre>
                <pre><p> </p></pre>
                <pre>some text here ****</pre>
            </td>
            <td valign="top">
                <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>Pin code:***</pre>
                <pre><p> </p></pre>
                <pre>some text here ****</pre>
            </td>
            <td valign="top">
                <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>Pin code:***</pre>
                <pre><p> </p></pre>
                <pre>some text here ****</pre>
            </td>
        </tr>
    </tbody>
</table>

Last edited by ctrld; 07-20-2014 at 07:10 AM..
# 2  
Old 07-20-2014
Do you already have some code/script you've started with? Where exactly are you stuck in your implementation?
# 3  
Old 07-20-2014
If you are familiar with XSLT (Extensible Stylesheet Language Transformations), you can very easily convert your document into XHTML.
# 4  
Old 07-20-2014
HTML Conversion of text file

I tried out awk, but I am not able to get the proper output. Smilie

Code:
awk 'BEGIN{print "<table> <table border=2>"} {print "<tr>";for(i=1;i<=NF;i++)print "<td>" $i"</td>";print "</tr>"} END{print "</table>"} inputtext.txt >htmloutput.html

# 5  
Old 07-20-2014
Code:
akshay@nio:/tmp$ cat file
Header 1
=======
Name:***
Age:***
Address:***
Work Phone:***
Email:***
Mobile:***
Country:***
City:***
Pincode:***

some text here ****

Header 2
=======
Name:***
Age:***
Address:***
Work Phone:***
Email:***
Mobile:***
Country:***
City:***
Pincode:***

some text here ****


Header 3
=======
Name:***
Age:***
Address:***
Work Phone:***
Email:***
Mobile:***
Country:***
City:***
Pincode:***

some text here ****

Code:
akshay@nio:/tmp$ cat table.awk

function dothis( j){
	if(headerfound)
	{
		
		for(j=1;j<=i-1;j++)
		{
			c = "\t<pre>" td[j] "</pre>"
			out[ph] = ( ph in out) ? out[ph] "\n" c  : c
		}
		delete td; i = 0
	}
}
/=======/{
		h = p
		dothis()
		headerfound = 1 
		next
}

headerfound{
	td[++i] = $0
}

{
	p = $0; 
	ph = h
}
END{ 
	dothis() 
	for(i in out)
	{
		c = "\t<td><pre>" i "</pre></td>"
		header = length(header) ? header "\n" c : c

		c = "<td>\n" out[i] "\n</td>"
		other = length(other) ? other "\n" c : c
	}

	

	print "<table>\n<tbody>\n<tr>\n" header "\n</tr>\n<tr>\n" other "\n</tr>\n</tbody>\n</table>"
}

Code:
akshay@nio:/tmp$ awk -f table.awk file
<table>
<tbody>
<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>
	<pre></pre>
	<pre>some text here ****</pre>
	<pre></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>
	<pre></pre>
	<pre>some text here ****</pre>
	<pre></pre>
	<pre></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>
	<pre></pre>
</td>
</tr>
</tbody>
</table>

----edit----

Adding attributes and styles like border, width, etc I left to you, modify print statement as per your requirement

Last edited by Akshay Hegde; 07-20-2014 at 02:08 PM..
This User Gave Thanks to Akshay Hegde For This Post:
# 6  
Old 07-21-2014
Only one word : Awesome!!

This is an excellent piece of work Akshay. Hats off! The output is exactly what I was looking for. Thanks a ton for your timely help. I really appreciate the helping attitude of experts out here.
# 7  
Old 07-22-2014
This is quite embarrassingSmilie , I am again n trouble. I need to separate the "some text here ***" to a separate row. I tried the following but its not working. Smilie

Code:
END{ 
	dothis() 
	for(i in out)
	{
		c = "\t<td><pre>" i "</pre></td>"
		header = length(header) ? header "\n" c : c


                if ( out[i] !~ /^some text here/ ) {
                        c = "<td>\n" out[i] "\n</td>"
                        other1 = length(other1) ? other1 "\n" c : c
                }

		c = "<td>\n" out[i] "\n</td>"
		other = length(other) ? other "\n" c : c
	}

	
	print "<table>\n<tbody>\n<tr>\n" header "\n</tr>\n<tr>\n" other "\n</tr>\n</tbody>\n</table>"
}

The following output is what I trying to achieve.

Image

Last edited by ctrld; 07-22-2014 at 05:44 PM..
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

Featured Tech Videos