gpt4 book ai didi

shell - 在 awk 中设置输出字段分隔符

转载 作者:行者123 更新时间:2023-12-03 20:04:21 24 4
gpt4 key购买 nike

我在我的 awk 脚本中尝试这个语句(在一个包含单独代码的文件中,所以不是内联的),脚本名称:print-table.awk

BEGIN {FS = "\t";OFS = "," ; print "about to open the file"}
{print $0}
END {print "about to close stream" }

并从 shell 以这种方式运行它
awk -f print-table.awk table

其中 table 是制表符分隔的文件,
我的目标是在外部函数中声明字段分隔符 (FS) 和输出字段分隔符 (OFS),并从 shell 调用
awk -f file input

不使用 -F"\t"在命令行中设置字段分隔符
并且没有将其标准输出到 sed 语句,用逗号替换制表符,

任何建议我该怎么做?

最佳答案

您需要说服 awk 某些内容已更改,以便使用您的 OFS 重新格式化 $0。以下工作虽然可能有更惯用的方法来做到这一点。

BEGIN {FS = "\t";OFS = "," ; print "about to open the file"}
{$1=$1}1
END {print "about to close stream" }

关于shell - 在 awk 中设置输出字段分隔符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20844666/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com