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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Where to close the file? 1

Status
Not open for further replies.

AnotherAlan

Technical User
Feb 10, 2006
362
GB
Hi All,

I've just returned from two weeks in Italy and the brain is fried, consequently can't get my head around this today.

I have a script that takes a file and splits it based upon the data between the /^imp_/ match in the below nawk statement.

This has been working great, but while I was away they have been putting more trades through the system, meaning that the nawk statement below breaks at file 21.

nawk '/^imp_/ {f="split."++d} f{print > f}' $FILENAME

I know I can put a close in here, but when I tried using

nawk '/^imp_/ {f="split."++d} f{print > f}; {close (f)}' $FILENAME

it is only printing the last line to the split.xx file and not the whole block of code I need.

I've also tried this;

nawk '/^imp_/ {f="split."++d} f{print >> f}; {close (f)}' $FILENAME

but this is putting the line breaks in the wrong place, as in the second imp_ to END block below;

$ cat -vet IMPORT.DAT
imp_blim$
keys,BRO3,FIN,,10280591,,$
block_detail,NL0000888691,"ADVANCED SHARES",ISIN,02-Sep-09,16:19,S,10000.00,10.11770000,07-Sep-09,EUR,,101177.000,P,202.350,,0.20,,N,O,1$
prorate,,0.000,0.000,0.000,202.350,,N$
END$
imp_blim$
keys,BRO3,FIN,,10280591,,$
block_detail,NL0000888691,"ADVANCED SHARES",ISIN,02-Sep-09,16:19,S,10000.00,10.11770000,07-Sep-09,EUR,,101177.000,$ - this line break should not be here
P,202.350,,0.20,,N,O,1$
prorate,,0.000,0.000,0.000,202.350,,N$
END$

BTW - The END lines are not present in the original file, I have added them for easier reading.
I hope this makes sense, if not then please refer to the opening sentence.

I'm no expert on awk/nawk and will continue to test, but
all help / pointers would be appreciated.
 
Maybe something like this,

Code:
nawk '/^imp_/{ [COLOR=red]close(f)[/color]; f="split." ++d }{ print > f }' filename
 
Beautiful, I forgot about working backwards...

Thanks very much, that was a big help.

Alan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top