如何在处理之前获取AWK中的字段数 [英] How to get number of fields in AWK prior to processing

查看:622
本文介绍了如何在处理之前获取AWK中的字段数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在awk脚本的BEGIN部分中为文件创建头,但是要做到这一点,我需要知道有多少个字段.我可以在主要部分中进行检查,以检查是否NR==1,但是每行都会对此求值,从而减慢了速度.

I would like to create a header for a file in the BEGIN part of my awk script, but to do that I need to know how many fields there are. I could put a check within the main section to check if NR==1 but that will get evaluated on each row, slowing things down.

以下是我尝试使用单线的尝试.

Below is my attempt using a one-liner.

fields.txt

fields.txt

a   1
b   2
c   3

结果:

awk 'NR==1{a=NF; print "before begin, there are ", a, "fields"}BEGIN{print "there are ", a, "fields"}{print a"\t"$0}END{print "there were", a, "fields"}' fields.txt
there are   fields
before begin, there are  2 fields
2   a   1
2   b   2
2   c   3
there were 2 fields

我猜BEGIN块仍在前一个块之前得到评估.我是否真的实现了目标,还是NR==1检查仍在每一行得到评估?

I guess the BEGIN block still gets evaluated before the preceding block. Have I really accomplished my goal, or is the NR==1 check still getting evaluated on each line?

编辑 所以只是为了说明我为什么要尝试以自己的方式做

EDIT So just to put in perspective why I'm trying to do it the way I am

  1. 我有一个文件,其中包含10万行40列
  2. 此文件是管道中另一个进程的输出,其中awk脚本是最后一步
  3. 我正在根据其他行计算两行并将其添加到输出中
  4. 我希望最终文件包含一个标题,该标题反映了两个新添加的列

推荐答案

听起来这是您要尝试执行的操作:

It sounds like this is what you're trying to do:

awk '
  BEGIN {if ((getline < ARGV[1]) > 0) a=NF; print "there are", a, "fields"}
  {print a"\t"$0}
  END {print "there were", a, "fields"}
' file
there are 2 fields
2       a   1
2       b   2
2       c   3
there were 2 fields

但如果考虑到NR==1检查相对于要对数据执行的任何其他转换所产生的微小性能影响,则值得使用idk.

but idk if it's worthwhile given the tiny performance impact of an NR==1 check relative to whatever other transformations you're going to perform on the data.

请确保您已阅读并完全理解在 http://awk上使用getline的所有含义. freeshell.org/AllAboutGetline (如果您正在考虑使用它的话).

Make sure you read and fully understand all of the implications of using getline at http://awk.freeshell.org/AllAboutGetline if you're considering using it.

这篇关于如何在处理之前获取AWK中的字段数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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