有没有办法避免“字符串中的换行"?在awk中的错误? [英] Is there a way to avoid "newline in string" errors in awk?
本文介绍了有没有办法避免“字符串中的换行"?在awk中的错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
包含换行符的cmdline args如何传递给 awk
?请参见下面的两个示例:
How are cmdline args containing newline chars passed to awk
? See two examples below:
awk -v s='text' 'BEGIN { print(s) }'
text
awk -v s=$'\n''text' 'BEGIN { print(s) }'
awk: newline in string
text... at source line 1
推荐答案
您将使用gawk原样工作.像您正在使用的OSX(BSD)awk一样,不要在字符串中放入换行符.
What you have would work as-is with gawk. With OSX (BSD) awk like you're using, either don't put a linefeed character in the string.
$ awk -v s='\ntext' 'BEGIN{ print s }'
text
或逃脱它:
awk -v s='\
text' 'BEGIN{ print s }'
text
或(随便是任何awk都不要)不使用-v而是将其作为参数传递:
or (portably with any awk) don't pass it using -v but as an argument instead:
$ awk 'BEGIN{s=ARGV[1]; ARGV[1]=""; print s }' $'\n''text'
text
这篇关于有没有办法避免“字符串中的换行"?在awk中的错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文