pehrs
pehrs

Reputation: 1531

Splitting a file using AWK on Mac OS X

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?

Upvotes: 6

Views: 4351

Answers (1)

DarkDust
DarkDust

Reputation: 92442

You can fix this script by using a variable:

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

Upvotes: 9

Related Questions