The below AWK code does what (I think) you want to have done. It will also work in case of varying characters in field 5 (A, B, C, etc) and print the input lines into corresponding output files (OUTPUT_A.DAT, OUTPUT_B.DAT, OUTPUT_C.DAT, ...)
awk -F'|' '{
outfile = "OUTPUT_" $5 ".DAT"
if (!a[$5]++)
print "HEADER" FS outfile > outfile
print $0 > outfile}
END {
for (i in a)
print "TRAILER" FS sprintf("%010d", a) > "OUTPUT_" i ".DAT"}'
yes, rowcount should be replaced by rcnt and newfilename should be replaced by nfnam in my above script.
Hi user8,
Thanks a lot for quick response. But, I tried to run each individual command at command line and it is working fine. Is there any other simples way of doing this by modifying my script.
What shell are you using, on what OS and version? --------- ksh
How do you running the script? --------- by giving name of shell script in the command line.
Could you copy and past the input file from the sample you posted here into a new file and test with that input file?
What OS and version
How exactly are you running the script?
With copy and paste I mean the other way around, copy it from this website and paste it into a file on which you can test your script..
also
what happens if there are 4 lines in your inout file?