Using awk with NSTask

久未见 提交于 2019-12-12 04:31:24

问题


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 :)


回答1:


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 be echo string | awk ..., which would involve two NSTask and a NSPipe.

Finally, for string manupulation, use NSString instead, as mentioned by dreamlax.




回答2:


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.



来源:https://stackoverflow.com/questions/1250344/using-awk-with-nstask

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!