Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with CLOSE command in AWK 1

Status
Not open for further replies.

Czar24

Technical User
Mar 22, 2004
16
US
Hey all, newbie to awk here.

I'm trying to write an awk command that will split test out of a larger file and put it into smaller files based on start and stop sequences. I'm running into the system limitation of 10 files open and can't get the close() command to work. Any help is appreciated.

The source file is:
11111111111111 dont want
11111111111112 start.*101-01
11111111111113 file 1 text
11111111111114 stop
11111111111115 dont want
11111111111116 start.*101-02
11111111111117 file 2 text
11111111111118 stop
11111111111119 dont want
11111111111120 start.*101-03
11111111111121 file 3 text
11111111111122 stop
11111111111123 dont want
11111111111124 start.*102-01
11111111111125 file 4 text
11111111111126 stop
11111111111127 dont want
11111111111128 start.*102-02
11111111111129 file 5 text
11111111111130 stop
11111111111131 dont want
11111111111132 start.*102-03
11111111111133 file 6 text
11111111111134 stop
11111111111135 dont want
11111111111136 start.*102-04
11111111111137 file 7 text
11111111111138 stop
11111111111139 dont want
11111111111140 start.*102-05
11111111111141 file 8 text
11111111111142 stop
11111111111143 dont want
11111111111144 start.*103-01
11111111111145 file 9 text
11111111111146 stop
11111111111147 dont want
11111111111148 start.*103-02
11111111111149 file 10 text
11111111111150 stop
11111111111151 dont want
11111111111152 start.*103-03
11111111111153 file 11 text
11111111111154 stop
.
.
.
.
.
.

My awk file is:

awk '
BEGIN{{prefix=100}}
/start/,/stop/ {{file = "test" prefix}{print >> file}{close(file)}}
/stop/ {{++prefix}}
' source


---------

This file is to take everything between "Start" and "Stop" and save it in a file called "test1##" for each case.

Any help is greatly appreciated!



Scott
 
Try this :
[tt]
awk '
BEGIN{prefix=100 }
/start/,/stop/ {file = "test" prefix ; print >> file}
/stop/ {if (file != "") close(file) ; ++prefix}
' source
[/tt]

Jean Pierre.
 
Jean Pierre, thanks for the quick response! but I'm still getting the same problem:

awk: too many output files 10
record number 42


If it matters, I'm running Korn Shell.

Here is my modified script, sould be identical to yours unless I'm missing something:

awk '
BEGIN{prefix=100 }
/start/,/stop/ {file = "test" prefix ; print >> file}
/stop/ {if (file != "") close(file) ; ++prefix}
' source
 
Another version, i am not sute that this new version resolves the problem :
[tt]
awk '
BEGIN{prefix=100 }
/START/ { if (file != "") close(file) ; file = "test" prefix++ }
/START/,/STOP/ { print > file)
' source
[/tt]

Jean Pierre.
 
Still getting the file limits... could this be something with my system?
 
Have you try nawk instead of awk ?

Jean Pierre.
 
Jean Pierre-

That worked! Thanks a million!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top