Simple awk question 
Author Message
 Simple awk question

Hi guys,

Im trying my first steps with awk, and i was wondering if there are any
commands like Readln() or something alike to read a line.
I still use a very old unix book: Unix, the complete reference, from
1988. I know its old, but hey, i'm just starting out...

The thing i want to do is to read a single line from a text file, find a
number between 2 fixed words, and put them in a different file.
The original file contains 1 line like:

face="Arial"><br><B>&nbsp;<nobr>15&#176;</nobr></b></font></td></tr>

I need the number "15" in this line, between "<nobr>" and the "&#176;"

Any idea's?

(p.s. it is for a statistical database that collects temperatures from
all over the world from wheather.yahoo.com on an hourly base)

Greetings from the netherlands,

-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.*-*-*.com/ - The #1 Newsgroup Service in the World!
-----==  Over 80,000 Newsgroups - 16 Different Servers! =-----



Sat, 13 Sep 2003 04:21:37 GMT  
 Simple awk question

Quote:
> Hi guys,

> Im trying my first steps with awk, and i was wondering if there are any
> commands like Readln() or something alike to read a line.
> I still use a very old unix book: Unix, the complete reference, from
> 1988. I know its old, but hey, i'm just starting out...

> The thing i want to do is to read a single line from a text file, find a
> number between 2 fixed words, and put them in a different file.
> The original file contains 1 line like:

> face="Arial"><br><B>&nbsp;<nobr>15&#176;</nobr></b></font></td></tr>

> I need the number "15" in this line, between "<nobr>" and the "&#176;"

> Any idea's?

> (p.s. it is for a statistical database that collects temperatures from
> all over the world from wheather.yahoo.com on an hourly base)

Derek,

Firstly, your best source of specific information is the relevant 'man'
page; try:

   man awk

Secondly, here is some code that will do as you require. It assumes that
there may be multiple 'temperature' lines within the file, and collects them
all, and then simply prints out the lot.

You execute as follows:

    awk -f srcFile dataFile

where 'srcFile' is the code below, and 'dataFile', the relevant HTML file.

I hope this helps.

BEGIN {
  /* Field separator */
  FS = ";";

Quote:
}

{
  /* Check if current line contains designated item */
  if (/<nobr>.*&#/)
  {
    /* Yes it does, so extract temperature, store in array */
    tmp = substr($2, 7);
    number[idx++] = substr(tmp, 1, index(tmp, "&") - 1);
  }

Quote:
}

END {

  /* Print out all the collected temperatures */
  for (i = 0; i < idx; i++)
  {
    print number[i];
  }

- Show quoted text -

Quote:
}



Sat, 13 Sep 2003 06:45:58 GMT  
 Simple awk question

% Im trying my first steps with awk, and i was wondering if there are any
% commands like Readln() or something alike to read a line.

The normal operation of awk is to read lines from input files
which are either specified on the command-line or set up in the
program in a certain way, perform tests (patterns) against each
input record, and execute code (actions) for each pattern which
evaluates to true, so it's often not necessary to have something
like readln. Having said that, there's a getline command.

% I still use a very old unix book: Unix, the complete reference, from
% 1988. I know its old, but hey, i'm just starting out...

Especially in this case, I'd suggest finding a newer reference.
awk underwent a major revision in 1989, which introduced a number
of useful features. The gawk manual is pretty good, widely available
on-line, and there's always a chance that it's been translated into
Dutch. The open group publishes the complete set of man pages for
standard Unix on its web site (http://www.opengroup.org -- search
for the `single unix specification'). This provides a succinct
reference.

% face="Arial"><br><B>&nbsp;<nobr>15&#176;</nobr></b></font></td></tr>
%
% I need the number "15" in this line, between "<nobr>" and the "&#176;"

If there can be no more than one per line, a script like this:

 match($0, /<nobr>[0-9]+\&#176;/) { print substr($0, RSTART+6, RLENGTH-12) }

will print out all those numbers. If there can be more than one per line,
you'd have to put that in a loop.
--

Patrick TJ McPhee
East York  Canada



Sat, 13 Sep 2003 09:57:37 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Simple awk question

2. Simple Awk Question

3. Simple AWK question

4. A very simple question on SED or AWK for a GURU, and an enjoyable problem

5. A simple question regarding shell scripts and awk

6. Awk sub command -simple question for html

7. awk:Simpler question: But critical ; please help

8. Simple question concerning awk command

9. Very simple awk for a simple user

10. Question in french (a simple question, very simple)

11. Simple forth question - maybe simple ?

12. Simple Question seeks simple answer

 

 
Powered by phpBB® Forum Software