Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP
Path: utzoo!mnetor!uunet!seismo!sundc!hqda-ai!cos!howard
From: howard@COS.COM (Howard C. Berkowitz)
Newsgroups: comp.unix.questions
Subject: awk question
Message-ID: <371@cos.COM>
Date: Fri, 17-Jul-87 14:12:03 EDT
Article-I.D.: cos.371
Posted: Fri Jul 17 14:12:03 1987
Date-Received: Sat, 18-Jul-87 15:09:23 EDT
Organization: Corporation for Open Systems, McLean, VA
Lines: 73
Keywords: arrays, control flow

I am attempting to write an awk program which reorganizes
text which has a repeating pattern of n lines of text 
followed by a heading line:
-----------------------INPUT TEXT EXAMPLE----------------
The purpose is to test if the implementation        
accepts an ACCEPT request correctly.                                      
.IP ISVB101       
The purpose is to test if the implementation        
detects the error when ACCEPT SPDU is sent with parameters in incorrect
order.            
.IP ISIB102 
---------------------------------------------------------
The awk program should store lines until the ".IP ..."
line is detected, then output (to file foo) the IP
line followed by all text lines:
------------------- DESIRED OUTPUT EXAMPLE  ------------
.IP ISVB101       
The purpose is to test if the implementation        
accepts an ACCEPT request correctly.                                      
.IP ISVB102       
The purpose is to test if the implementation        
detects the error when ACCEPT SPDU is sent with parameters in incorrect
order.            
---------------------------------------------------------
The awk program I have written for this, which includes
debugging code, is:

BEGIN { i = 1
        nip = 0
        ntx = 0
        print "INIT foo" >"foo" }

$1 !~ /.IP/
 {
  # add this text line to the s array.
  # do not yet output it.

  s[i++] = $0
  ++ntx
 }

$1 ~ /.IP/
 {
  # capture this line in the array's first
  # position, then print the array in order.

  s[0] = $0
  for (j=0; j<= i; j++) print j "-" s[j] > "foo"
  i = 1;
  ++nip
 }
END {print "nip=" nip " ntx=" > "foo"}
----------------------------------------------------------------
BEGIN gets control; END never does.  The output begins:

INIT foo
0-The purpose is to test if the implementation        
1-The purpose is to test if the implementation        
2-
0-accepts an ACCEPT request correctly.                                      
1-accepts an ACCEPT request correctly.                                      
2-
0-.IP ISVB101       
1-.IP ISVB101       
2-
------------------------------------------------------------
This type of duplicate output continues; the final END print
never executes.  Help!
-- 
-- howard(Howard C. Berkowitz) @cos.com
 {seismo!sundc, hadron, hqda-ai}!cos!howard
(703) 883-2812 [ofc] (703) 998-5017 [home]
DISCLAIMER:  I explicitly identify COS official positions.