为什么 awk 中的 OFS 和 FS 不同?
在awk中,至少在gawk中,字段分隔符FS是空白(制表符或空格),这是合理的。然而,输出字段分隔符 OFS 默认设置为空格。我希望它是制表符,因为制表符作为 UNIX 文本文件中的列分隔符比空格更标准(根据我的经验)。使它成为一个空间的理由是什么?
In awk, at least in gawk, the field separator FS is whitespace (tab or space), which is reasonable. The output field separator OFS however is set to space by default. I would expect it to be tab, since tab is more standard as a separator of columns in UNIX text files than space (in my experience). What is the rationale behind making it a space?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
带 TAB 的文本在不同的文本编辑器中可能看起来不同。因为他们中的许多人都有“如何解释 TAB”选项,例如 4 个空格、8 个空格等。但是带空格的文本看起来到处都是一样的。
此外,一些缩进敏感的编程语言建议使用空格而不是制表符,例如 在这里。从您的角度来看,这个建议可能也不合理。
如果您希望将空间作为 OFS 默认值,您可以创建一个别名,例如
myawk=awk -v OFS='\t'
Text with TAB may look different in different text editors. Because many of them have the option 'how to interpret TAB' e.g. 4 spaces, 8 spaces etc. But text with space looks everywhere the same.
Also some indent sensitive programming languages recommend to use spaces instead of tab, e.g. here. from your point of view, this recommendation may not reasonable either.
If you prefer to have space as OFS default, you may create an alias say,
myawk=awk -v OFS='\t'
awk
编程语言可能比您对当今任何事实上的 Unix 标准的直觉还要古老。话虽如此,默认值是完全有意义的,其原因与人们反对在源文件中使用制表符缩进时经常看到的原因大致相同。
The
awk
programming language is probably older than your intuition of any present-day de facto Unix standard.Having said that, the default makes perfect sense, for roughly the same reasons you often see cited when people argue against using tabs for indentation in source files.
基于@Kent 脚本,以下是我在输入(F 参数)和输出(OFS 参数)中处理 csv 和 tsv 的别名:
Building on @Kent scripts, here are my aliases to handle csv and tsv, in input (F-parmeter) and output (OFS-parameter):
实际上,FS 的默认值是“ ”,因此 OFS 具有相同的值是有意义的。 awk 的实现是这样的,当 FS 为“”时,awk 会跳过任何前导或尾随空格,并将所有连续空格视为分隔字段,但 FS 和 OFS 的默认值是相同的,“ ”。
Actually, the default value of FS is " " so it makes sense for OFS to have the same value. The implementation of awk is such that when FS is " ", awk skips any leading or trailing spaces and treats all contiguous spaces as separating the fields but nevertheless the default values of both FS and OFS are identical, " ".