ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
<html>
<head>
</head>
<body>
paste your stuff here
</body>
</html>
And you can use <br> for line breaks.
PS
Usually you try things first, get stuck and post a question indicating what the problem is.
I am using Linux & this is the output of the one shell script which has been written by me.I have to arrange this in the form so that i can be able to put that data in wabpage hour by hour..
Compile and then run like any other Java class, probably using `javac SOURCE` and then `java genHtml`.
Solution in Perl:
Code:
#!/usr/bin/env perl
use warnings;
use strict;
open my $file, "data.txt";
print "<html><head><title>Perl!</title></head><body><table>\n";
while (<$file>) {
my $line = $_; chomp $line;
my @data = split /\s/, $line;
print "<tr><td>$data[0]</td><td>$data[1]</td></tr>\n";
}
print "</table></body></html>\n";
Run it by setting its execute bit and running it like a program.
Solution in Python:
Code:
#!/usr/bin/env python
print "<html><head><title>Python!</title></head><body><table>"
f = open("data.txt")
for line in f.readlines():
data = line.split()
print "<tr><td>" + data[0] + "</td><td>" + data[1] + "</td></tr>"
f.close()
print "</table></body></html>"
Run it by setting the file's executable bit and invoking it like a program.
Solution in Ruby:
Code:
#!/usr/bin/env ruby
puts "<html><head><title>Ruby!</title></head><body><table>"
File.open "data.txt" do |file|
file.each do |line|
data = line.split
puts "<tr><td>" + data[0] + "</td><td>" + data[1] + "</td></tr>"
end
end
puts "</table></body></html>"
Again, just set the executable bit and then run it like any other program.
You can execute this on your data file and redirect output by running `sed -f SOURCE DATAFILE > OUTPUT`.
Solution in Standard ML:
Code:
fun getList stream =
case TextIO.inputLine stream of
NONE => []
| SOME(v) => String.tokens ( fn #" " => true
| #"\n" => true
| _ => false ) v;
fun doit stream =
case getList stream of
[] => ()
| xs => ( print ( "<tr><td>" ^ List.nth (xs, 0)
^ "</td><td>" ^ List.nth (xs, 1)
^ "</td></tr>\n" ) ;
doit stream ) ;
fun main file =
( print "<html><head><title>Standard ML!</title></head><body><table>\n";
doit (TextIO.openIn file);
print "</table></body></html>\n" ) ;
val () = main "data.txt";
You can compile it with `mlton SOURCE` and then run the resulting binary, or execute it with `main DATAFILE` from your favorite SML REPL.
Solution in TCL:
Code:
#!/usr/bin/env tcl
set channelId [open data.txt r]
set line ""
puts "<html><head><title>TCL!</title></head><body><table>"
while { [gets $channelId line] > 1 } {
regexp {([0-9]+)\s+([0-9]+)} $line match submatch(0) submatch(1)
puts "<tr><td>$submatch(0)</td><td>$submatch(1)</td></tr>"
}
puts "</table></body></html>"
close $channelId
Run this by setting the executable bit of the source file and invoking it like any other script. You may have to change the first line to use `tclsh` instead of `tcl`, depending on your setup.
Last edited by taylor_venable; 01-08-2007 at 02:30 PM.
Reason: Added more examples.
Solution in Awk:
...
Solution in C:
...
Solution in Java:
...
Solution in Perl:
...
Solution in Python:
...
Solution in Ruby:
...
Solution in Scheme:
...
Solution in Sed:
...
Solution in Standard ML:
...
Solution in TCL:
Distribution: UBUNTU 5.10 since Jul-18,2006 on Intel 820 DC
Posts: 422
Rep:
Hey Jagan,
As a principle you should read up HTML tags and how HTML means that your ordinary text (hourly readings in this case) are "marked up" with the tags (no hyper text in your case). You wont always have a ready forum for your code reference.
If we have more file like this then how we can do.
Describe in C language.
Here you go:
Code:
#include <ctype.h>
#include <stdio.h>
void genHtml(char* infile, char* outfile) {
FILE* ins = fopen(infile, "r");
FILE* outs = fopen(outfile, "w");
char c = '_';
char datum[1024];
int i = 0, j = 0;
fprintf(outs, "<html><head><title>C!</title></head><body><table>\n");
while ((c = fgetc(ins)) != EOF) {
if (isdigit(c)) {
datum[i++] = c;
}
else if (isspace(c)) {
datum[i++] = '\0';
if (c != '\n') {
j = i;
}
else {
fprintf(outs, "<tr><td>%s</td><td>%s</td></tr>\n", datum, datum + j);
i = 0; j = 0;
}
}
}
fprintf(outs, "</table></body></html>\n");
fclose(ins);
fclose(outs);
}
int main(int argc, char** argv) {
int i = 1;
for (; i + 1 < argc; i += 2) {
genHtml(argv[i], argv[i+1]);
}
return 0;
}
Compile and run, specifying for arguments pairs of the input and output files to be used. For example, `./genHtml input.txt output.txt` or `./genHtml input1.txt output1.txt input2.txt output2.txt`. Don't worry about specifying the wrong number of parameters, like `./genHtml input.txt` because the unpaired value won't get used! (The same goes for any odd number of arguments, the last one is discarded.)
BTW, caveat emptor, this code shouldn't be considered extremely safe or totally correct. It's written in C for one. The maximum line size of 1023 characters. It also presumes that the file is in the right format, and won't complain if it's not. It's also undocumented, so don't use it if you don't understand it. (You can ask me for help on that, if you need too, though.)
You'd really probably be better off with a shell script that does the generation of the file, then uses Awk to create the HTML all at once. Then add it to cron so it runs every hour.
Alternatively, when you generate this data you could do it in XML and then use an XSLT stylesheet have your client browser generate HTML from it (the XML data). This would give your data more form (in XML) for other processing if necessary. Its also lighter on the server load. But it may lead to bloated files if you have a LOT of them, and the data doesn't have that much structure anyway. Just a thought.
Last edited by taylor_venable; 01-09-2007 at 11:39 AM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.