LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 01-08-2007, 04:45 AM   #1
jagankumarmehta
LQ Newbie
 
Registered: Dec 2006
Posts: 6

Rep: Reputation: 0
Question need help in programming..


Hi,

i have one file with the name of region.txt.

In that file i have counts for the particular hour.Given below:-
hour count
00 641
01 706
02 604
03 559
04 264
06 1
09 8
10 4
11 2
13 1
14 12
16 1
19 2
20 1
22 66
23 1

I want to print the count hour by hour in the html page.I need the coding for that.
 
Old 01-08-2007, 04:55 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,378

Rep: Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963
what do you mean "in the html page"?? you need to agive a *LOT* more information than that... you've not even said what langauge you're using.
 
Old 01-08-2007, 05:04 AM   #3
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,786

Rep: Reputation: 282Reputation: 282Reputation: 282
Just put some HTML tags around it

Basic html page:
Code:
<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.
 
Old 01-08-2007, 07:38 AM   #4
jagankumarmehta
LQ Newbie
 
Registered: Dec 2006
Posts: 6

Original Poster
Rep: Reputation: 0
Question

Quote:
Originally Posted by Wim Sturkenboom
Just put some HTML tags around it

Basic html page:
Code:
<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..
 
Old 01-08-2007, 09:53 AM   #5
taylor_venable
Member
 
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Rep: Reputation: 40
Smile Different Names for the Same Thing

Solution in Awk:
Code:
BEGIN { printf "<html><head><title>Awk!</title></head><body><table>\n" }
{ printf "<tr><td>%d</td><td>%d</td></tr>\n", $1, $2 };
END { printf "</table></body></html>\n" }
Run it with `awk -f SCRIPT DATAFILE > OUTPUT`.

Solution in C:
Code:
#include <ctype.h>
#include <stdio.h>

int main(int argc, char** argv) {
    FILE* fp = fopen("data.txt", "r");

    char c = '_';
    char datum[1024];

    int i = 0, j = 0;

    printf("<html><head><title>C!</title></head><body><table>\n");

    while ((c = fgetc(fp)) != EOF) {
        if (isdigit(c)) {
            datum[i++] = c;
        }
        else if (isspace(c)) {
            datum[i++] = '\0';
            if (c != '\n') {
                j = i;
            }
            else {
                printf("<tr><td>%s</td><td>%s</td></tr>\n", datum, datum + j);
                i = 0; j = 0;
            }
        }
    }

    printf("</table></body></html>\n");

    return 0;
}
Compile this with `gcc SOURCE` and run `a.out`, or specify the output binary with `gcc -o BINARY SOURCE`.

Solution in Java:
Code:
import java.io.*;

class genHtml {
    public static void main(String[] args) {
        try {
            BufferedReader br = new BufferedReader(new FileReader("data.txt"));
            String s;

            System.out.println("<html><head><title>Java!</title></head><body><table>");

            while ((s = br.readLine()) != null) {
                String[] data = s.split(" ");
                System.out.println("<tr><td>" + data[0] + "</td><td>" + data[1] + "</td></tr>");
            }

            br.close();

            System.out.println("</table></body></html>");
        }
        catch (Exception e) {
            System.err.println("ERROR!");
        }
    }
}
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.

Solution in Scheme:
Code:
(define (doit input-file)
  (let ((datum (read input-file)))
    (if (not (eof-object? datum))
        (begin
          (display "<tr><td>")
          (display datum)
          (display "</td><td>")
          (display (read input-file))
          (display "</td></tr>\n")
          (doit input-file))
        '())))

(define (init input-file)
  (display "<html><head><title>Scheme!</title></head><body><table>\n")
  (doit input-file)
  (display "</table></body></html>\n")
  '())

(call-with-input-file "data.txt" init)
Run this with your Scheme implementation. PLT Scheme, for example, uses `mzscheme -f SOURCE` to execute it.

Solution in Sed:
Code:
1i\
<html><head><title>Sed!</title></head><body><table>
s/^/<tr><td>/
s/$/<\/td><\/tr>/
s/ /<\/td><td>/
$a\
</table></body></html>
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 01:30 PM. Reason: Added more examples.
 
Old 01-08-2007, 02:09 PM   #6
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,786

Rep: Reputation: 282Reputation: 282Reputation: 282
There is 'something' that you can put in the head section of the above html page. That way you can let the browser refresh the page every hour.

Unfortunately you have to figure that out yourself (as I'm not 100% sure at this moment).
 
Old 01-08-2007, 08:01 PM   #7
95se
Member
 
Registered: Apr 2002
Location: Windsor, ON, CA
Distribution: Ubuntu
Posts: 740

Rep: Reputation: 32
Quote:
Originally Posted by taylor_venable
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:
Haha, awesome. Thanks for that reply!
 
Old 01-08-2007, 11:52 PM   #8
jagankumarmehta
LQ Newbie
 
Registered: Dec 2006
Posts: 6

Original Poster
Rep: Reputation: 0
Dear all

Thanks a lot for your support..............
 
Old 01-09-2007, 06:01 AM   #9
jagankumarmehta
LQ Newbie
 
Registered: Dec 2006
Posts: 6

Original Poster
Rep: Reputation: 0
Question

Hi Taylor,

If we have more file like this then how we can do.
Describe in C language.
 
Old 01-09-2007, 06:11 AM   #10
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Distribution: UBUNTU 5.10 since Jul-18,2006 on Intel 820 DC
Posts: 607

Rep: Reputation: 127Reputation: 127
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.

End
 
Old 01-09-2007, 10:35 AM   #11
taylor_venable
Member
 
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Rep: Reputation: 40
Quote:
Originally Posted by jagankumarmehta
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 10:39 AM.
 
  


Reply

Tags
awesome, awk, html, java, parse, perl, python, ruby, sed, tcl


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Difference between Userland programming and Kernel Programming.. meetsagittarian Programming 1 02-20-2006 12:26 AM
Difference between Top Down programming & Bottom up programming minil Programming 1 06-17-2005 02:42 AM


All times are GMT -5. The time now is 08:20 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration