Ranges in AWK (file name is two lines down) 
Author Message
 Ranges in AWK (file name is two lines down)

************************************************************************
The following awk script will search for a specific IQ score of 100, &
then it will go two lines down, and report the file name that had that score.

  awk 'BEGIN {n=3;}{{if(index($0,"IQ score is 100")>0)n=0;else n++}
  {if(n==2){print $1}}}' $INPUT_FILE > $OUTPUT_FILE

My question is:
Q: How can I add a regular expression to this awk script so that it will
   report not just the files with a score of 100, but, all files within
   the range of 80 to 99?
************************************************************************
I tried using ~ /regular-expression/, but, that failed me:

Here's my best pass (which fails):

  awk 'BEGIN {n=3;}
  {{if(index($0,~ /IQ score is 8[0-9]|IQ score is 9[0-9]/)>0)n=0;else n++}
  {if(n==2){print $1}}}' $INPUT_FILE > $OUTPUT_FILE

My question is:
Q: How can I report all the files with an IQ score between a given range
   of 80 to 99?
------------------------------------------------------------
In this real-world problem, the $INPUT_FILE looks something like this:

   ...stuff...
   INFO (IQ): IQ score is 0 (best is 100).
   INFO (IQ): Program SKILL Lint finished with status FAIL.
        /tmp/ssk/testdir/file1.txt
   ...stuff...
   INFO (IQ): IQ score is 43 (best is 100).
   INFO (IQ): Program SKILL Lint finished with status FAIL.
        /tmp/ssk/testdir/file2.txt
   ...stuff...
   INFO (IQ): IQ score is 87 (best is 100).
   INFO (IQ): Program SKILL Lint finished with status PASS.
        /tmp/ssk/testdir/file3.txt
   ...stuff...
   INFO (IQ): IQ score is 100 (best is 100).
   INFO (IQ): Program SKILL Lint finished with status PASS.
        /tmp/ssk/testdir/file4.txt
   ...
   etc.
------------------------------------------------------------
Q: How can I add a regular expression to this awk script to
   report all the files with an IQ score ranging betwen 80 and 99?
   (The file name is always two lines below the score itself.)
------------------------------------------------------------

John Gianni



Mon, 16 Apr 2001 03:00:00 GMT  
 Ranges in AWK (file name is two lines down)


writes:

Quote:
>The following awk script will search for a specific IQ score of 100, &
>then it will go two lines down, and report the file name that had that score.

>  awk 'BEGIN {n=3;}{{if(index($0,"IQ score is 100")>0)n=0;else n++}
>  {if(n==2){print $1}}}' $INPUT_FILE > $OUTPUT_FILE

<snip>

Quote:
>I tried using ~ /regular-expression/, but, that failed me:

>Here's my best pass (which fails):

>  awk 'BEGIN {n=3;}
>  {{if(index($0,~ /IQ score is 8[0-9]|IQ score is 9[0-9]/)>0)n=0;else n++}
>  {if(n==2){print $1}}}' $INPUT_FILE > $OUTPUT_FILE

<snip>

Quote:
>In this real-world problem, the $INPUT_FILE looks something like this:

>   ...stuff...
>   INFO (IQ): IQ score is 0 (best is 100).
>   INFO (IQ): Program SKILL Lint finished with status FAIL.
>    /tmp/ssk/testdir/file1.txt
>   ...stuff...
>   INFO (IQ): IQ score is 43 (best is 100).
>   INFO (IQ): Program SKILL Lint finished with status FAIL.
>    /tmp/ssk/testdir/file2.txt

<snip>

Quote:
>Q: How can I add a regular expression to this awk script to
>   report all the files with an IQ score ranging betwen 80 and 99?
>   (The file name is always two lines below the score itself.)

....

Try

awk '/IQ score is/ && (80 <= $6 && $6 <= 99) {
    getline; getline; print $1

Quote:
}' $INPUT_FILE > $OUTPUT_FILE



Mon, 16 Apr 2001 03:00:00 GMT  
 Ranges in AWK (file name is two lines down)

Quote:

> ------------------------------------------------------------
> Q: How can I add a regular expression to this awk script to
>    report all the files with an IQ score ranging betwen 80 and 99?
>    (The file name is always two lines below the score itself.)
> ------------------------------------------------------------

Assumptions:

    1. The score is always preceded by the text "IQ score is" and
       is the sixth word on the line (i.e., the sixth field of the
       record). The text "IQ score is" only ever occurs on these
       lines.

    2. The corresponding file names always begin with a slash ("/")
       and are always the first word on a line that begins with a
       single tab. Only the lines with these file names match this
       pattern.

    3. A score line always precedes its corresponding file name
       line.

Solution (Summary):

Match this pattern:   /IQ score is/
Perform this action:  { score = $6 }
Match this pattern:   /^\t\// && score >= 80 && score <= 99
Perform this action:  { print score, $1 }

(The second pattern above is a compound expression with three
conditions.)

Solution (Demonstration):

$ cat iq.dat
...stuff...
INFO (IQ): IQ score is 0 (best is 100).
INFO (IQ): Program SKILL Lint finished with status FAIL.
        /tmp/ssk/testdir/file1.txt
...stuff...
INFO (IQ): IQ score is 43 (best is 100).
INFO (IQ): Program SKILL Lint finished with status FAIL.
        /tmp/ssk/testdir/file2.txt
...stuff...
INFO (IQ): IQ score is 87 (best is 100).
INFO (IQ): Program SKILL Lint finished with status PASS.
        /tmp/ssk/testdir/file3.txt
...stuff...
INFO (IQ): IQ score is 100 (best is 100).
INFO (IQ): Program SKILL Lint finished with status PASS.
        /tmp/ssk/testdir/file4.txt
...
etc.
$ awk '/IQ score is/ { score = $6 } /^\t\// { print score, $1 }' OFS='\t' iq.dat
0       /tmp/ssk/testdir/file1.txt
43      /tmp/ssk/testdir/file2.txt
87      /tmp/ssk/testdir/file3.txt
100     /tmp/ssk/testdir/file4.txt
$ awk '/IQ score is/ { score = $6 } /^\t\// && score >= 80 && score <= 99 \

Quote:
> { print score, $1 }' OFS='\t' iq.dat

87      /tmp/ssk/testdir/file3.txt
$

--
Jim Monty

Tempe, Arizona USA



Mon, 16 Apr 2001 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. join a range of lines using awk/sed

2. Outputting info from awk and using the info to name the awk output file

3. How to compare two files line-by-line

4. How to replace one or two words with one word with one line of awk code

5. using two files in awk

6. read two sets of data from two spread sheet files to two arrays problem

7. Writing two equal files without creating two files in dct

8. DOS File Lookup - just the file name, not the path AND file name

9. Two files with same name problem

10. Need to split file with two reports per line

11. HowTo? : convert file, take two line parts

12. Naming the output file based on awk script variable

 

 
Powered by phpBB® Forum Software