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?
You can fix this script by using a variable:
awk '/SEPARATOR/{n++}{filename = "part" n ".txt"; print >filename }' in.txt
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With