什么是" NR == FNR"在AWK? [英] What is "NR==FNR" in awk?

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

问题描述

我使用的学习文件比较 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 significance of NR==FNR in this? If I try with FNR==NR then also I get same output?

到底是什么呢?

推荐答案

在awk中, FNR 是指在当前文件的记录数(通常是行号)和 NR 指总记录数。这意味着,条件 NR == FNR 只对第一个文件真的如 FNR 重置回1每个文件的第一行,但 NR 不断提高。

In awk, FNR refers to the record number (typically the line number) in the current file and NR refers to the total record number. 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.

此模式通常用于仅在第一个文件执行操作。在接下来里面的块是指任何进一步的命令被跳过,所以他们只在比第一其它文件运行。

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

It's unclear why you would expect FNR==NR to be any different to NR==FNR.

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

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