在设置awk中的输出字段分隔符 [英] setting the output field separator in awk

查看:1651
本文介绍了在设置awk中的输出字段分隔符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I'n在我的awk脚本尝试这种说法(含独立code文件,所以没有内置),脚本名:打印table.awk

I'n trying this statement in my awk script (in a file containing separate code, so not inline), script name: print-table.awk

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

和从shell中运行它这样

and running it this way from the shell

awk -f print-table.awk table

如果表是一个制表符分隔的文件,
我的目标是要申报的字段分隔符(FS)和外部函数中的输出字段分隔符(OFS),并从外壳调用只需将

Where table is a tab separated file, My goal is to declare the field separator (FS) and the output field separator (OFS) within the external function, and calling from the shell simply the

awk -f file input

不与-F\\ t的设置字段分隔在命令行
和不标准输出到一个sed的声明用逗号更换标签,

without setting the field separator in the command line with -F"\t" and without stdout it to a sed statement replacing the tab with a comma,

任何建议我如何能做到这一点?

Any advise how can i do that?

推荐答案

您需要说服的东西已经改变让它重新$ 0只使用您的OFS AWK。虽然有以下的作品可能是一个更惯用的方式来做到这一点。

You need to convince awk that something has changed to get it to reformat $0 using your OFS. The following works though there may be a more idiomatic way to do it.

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

这篇关于在设置awk中的输出字段分隔符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆