[comp.unix.questions] awk scripts, examples of

plantz@manta.NOSC.MIL (Glen W. Plantz) (12/22/90)

I need to use awk to scan a file that consists of lines, each line starting 
with an integer, followed by a line of text that should have a period and then
a newline character following that. The script should save the number at the
beginning of the line, and if the line does not have a "period" prior to the 
"newline", to print out a message, with the integer number that was at the
beginning of the line.

I do not use awk, but would like to learn. I have some of the "standard"
references, but would like to see more examples of "working" scripts to
aid in my learning.  Thanks in advance for any help.


==============================================================================
Glen Plantz				Computer Sciences Corporation
					4045 Hancock Street
email - plantz@nosc.mil			San Diego, CA  92110
phone - (619)225-2538

==============================================================================

morgan@ms.uky.edu (Wes Morgan) (12/27/90)

In article <1543@manta.NOSC.MIL> plantz@manta.NOSC.MIL (Glen W. Plantz) writes:
>
>I need to use awk to scan a file that consists of lines, each line starting 
>with an integer, followed by a line of text that should have a period and then
>a newline character following that. The script should save the number at the
>beginning of the line, and if the line does not have a "period" prior to the 
>"newline", to print out a message, with the integer number that was at the
>beginning of the line.
>

Well, here's a quick solution to your problem.  This has been tested on
an AT&T 3B2/1000 running SVR3.2.2.......

Given this text file "testtext":

1 this is a test of this function.
2 i think this is an editing application.
3 it might work in any text processor, though
999 a troff-like system could also use this stuff.

and this script:

#!/bin/sh
# 
# hack out and validate lines preceded by integers.

awk '{ linenum = $1;
       if (index($NF,".") == 0) {
          printf("Error in line %d; no period\n",linenum);
       } else {
          print;
       }
      }' testtext

You get this output:

1 this is a test of this function.
2 i think this is an editing application.
Error in line 3; no period
999 a troff-like system could also use this stuff.

If you need some more help, feel free to get in touch.  I don't claim 
that this is an elegant solution, but it's the old "top-of-the-head"
solution.....

O'Reilly and Associates have just published "sed & awk", the newest
of their Nutshell handbooks.  It's easy to read, and includes many
working scripts, 10 of which were submitted by Usenet readers.


-- 
    | Wes Morgan, not speaking for | {any major site}!ukma!ukecc!morgan | 
    | the University of Kentucky's |        morgan@engr.uky.edu         |
    | Engineering Computing Center |   morgan%engr.uky.edu@UKCC.BITNET  | 
     Lint is the compiler's only means of dampening the programmer's ego.