拆分在Mac OS X上使用AWK文件 [英] Splitting a file using AWK on Mac OS X
问题描述
我想拆就特定的使用分离AWK的文件。 awk的code是这样的:
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
这code完美的作品在我的Linux系统。但是,在Mac OS X上运行完全相同的code时,我收到以下错误:
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
我认为这是某种awk的附带OS X和mawk从Linux之间的差异。取出在打印字符串连接获取awk程序在两种平台上工作,但我想preFER保持preFIX和surfix。我怎样写分割的是分离器的文件分成多个零件编号及工程两个平台上的awk程序?
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
这篇关于拆分在Mac OS X上使用AWK文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!