From awk(1) man pages, the definition of awk NR and FNR variables are:
NR ordinal number of the current record
FNR ordinal number of the current record in the current file
For a single file, FNR is nothing but NR (line number)
e.g.
$ cat main.txt
ID1:2.45
ID2:21.5
ID4:1.32
ID3:89.2
ID7:12.4
ID9:13
ID5:19.8
$ awk 'BEGIN {print "NR\tFNR\tline"}
{print NR"\t"FNR"\t"$0}
' FS=: main.txt
NR FNR line
1 1 ID1:2.45
2 2 ID2:21.5
3 3 ID4:1.32
4 4 ID3:89.2
5 5 ID7:12.4
6 6 ID9:13
7 7 ID5:19.8
And for more than one file, NR and FNR will be equal for the first processed file, but on the first line of the second and subsequent files FNR will start from 1 again.
Lets have one more file named filter.txt
$ cat subfile.txt
ID9
ID3
ID4
Now, lets print the same NR, FNR and the lines with both the files combined.
$ awk 'BEGIN {print "NR\tFNR\tline"}
{print NR"\t"FNR"\t"$0}
' FS=: subfile.txt main.txt
NR FNR line
1 1 ID9
2 2 ID3
3 3 ID4
4 1 ID1:2.45
5 2 ID2:21.5
6 3 ID4:1.32
7 4 ID3:89.2
8 5 ID7:12.4
9 6 ID9:13
10 7 ID5:19.8
You might be wondering, what can be the use of awk NR and FNR combination. I already did a few posts using NR==FNR functionality.
Lets discuss one of them.
$ cat main.txt
ID1:2.45
ID2:21.5
ID4:1.32
ID3:89.2
ID7:12.4
ID9:13
ID5:19.8
$ cat subfile.txt
ID9
ID3
ID4
Requirement: We need to delete those rows from main.txt for which the id field (1st field) has an entry in subfile.txt.
so, required output:
ID1:2.45
ID2:21.5
ID7:12.4
ID5:19.8
The solution is:
$ awk 'NR==FNR{A[$1];next}!($1 in A)' FS=: subfile.txt main.txt
So, associative array named A is created with the Ids of 1st processed file (subfile.txt, for which NR==FNR, as it is first in the sequence "subfile.txt main.txt" above).
To confirm,
$ awk 'NR==FNR {print $0,FILENAME}' FS=: subfile.txt main.txt
ID9 subfile.txt
ID3 subfile.txt
ID4 subfile.txt
So associative array A will contain the elements ID9 ID3 and ID4.
We proceed to the next lines and printing only those of remaining rows, which does not (!) have 1st filed as any one of the elements of the associative array A.
[FROM]http://unstableme.blogspot.com/2009/01/difference-between-awk-nr-and-fnr.html
分享到:
相关推荐
### awk中NR和FNR的区别详解与实例演示 在学习awk这一强大的文本处理工具时,了解NR和FNR这两个变量的用法对于高效地进行数据处理尤为重要。本文将详细介绍NR和FNR的区别,并通过实际例子来帮助理解它们各自的功能...
在深入探讨AWK中FNR和NR,OFS和FS,RS和ORS这些核心概念之前,我们先简要回顾一下AWK的基本功能。AWK是一种强大的文本处理工具,主要用于模式扫描与处理语言,广泛应用于数据处理、报告生成、格式转换等场景。其核心...
awk and sedawk and sedawk and sedawk and sedawk and sedawk and sedawk and sedawk and sedawk and sedawk and sedawk and sedawk and sedawk and sedawk and sedawk and sedawk and sedawk and sedawk and sed
NR - The Number of Records Variable RS - The Record Separator Variable ORS - The Output Record Separator Variable FILENAME - The Current Filename Variable Associative Arrays Multi-dimensional ...
awk提供了一系列内置变量,如`NR`(当前记录数)、`NF`(当前记录的字段数)和`FNR`(在当前文件中的记录数)。这些变量可以帮助我们编写更复杂的脚本。 **7. 函数** awk还提供了一些内建函数,如`length()`(返回...
在本压缩包"awk and sed 3"中,包含了一份名为"awk and sed3.pdf"的文档,很可能是对这两个工具的最新版介绍和深入教程。 `awk` 是一种编程语言,专门用于处理结构化文本文件。它的名字来源于其创始人 Alfred V. ...
执行`awk '{print "NR = " NR " FNR = " FNR $0}' file1 file2`会得到: ``` NR = 1 FNR = 1 a NR = 2 FNR = 2 b NR = 3 FNR = 3 c NR = 4 FNR = 1 d NR = 5 FNR = 2 e NR = 6 FNR = 3 f ``` 3. Awk如何引入外部...
- **NR、FILENAME、FNR**:NR是当前处理的记录总数,FILENAME是当前处理的文件名,FNR是当前文件的记录数。 **Awk变量的操作符** - **变量、一元操作符、算术操作符、字符串操作符、赋值操作符、比较操作符、正则...
### Linux中AWK内建变量FS、NF、NR、RT、RS、ORS、OFS详解 #### 一、概述 AWK是一种强大的文本处理工具,主要用于格式化文本数据,广泛应用于Linux/Unix环境中。它拥有丰富的内置变量和函数,使得用户能够方便地...
- `FNR`:当前文件中的行号,独立于总行号NR。 - `FS`:字段分隔符,默认为空白字符。 - `IGNORECASE`:忽略大小写差异,用于模式匹配。 - `NF`:当前行中的字段数量。 - `NR`:总行号,从第一行开始计数。 - `OFMT`...
This book is about a set of oddly named UNIX utilities, sed and awk. These utilities have many things in common, including the use of regular expressions for pattern matching. Since pattern matching ...
`awk`提供了许多内置变量,如`NF`表示当前行的字段数,`NR`表示已读取的记录数(行数),`FNR`则是在当前文件中的记录数。 **4. 模式匹配** `awk`的模式可以是正则表达式,例如`/pattern/`,也可以是逻辑表达式,如...
awk -F'[/,]' 'NR==FNR{a[$0]}NR>FNR{if ($2 in a) print $0}' b a >c ``` 这种方法的逻辑与方法一类似,但使用`NR`(总行数)和`FNR`(当前文件的行数)来区分处理不同的文件。当`NR==FNR`时,将`b`文件的内容...
利用NR和FNR变量,我们可以判断awk正在处理的是哪个文件中的行。通过设置RS参数,我们可以改变记录的分隔方式,使其与普通文本文件的处理有所不同。 结合上述介绍,awk命令是Linux环境下进行文本处理的重要工具,...
awk '{print NR, $0}' input-file ``` 对输出进行排序则需要结合 `sort` 命令。 ### AWK程序的选择和数据验证 AWK允许通过比较、计算或者文本内容来进行选择,例如: ```awk awk '$3 > 0' input-file # 选择第三...