Help: very simple script problem

Discussion in 'Ruby' started by Tristan Knowles, Jul 14, 2005.

  1. Hi,

    After reading up on Ruby I thought it sounded good and
    decided to learn it so I can manipulate my files on my
    linux server such as log, csv, conf etc. Anyway, i'm
    a beginner when it comes to programming, so bear with
    me.

    I use the Asterisk PBX and came across this script
    here which reads a csv file and puts each record into
    an html table:
    http://rubyforge.org/snippet/detail.php?type=3Dsnippet&id=3D76


    Anyway, I thought I would then apply this basic
    concept to a log file, but I cannot get it to display
    through a web browser. Here is my script:


    =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
    #!/usr/bin/ruby

    My_Title =3D "Testing Log Read"
    Log_File =3D "/var/log/apache2/error.log"

    require 'cgi'

    cgi =3D CGI.new
    printf cgi.header("Content-type" =3D> "text/html\n\n")


    printf "<html>\n<head>\n"
    printf "<title>#{My_Title}</title>\n"
    printf "</head>\n<body bgcolor=3D\"#efefef\">\n"
    printf "<table width=3D\"80%\">\n<tr><td>Call
    records</td></tr>\n"
    if File.exists? Log_File
    lines =3D File.open(Log_File).readlines
    lines.each_with_index do |line, index|
    printf " <tr%s>\n <td>%d</td>",
    index % 2 =3D=3D 0 ? ' bgcolor=3D"#dddddd"' : '
    bgcolor=3D"#cccccc"', index + 1=20
    entries =3D line.chop.split(/\n/)
    entries.each_with_index do |field, index|
    printf "<td>%s</td>", entries
    end
    printf "\n</tr>\n"
    end
    else
    printf "No File Found\n"
    end
    printf "</body>\n</html>"
    =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D


    If I comment out all the printf commands related to
    web and cgi, and run it through the shell, it seems to
    be taking each line and putting it into <tr><td>
    brackets, but I can't get it to display through a
    browser, it stops at the third printf command.


    I have tried heaps of different things, but feel like
    i'm running around in circles now.

    Any advise with this one?


    Cheers.
    Tristan



    =09
    =09
    =09
    ___________________________________________________________=20
    Yahoo! Messenger - NEW crystal clear PC to PC calling worldwide with voic=
    email http://uk.messenger.yahoo.com
     
    Tristan Knowles, Jul 14, 2005
    #1
    1. Advertising

  2. Tristan Knowles

    pat eyler Guest

    Re: Help: very simple script problem

    Here's a working version that I've made some changes to (some of the
    changes were merely to show a different way of doing things -- but I
    got rid of the cgi module since you weren't really making enough use
    of it to justify the extra load time):

    #!/usr/bin/ruby

    My_Title =3D "Testing Log Read"
    Log_File =3D "/var/log/apache2/error.log"

    puts <<EOF
    Content-type: text/html

    <html>
    <head>
    <title>#{My_Title}</title>
    </head>
    <body bgcolor=3D\"#efefef\">
    <table width=3D\"80%\">
    <tr><td>Call records</td></tr>
    EOF

    if File.exists? Log_File
    lines =3D File.open(Log_File).readlines
    lines.each_with_index do |line, index|
    index % 2 =3D=3D 0 ? color =3D "#dddddd" : color=3D"#cccccc"
    puts " <tr bgcolor=3D\"#{color}\">\n <td>#{index}</td>"
    =20
    entries =3D line.chop.split(/\n/)
    entries.each do |field|
    puts "<td>#{field}</td>"
    end
    puts "\n</tr>\n"
    end
    else
    puts "No File Found\n"
    end
    puts "</body>\n</html>"



    On 7/14/05, Tristan Knowles <> wrote:
    > Hi,
    >=20
    > After reading up on Ruby I thought it sounded good and
    > decided to learn it so I can manipulate my files on my
    > linux server such as log, csv, conf etc. Anyway, i'm
    > a beginner when it comes to programming, so bear with
    > me.
    >=20
    > I use the Asterisk PBX and came across this script
    > here which reads a csv file and puts each record into
    > an html table:
    > http://rubyforge.org/snippet/detail.php?type=3Dsnippet&id=3D76
    >=20
    >=20
    > Anyway, I thought I would then apply this basic
    > concept to a log file, but I cannot get it to display
    > through a web browser. Here is my script:
    >=20
    >=20
    > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
    > #!/usr/bin/ruby
    >=20
    > My_Title =3D "Testing Log Read"
    > Log_File =3D "/var/log/apache2/error.log"
    >=20
    > require 'cgi'
    >=20
    > cgi =3D CGI.new
    > printf cgi.header("Content-type" =3D> "text/html\n\n")
    >=20
    >=20
    > printf "<html>\n<head>\n"
    > printf "<title>#{My_Title}</title>\n"
    > printf "</head>\n<body bgcolor=3D\"#efefef\">\n"
    > printf "<table width=3D\"80%\">\n<tr><td>Call
    > records</td></tr>\n"
    > if File.exists? Log_File
    > lines =3D File.open(Log_File).readlines
    > lines.each_with_index do |line, index|
    > printf " <tr%s>\n <td>%d</td>",
    > index % 2 =3D=3D 0 ? ' bgcolor=3D"#dddddd"' : '
    > bgcolor=3D"#cccccc"', index + 1
    > entries =3D line.chop.split(/\n/)
    > entries.each_with_index do |field, index|
    > printf "<td>%s</td>", entries
    > end
    > printf "\n</tr>\n"
    > end
    > else
    > printf "No File Found\n"
    > end
    > printf "</body>\n</html>"
    > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
    >=20
    >=20
    > If I comment out all the printf commands related to
    > web and cgi, and run it through the shell, it seems to
    > be taking each line and putting it into <tr><td>
    > brackets, but I can't get it to display through a
    > browser, it stops at the third printf command.
    >=20
    >=20
    > I have tried heaps of different things, but feel like
    > i'm running around in circles now.
    >=20
    > Any advise with this one?
    >=20
    >=20
    > Cheers.
    > Tristan
    >=20
    >=20
    >=20
    >=20
    >=20
    >=20
    > ___________________________________________________________
    > Yahoo! Messenger - NEW crystal clear PC to PC calling worldwide with voic=

    email http://uk.messenger.yahoo.com
    >=20
    >=20



    --=20
    thanks,
    -pate
    -------------------------
    We are often unable to tell people what they need to know, because=20
    they want to know something else, and would therefore only=20
    misunderstand what we said
    - the Raven (George MacDonald, Lilith)
     
    pat eyler, Jul 14, 2005
    #2
    1. Advertising

  3. Tristan Knowles wrote:
    > Hi,
    >
    > After reading up on Ruby I thought it sounded good and
    > decided to learn it so I can manipulate my files on my
    > linux server such as log, csv, conf etc. Anyway, i'm
    > a beginner when it comes to programming, so bear with
    > me.
    >
    > I use the Asterisk PBX and came across this script
    > here which reads a csv file and puts each record into
    > an html table:
    > http://rubyforge.org/snippet/detail.php?type=snippet&id=76


    <snip>

    For simple tables, allow me to shamelessly promote my html-table package.

    # example
    table = Table.new("Call Records")
    table.width = "80%"

    IO.foreach(log_file){ |line|
    table.push(Table::Row.new(line.chomp))
    }

    File.open("test_out.html", "w+"){ |fh|
    fh.puts(table.html)
    }

    That isn't exactly what you were trying to do with your example, but I
    hope you get the idea.

    Regards,

    Dan
     
    Daniel Berger, Jul 14, 2005
    #3
  4. Re: Help: very simple script problem

    Ahhh. Thats better :). Much easier to understand
    now.

    I didn't realise you could use puts to write plain
    html to a file, much quicker and easier than cgi.

    I can actually cut this down to this much now, not
    sure if i'm losing anything here, still seems to
    output the same.


    =3D=3D=3D=3D=3D=3D
    #!/usr/bin/ruby

    My_Title =3D "Testing Log Read"
    Log_File =3D "/var/log/apache2/error.log"

    puts <<EOF
    Content-type: text/html

    <html>
    <head>
    <title>#{My_Title}</title>
    </head>
    <body bgcolor=3D\"#efefef\">
    <table width=3D\"95%\">
    <tr><td colspan=3D\"2\">Apache Log Records</td></tr>
    EOF

    if File.exists? Log_File
    lines =3D File.open(Log_File).readlines
    lines.each_with_index do |line, index|
    index % 2 =3D=3D 0 ? color =3D "#dddddd" :
    color=3D"#cccccc"
    # where index =3D=3D 5
    puts "<tr bgcolor=3D\"#{color}\">\n=20
    <td>#{index}</td>\n <td>#{line}</td>" =20
    puts "</tr>"
    end
    else
    puts "<tr><td>No File Found:<br /> Check path:
    #{Log_File}</td></tr>\n"
    end
    puts "</body>\n</html>"
    =3D=3D=3D=3D=3D=3D

    Thanks again for your help:).


    Tristan



    --- pat eyler <> wrote:

    > Here's a working version that I've made some changes
    > to (some of the
    > changes were merely to show a different way of doing
    > things -- but I
    > got rid of the cgi module since you weren't really
    > making enough use
    > of it to justify the extra load time):
    >=20
    > #!/usr/bin/ruby
    >=20
    > My_Title =3D "Testing Log Read"
    > Log_File =3D "/var/log/apache2/error.log"
    >=20
    > puts <<EOF
    > Content-type: text/html
    >=20
    > <html>
    > <head>
    > <title>#{My_Title}</title>
    > </head>
    > <body bgcolor=3D\"#efefef\">
    > <table width=3D\"80%\">
    > <tr><td>Call records</td></tr>
    > EOF
    >=20
    > if File.exists? Log_File
    > lines =3D File.open(Log_File).readlines
    > lines.each_with_index do |line, index|
    > index % 2 =3D=3D 0 ? color =3D "#dddddd" :
    > color=3D"#cccccc"
    > puts " <tr bgcolor=3D\"#{color}\">\n =20
    > <td>#{index}</td>"
    > =20
    > entries =3D line.chop.split(/\n/)
    > entries.each do |field|
    > puts "<td>#{field}</td>"
    > end
    > puts "\n</tr>\n"
    > end
    > else
    > puts "No File Found\n"
    > end
    > puts "</body>\n</html>"
    >=20
    >=20
    >=20
    > On 7/14/05, Tristan Knowles <>
    > wrote:
    > > Hi,
    > >=20
    > > After reading up on Ruby I thought it sounded good

    > and
    > > decided to learn it so I can manipulate my files

    > on my
    > > linux server such as log, csv, conf etc. Anyway,

    > i'm
    > > a beginner when it comes to programming, so bear

    > with
    > > me.
    > >=20
    > > I use the Asterisk PBX and came across this script
    > > here which reads a csv file and puts each record

    > into
    > > an html table:
    > >

    >

    http://rubyforge.org/snippet/detail.php?type=3Dsnippet&id=3D76
    > >=20
    > >=20
    > > Anyway, I thought I would then apply this basic
    > > concept to a log file, but I cannot get it to

    > display
    > > through a web browser. Here is my script:
    > >=20
    > >=20
    > > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
    > > #!/usr/bin/ruby
    > >=20
    > > My_Title =3D "Testing Log Read"
    > > Log_File =3D "/var/log/apache2/error.log"
    > >=20
    > > require 'cgi'
    > >=20
    > > cgi =3D CGI.new
    > > printf cgi.header("Content-type" =3D>

    > "text/html\n\n")
    > >=20
    > >=20
    > > printf "<html>\n<head>\n"
    > > printf "<title>#{My_Title}</title>\n"
    > > printf "</head>\n<body bgcolor=3D\"#efefef\">\n"
    > > printf "<table width=3D\"80%\">\n<tr><td>Call
    > > records</td></tr>\n"
    > > if File.exists? Log_File
    > > lines =3D File.open(Log_File).readlines
    > > lines.each_with_index do |line, index|
    > > printf " <tr%s>\n <td>%d</td>",
    > > index % 2 =3D=3D 0 ? ' bgcolor=3D"#dddddd"' : '
    > > bgcolor=3D"#cccccc"', index + 1
    > > entries =3D line.chop.split(/\n/)
    > > entries.each_with_index do |field, index|
    > > printf "<td>%s</td>", entries
    > > end
    > > printf "\n</tr>\n"
    > > end
    > > else
    > > printf "No File Found\n"
    > > end
    > > printf "</body>\n</html>"
    > > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
    > >=20
    > >=20
    > > If I comment out all the printf commands related

    > to
    > > web and cgi, and run it through the shell, it

    > seems to
    > > be taking each line and putting it into <tr><td>
    > > brackets, but I can't get it to display through a
    > > browser, it stops at the third printf command.
    > >=20
    > >=20
    > > I have tried heaps of different things, but feel

    > like
    > > i'm running around in circles now.
    > >=20
    > > Any advise with this one?
    > >=20
    > >=20
    > > Cheers.
    > > Tristan
    > >=20
    > >=20
    > >=20
    > >=20
    > >=20
    > >=20
    > >

    >

    ___________________________________________________________
    > > Yahoo! Messenger - NEW crystal clear PC to PC

    > calling worldwide with voicemail
    > http://uk.messenger.yahoo.com
    > >=20
    > >=20

    >=20
    >=20
    > --=20
    > thanks,
    > -pate
    > -------------------------
    > We are often unable to tell people what they need to
    > know, because=20
    > they want to know something else, and would
    > therefore only=20
    > misunderstand what we said
    > - the Raven (George MacDonald, Lilith)
    >=20
    >=20




    =09
    ___________________________________________________________=20
    How much free photo storage do you get? Store your holiday=20
    snaps for FREE with Yahoo! Photos http://uk.photos.yahoo.com
     
    Tristan Knowles, Jul 14, 2005
    #4
  5. --- Daniel Berger <> wrote:=20
    > For simple tables, allow me to shamelessly promote
    > my html-table package.
    >=20
    > # example
    > table =3D Table.new("Call Records")
    > table.width =3D "80%"
    >=20
    > IO.foreach(log_file){ |line|
    > table.push(Table::Row.new(line.chomp))
    > }
    >=20
    > File.open("test_out.html", "w+"){ |fh|
    > fh.puts(table.html)
    > }
    >=20


    Hey that looks nice. Would be a much nicer way to put
    any file into a table, that can be my next step. I'm
    just trying to get the basics down first, but that
    definately looks useful.

    Can i just confirm that the above needs some sort of
    tables.rb file and a "require 'tables'" sort of thing?
    Or is this something else?


    Tristan



    =09
    ___________________________________________________________=20
    How much free photo storage do you get? Store your holiday=20
    snaps for FREE with Yahoo! Photos http://uk.photos.yahoo.com
     
    Tristan Knowles, Jul 14, 2005
    #5
  6. Tristan Knowles wrote:
    > --- Daniel Berger <> wrote:
    >
    >>For simple tables, allow me to shamelessly promote
    >>my html-table package.
    >>
    >># example
    >>table = Table.new("Call Records")
    >>table.width = "80%"
    >>
    >>IO.foreach(log_file){ |line|
    >> table.push(Table::Row.new(line.chomp))
    >>}
    >>
    >>File.open("test_out.html", "w+"){ |fh|
    >> fh.puts(table.html)
    >>}
    >>

    >
    >
    > Hey that looks nice. Would be a much nicer way to put
    > any file into a table, that can be my next step. I'm
    > just trying to get the basics down first, but that
    > definately looks useful.
    >
    > Can i just confirm that the above needs some sort of
    > tables.rb file and a "require 'tables'" sort of thing?
    > Or is this something else?


    Yes, this would be at the top:

    require "html/table"
    include HTML

    That's it.

    Regards,

    Dan
     
    Daniel Berger, Jul 14, 2005
    #6
  7. Daniel-
    Where can I get your html-tables package?

    Thanks -Ezra
    On Jul 14, 2005, at 9:32 AM, Daniel Berger wrote:

    > Tristan Knowles wrote:
    >
    >> --- Daniel Berger <> wrote:
    >>
    >>> For simple tables, allow me to shamelessly promote
    >>> my html-table package.
    >>>
    >>> # example
    >>> table = Table.new("Call Records")
    >>> table.width = "80%"
    >>>
    >>> IO.foreach(log_file){ |line|
    >>> table.push(Table::Row.new(line.chomp))
    >>> }
    >>>
    >>> File.open("test_out.html", "w+"){ |fh|
    >>> fh.puts(table.html)
    >>> }
    >>>
    >>>

    >> Hey that looks nice. Would be a much nicer way to put
    >> any file into a table, that can be my next step. I'm
    >> just trying to get the basics down first, but that
    >> definately looks useful.
    >> Can i just confirm that the above needs some sort of
    >> tables.rb file and a "require 'tables'" sort of thing?
    >> Or is this something else?
    >>

    >
    > Yes, this would be at the top:
    >
    > require "html/table"
    > include HTML
    >
    > That's it.
    >
    > Regards,
    >
    > Dan
    >


    -Ezra Zygmuntowicz
    Yakima Herald-Republic
    WebMaster
    509-577-7732
     
    Ezra Zygmuntowicz, Jul 14, 2005
    #7
  8. --- Ezra Zygmuntowicz <> wrote:

    > Daniel-
    > Where can I get your html-tables package?
    >=20
    > Thanks -Ezra
    > On Jul 14, 2005, at 9:32 AM, Daniel Berger wrote:


    In all its glory...
    http://raa.ruby-lang.org/project/html-table/


    =09
    =09
    =09
    ___________________________________________________________=20
    Yahoo! Messenger - NEW crystal clear PC to PC calling worldwide with voic=
    email http://uk.messenger.yahoo.com
     
    Tristan Knowles, Jul 14, 2005
    #8
  9. Re: Help: very simple script problem

    Just another quick question regarding flow control.

    What variable can I refer to, and what method do I use
    to create a while loop so that only x amount of lines
    are shown at once from a file?=20

    This is how I have been approaching it, its obviously
    not working, and I'm still not up to speed at working
    out how to use methods based on the online reference
    details only.

    =3D=3D=3D=3D=3D
    if File.exists? Log_File
    lines =3D File.open(Log_File).readlines
    lines.each_with_index do |line, index|
    index % 2 =3D=3D 0 ? color =3D "#dddddd" :
    color=3D"#cccccc"

    while index <=3D 50

    puts "<tr bgcolor=3D\"#{color}\">\n=20
    <td>#{index}</td>\n <td>#{line}</td>" =20
    puts "</tr>"
    =20
    end

    end
    =3D=3D=3D=3D=3D


    Am I even approaching this the correct way? Or should
    this be handled once step before with a grep
    statement?

    Thanks again.
    Tristan


    =09
    ___________________________________________________________=20
    How much free photo storage do you get? Store your holiday=20
    snaps for FREE with Yahoo! Photos http://uk.photos.yahoo.com
     
    Tristan Knowles, Jul 14, 2005
    #9
  10. Re: Help: very simple script problem

    Tristan Knowles wrote:
    > Just another quick question regarding flow control.
    >
    > What variable can I refer to, and what method do I use
    > to create a while loop so that only x amount of lines
    > are shown at once from a file?
    >
    > This is how I have been approaching it, its obviously
    > not working, and I'm still not up to speed at working
    > out how to use methods based on the online reference
    > details only.
    >
    > =====
    > if File.exists? Log_File
    > lines = File.open(Log_File).readlines
    > lines.each_with_index do |line, index|
    > index % 2 == 0 ? color = "#dddddd" :
    > color="#cccccc"
    >
    > while index <= 50
    >
    > puts "<tr bgcolor=\"#{color}\">\n
    > <td>#{index}</td>\n <td>#{line}</td>"
    > puts "</tr>"
    >
    > end
    >
    > end
    > =====
    >
    >
    > Am I even approaching this the correct way? Or should
    > this be handled once step before with a grep
    > statement?


    Your code looks fine as is, although I wonder if 50 is a constant number
    you can rely on. But, here's some more info for you:

    If you only want 1 line per iteration, then your best bet is to use
    IO.foreach. If you only want to slurp X number of lines into a
    variable, then just keep in mind that IO.readlines returns an array.
    So, you can use a range as the index and do:

    # No need to use the File class, btw
    lines = IO.readlines(Log_File)[0..49]

    Just keep in mind that, even though you're only selecting 50 lines, the
    entire file has been slurped into memory.

    Regards,

    Dan
     
    Daniel Berger, Jul 14, 2005
    #10
  11. Re: Help: very simple script problem

    --- Daniel Berger <> wrote:

    > If you only want 1 line per iteration, then your
    > best bet is to use=20
    > IO.foreach. If you only want to slurp X number of
    > lines into a=20
    > variable, then just keep in mind that IO.readlines
    > returns an array.=20
    > So, you can use a range as the index and do:
    >=20
    > # No need to use the File class, btw
    > lines =3D IO.readlines(Log_File)[0..49]
    >=20
    > Just keep in mind that, even though you're only
    > selecting 50 lines, the=20
    > entire file has been slurped into memory.
    >=20


    Thanks again. IO.readlines(file)[a..b] worked a
    treat.
    I will have a look at foreach.

    Once i am able to understand the reference more
    easily, I'm sure the doors of opportunity will open
    up. For now, I know the logical way i want to do
    something, but I'm not sure the ways ruby allows it.

    So far its been so simple though, its great.


    Cheers.
    Tristan


    =09
    =09
    =09
    ___________________________________________________________=20
    Yahoo! Messenger - NEW crystal clear PC to PC calling worldwide with voic=
    email http://uk.messenger.yahoo.com
     
    Tristan Knowles, Jul 14, 2005
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Raymond Arthur St. Marie II of III

    very Very VERY dumb Question About The new Set( ) 's

    Raymond Arthur St. Marie II of III, Jul 23, 2003, in forum: Python
    Replies:
    4
    Views:
    517
    Raymond Hettinger
    Jul 27, 2003
  2. shanx__=|;-

    very very very long integer

    shanx__=|;-, Oct 16, 2004, in forum: C Programming
    Replies:
    19
    Views:
    1,717
    Merrill & Michele
    Oct 19, 2004
  3. Abhishek Jha

    very very very long integer

    Abhishek Jha, Oct 16, 2004, in forum: C Programming
    Replies:
    4
    Views:
    466
    jacob navia
    Oct 17, 2004
  4. Peter

    Very very very basic question

    Peter, Feb 8, 2005, in forum: C Programming
    Replies:
    14
    Views:
    541
    Dave Thompson
    Feb 14, 2005
  5. olivier.melcher

    Help running a very very very simple code

    olivier.melcher, May 12, 2008, in forum: Java
    Replies:
    8
    Views:
    2,376
Loading...

Share This Page