In my quest to solve a bigger problem (See my previous post called "Create SQL DML insert statements from file using AWK or similar" - sorry, not allowed to post urls until I have > 5 posts) I'm trying to get my head round awk, but have some problem figuring out why the following script does work while the corresponding(?) one-liner does not?
Here is the script that works:
#! /usr/bin/awk -f
/^ ABC_/{
g++
f=$1
}
{
print $0>f
}
It splits a large file into smaller files based on blocks of text starting with " ABC_". And it does work.
However, If I try to enter this as a one-liner like this:
awk '/^ ABC_/{g++ f=$1} {print $0>f}' largefile.txt
I get the following error:
awk: line 1: syntax error at or near =
Why? What is wrong with my one-liner?
missing semi colon after the g++
awk '/^ ABC_/{g++; f=$1} {print $0>f}' largefile.txt
1 Like
A missing semi colon it was - thank you very much itkamaraj!
---------- Post updated at 11:00 AM ---------- Previous update was at 09:42 AM ----------
Well, it worked fine on my Linux-box, but as soon as I moved it over to my really old Solaris server (Solaris 10 8/07) I get
awk: too many output files 10
record number 55
So, based on info found elswhere on this forum, I tried to use nawk instead:
nawk '/^ ABC_/{g++; f=$1} {print $0>f; close(f)}' largefile.txt
But this gives me just empty files... Is there a workaround for my script when it comes to this limitation of 10 open files in Solaris? Can I tweak the script in some way?
---------- Post updated at 11:53 AM ---------- Previous update was at 11:00 AM ----------
I got past the number of open files limitation by using the POSIX compliant awk instead of the awk that is in the solaris standard path (/usr/bin/awk):
/usr/xpg4/bin/awk '/^ ABC_/{g++; f=$1} {print $0>f}' largefile.txt