Splitting a file using AWK on Mac OS X

4.2k Views Asked by At

I am trying to split a file using AWK on a specific separator. The awk code looks like this:

awk '/SEPARATOR/{n++}{print > "part" n ".txt" }' in.txt

this code works perfectly on my Linux systems. But when running exactly the same code on Mac OS X i get the following error:

awk: syntax error at source line 1
 context is
    /SEPARATOR/{n++}{print > "part" >>>  n <<<  ".txt" }
awk: illegal statement at source line 1

I assume that this is some kind of difference between the awk that ships with OS X and mawk from Linux. Removing the string concatenation in the print gets the awk program to work on both platforms, but I would prefer to keep the prefix and surfix. How do I write an awk program that splits a file on a separator into multiple numbered parts and works on both platforms?

1

There are 1 best solutions below

0
DarkDust On BEST ANSWER

You can fix this script by using a variable:

awk '/SEPARATOR/{n++}{filename = "part" n ".txt"; print >filename }' in.txt