Split a big file into multiple files using awk

this thread is a continuation from previous thread
http://www.unix.com/shell-programming-and-scripting/223901-split-big-file-into-multiple-files-based-first-four-characters.html

..I am using awk to split file and I have a syntax error while executing the below code

[awk '!/^$/{ a=substr($0,1,4) print $0 > "SBSCR." a ".txt"}' harsha1.txt
 syntax error The source line is 1.
 The error context is
                !/^$/{ a=substr($0,1,4) >>>  print <<<  $0 > "SBSCR." a ".txt"}
 awk: The statement cannot be correctly parsed.
 The source line is 1.
]

I am using AIX 7.2 and this code worked fine previously.I am an awk dummy..please help what the synax error..please help me debug the code.

below is how my data text file looks like

Image - TinyPic - Free Image Hosting, Photo Sharing & Video Hosting

Try:

awk '!/^$/{ a=substr($0,1,4) print $0 > ("SBSCR." a ".txt")}' harsha1.txt

Note, however, that awk has a limited number of file descriptors available for use in creating output files. You may need to add logic to your code to close the previous output file when you switch to a new output file.

I have actually tried that but this time..It does not throw any error but takes ever to run and eventually I have to cancel the execution

---------- Post updated at 02:34 PM ---------- Previous update was at 08:36 AM ----------

I tried this and it worked

[$ awk '{print > substr ($0,1,4)}' file]..

Can someone help me in formatting this awk statement so that my output file name looks like ABCD.substr ($0,1,4).txt

I apologize; I must have been asleep when I looked at your code yesterday. Change:

awk '!/^$/{ a=substr($0,1,4) print $0 > "SBSCR." a ".txt"}' harsha1.txt

to:

awk '!/^$/{ a=substr($0,1,4); print $0 > ("SBSCR." a ".txt")}' harsha1.txt

You need both the semicolon and the parentheses to turn this into a working and portable awk command.

2 Likes

that worked great.thanks a lot