什么是NR和FNR,什么是"NR == FNR"?意味着? [英] What are NR and FNR and what does "NR==FNR" imply?

查看:509
本文介绍了什么是NR和FNR,什么是"NR == FNR"?意味着?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用awk学习文件比较.

I am learning file comparison using awk.

我发现了如下语法,

awk 'NR==FNR{a[$1];next}$1 in a{print $1}' file1 file2

我不明白NR==FNR在这方面的意义是什么? 如果我尝试使用FNR==NR,那么我还能得到相同的输出吗?

I couldn't understand what is the significance of NR==FNR in this? If I try with FNR==NR then also I get the same output?

它到底是做什么的?

推荐答案

在awk中,FNR表示当前文件中的记录号(通常是行号),而NR表示总记录号.运算符==是比较运算符,当两个周围的操作数相等时返回true.

In awk, FNR refers to the record number (typically the line number) in the current file and NR refers to the total record number. The operator == is a comparison operator, which returns true when the two surrounding operands are equal.

这意味着条件NR==FNR仅对第一个文件为true,因为对于每个文件的第一行FNR重置为1,但NR仍在增加.

This means that the condition NR==FNR is only true for the first file, as FNR resets back to 1 for the first line of each file but NR keeps on increasing.

此模式通常用于仅对第一个文件执行操作.该块内的next表示所有其他命令都将被跳过,因此它们只能在第一个文件以外的文件上运行.

This pattern is typically used to perform actions on only the first file. The next inside the block means any further commands are skipped, so they are only run on files other than the first.

条件FNR==NR将与NR==FNR相同的两个操作数进行比较,因此其行为方式相同.

The condition FNR==NR compares the same two operands as NR==FNR, so it behaves in the same way.

这篇关于什么是NR和FNR,什么是"NR == FNR"?意味着?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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