将 awk 与 NSTask 结合使用
我将如何使用这个 awk 命令:
awk 'NR>1{print $1}' string-to-modify
与 NSTask? 我已经尝试将 /usr/bin/awk 设置为启动路径,将 'NR>1{print $1}'
设置为参数,然后将要修改的字符串设置为另一个参数,但我得到的只是:
/usr/bin/awk: syntax error at source line 1
context is
>>> ' <<<
/usr/bin/awk: bailing out at source line 1
任何帮助,将不胜感激 :)
How would I use this awk command:
awk 'NR>1{print $1}' string-to-modify
with NSTask? I already tried setting /usr/bin/awk as the launch path, 'NR>1{print $1}'
as an argument, then the string to modify as another argument but all I get is this:
/usr/bin/awk: syntax error at source line 1
context is
>>> ' <<<
/usr/bin/awk: bailing out at source line 1
Any help would be appreciated :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你想达到什么目的? 您不需要启动 awk 来执行字符串操作。
如果您尝试从字符串中删除行,请尝试使用类似 NSString 的
getLineStart:end:contentsEnd:forRange:
方法。What are you trying to achieve? You shouldn't need to launch awk to perform string manipulation.
If you're trying to remove lines from a string, try using something like NSString's
getLineStart:end:contentsEnd:forRange:
method.首先,删除单引号:
NSTask
不会调用可以解释它们的 shell。 这就是您收到错误的原因。其次,
awk
将文件名作为最后一个参数,而不是字符串。 如果您确实需要 NSTask 和 awk,则 shell 命令将是 echo string | awk ...,这将涉及两个 NSTask 和一个 NSPipe。最后,对于字符串操作,请使用 NSString 代替,如 dreamlax 中提到的。
First, remove single quotes:
NSTask
doesn't invoque a shell which could interpret them. This is the cause of the error you get.Second,
awk
takes a filename as last argument, not a string. If you really need NSTask and awk, the shell command would beecho string | awk ...
, which would involve two NSTask and a NSPipe.Finally, for string manupulation, use NSString instead, as mentioned by dreamlax.