Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Splitting a file using AWK on Mac OS X

Tags:

linux

macos

awk

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?

like image 627
pehrs Avatar asked Nov 02 '11 12:11

pehrs


1 Answers

You can fix this script by using a variable:

awk '/SEPARATOR/{n++}{filename = "part" n ".txt"; print >filename }' in.txt
like image 159
DarkDust Avatar answered Oct 27 '22 12:10

DarkDust