LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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 03-05-2011, 04:55 AM   #1
unsymbol
LQ Newbie
 
Registered: Mar 2011
Posts: 1

Rep: Reputation: 0
Parse Text File With Perl


I'm dumping an HTML document with lynx and wanted to parse some float values from the file but having a hard time figuring out how to do this in a shell script.

Code:
   These weather Pages are provided by the Department of Electronics,
   University of York
   in partnership with our sponsor [1]Vaisala.

Temperature:     6.2 °C        Highest:           33 °C      (Mon 17th Jul 2006)
                               Lowest:            -12.3 °C   (Mon 6th Dec 2010)
Wind Chill:      3.6 °C        Lowest:            -17.8 °C   (Thu 30th Jan 2003)
Pressure:        1028.0 mBars  Highest:           1041 mBars (Sat 17th Feb 2001)
                               Lowest:            949 mBars  (Mon 30th Oct 2000)
Wind Speed:      7.8 mph       Highest:           47.3 mph   (Mon 7th Feb 2011)
Wind Gust:       14.9 mph      Highest:           69.1 mph   (Tue 1st Sep 2009)
Wind Direction:  027 Degrees
Humidity:        85.4 %
Dew Point:       3.9 °C
Total Rainfall:
(Since Midnight) 0.00 mm       Highest:           62.4 mm    (Mon 25th Jun 2007)
                               Highest Intensity: 58.0 mm/h  (Mon 7th Jun 2010)
Total Hail Fall:
(Since Midnight) 0.0 Hits/cm^2

   Fetched at 10:42, Saturday 5th March 2011 (GMT) - (53°56'45.74"N,
   1°3'13.74"W)
   Maximum and minimum since 21/12/1998

References

   1. http://www.vaisala.com/
I'd like to parse the Temperature: Wind Chill: Pressure: Wind Speed: Wind Gust: Wind Direction: Dew Point: Total Rainfall: Total Hail Fall: values so that I have a text output as follows (maintaining negative values where possible).

Code:
6.2 3.6 1028 7.8 14.9 27 85.4 3.9 0 0
 
Old 03-05-2011, 05:36 AM   #2
kurumi
Member
 
Registered: Apr 2010
Posts: 228

Rep: Reputation: 53
Ruby(1.9+)

Code:
#!/usr/bin/env ruby  -Ku
st=[]
f=File.open("file")
begin
    while (line = f.readline)
        if line[/Temperature|Wind Chill|Pressure|Wind Speed|Wind Gust|Wind Direction|Dew Point/]
            st << line.split(/:\s+/)[1].split(/\s+/)[0]
        elsif line[/Total Rainfall|Total Hail Fall/]
            st << f.readline.split(/\)/)[1].split(/\s+/)[1]
        end
    end
rescue EOFError
    f.close
    puts st.join(" ")
end
Test run:
Code:
# ruby test.rb
6.2 3.6 1028.0 7.8 14.9 027 3.9 0.00 0.0
 
  


Reply



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
[SOLVED] sed - parse text file elexx Programming 8 02-22-2011 10:19 AM
Perl Parse .bin file acandria17 Programming 2 07-07-2009 05:42 AM
How to parse text file to a set text column width and output to new text file? jsstevenson Programming 12 04-23-2008 02:36 PM
perl script to parse this file ohcarol Programming 10 11-02-2006 09:50 AM
optimizing perl parse file. eastsuse Programming 1 12-22-2004 02:49 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:38 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration