如何选择只在我AWK脚本中的第10行 [英] How to select only the first 10 rows in my AWK script

查看:544
本文介绍了如何选择只在我AWK脚本中的第10行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个大的CSV文件。基本上我想要做的是只显示前5列(我可以),但我也只是想显示前10行,这是我想不通。现在这是.awk文件脚本我有:

I have a large CSV file. Basically what I want to do is display only the first 5 columns (which I can) but I also only want to display the first 10 rows, which is what I can't figure out. Right now this is the .awk file script I have:

"BEGIN {FS = ","}
{print $1", " $2", " $3", " $4", " $5}"

在万亿期限,这是我用的命令,导致每一行的第5栏将显示:
$ AWK -f example.awk example.csv

In Tera Term this is the command I use, which results in the first 5 columns of every row to be displayed: $awk -f example.awk example.csv

我已经尝试了几个使用NR&LT的途径; = 11,但是,保持未来与错误讯息试图在万亿期限运行时

I have tried a couple of ways of using NR<=11 but that keeps coming up with error messages when trying to run on Tera Term.

请帮帮忙!

推荐答案

尝试:

awk -F, '{print $1,$2,$3,$4,$5} NR==10{exit}' OFS=', ' file.csv

[注意awk的code在 -quotes,没有双引号。这是必要的,以prevent从壳misinter preting $ 1 等,shell变量和扩大它们。]

[Note that the awk code is in single-quotes, not double-quotes. That is necessary to prevent the shell from misinterpreting $1, etc., as shell variables and expanding them.]


  • -F 会告诉awk使用作为输入字段分隔符。这相当于,但更简洁比 BEGIN {FS =,}

  • -F, tells awk to use , as the field separator on input. This is equivalent to but much briefer than BEGIN {FS = ","}.

打印$ 1,$ 2,$ 3,$ 4,$ 5 会告诉awk打印的前五列。 (参见下面的为什么不需要在这个版本字符串。)

print $1,$2,$3,$4,$5 tells awk to print the first five columns. (See below for why the ", " strings aren't needed in this version.)

NR == 10 {退出} 会告诉awk退出的第十行。

NR==10{exit} tells awk to exit on the tenth line.

OFS =','会告诉awk使用逗号空间作为输出字段分隔符。此设置使很多在原来的code不必要的报价的。

OFS=', ' tells awk to use comma-space as the field separator on output. This setting makes much of the quoting in the original code unnecessary.

这篇关于如何选择只在我AWK脚本中的第10行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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