选择awk中的行和元素 [英] Select row and element in awk

查看:75
本文介绍了选择awk中的行和元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解到,在awk中,$2是第二列.如何指定第i行和第i行第j列的元素?

I learned that in awk, $2 is the 2nd column. How to specify the ith line and the element at the ith row and jth column?

推荐答案

要打印第二行:

awk 'FNR == 2 {print}'

要打印第二个字段:

awk '{print $2}'

要打印第五行的第三个字段:

To print the third field of the fifth line:

awk 'FNR == 5 {print $3}'

这是一个带有标题行和(冗余)字段描述的示例:

Here's an example with a header line and (redundant) field descriptions:

awk 'BEGIN {print "Name\t\tAge"}  FNR == 5 {print "Name: "$3"\tAge: "$2}'

顺便说一下,比"\ t \ t"还有更好的对齐列的方法.

There are better ways to align columns than "\t\t" by the way.

如果没有理由处理整个文件,请在打印完所需记录后立即使用exit停止操作:

Use exit to stop as soon as you've printed the desired record if there's no reason to process the whole file:

awk 'FNR == 2 {print; exit}'

这篇关于选择awk中的行和元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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