简体   繁体   中英

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?

您可以使用变量修复此脚本:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM