NR,表示awk開始執行程序后所讀取的數據行數.
FNR,與NR功用類似,不同的是awk每打開一個新文件,FNR便從0重新累計.
下面看兩個例子:
1,對於單個文件NR 和FNR 的 輸出結果一樣的 :
# awk '{print NR,$0}' file1 1 a b c d 2 a b d c 3 a c b d #awk '{print FNR,$0}' file1 1 a b c d 2 a b d c 3 a c b d
2,但是對於多個文件 :
# awk '{print NR,$0}' file1 file2 1 a b c d 2 a b d c 3 a c b d 4 aa bb cc dd 5 aa bb dd cc 6 aa cc bb dd # awk '{print FNR,$0}' file1 file2 1 a b c d 2 a b d c 3 a c b d 1 aa bb cc dd 2 aa bb dd cc 3 aa cc bb dd
在看一個例子關於NR和FNR的典型應用:
現在有兩個文件格式如下:
#cat a 張三|000001 李四|000002 #cat b 000001|10 000001|20 000002|30 000002|15
想要得到的結果是將用戶名,帳號和金額在同一行打印出來,如下:
張三|000001|10
張三|000001|20
李四|000002|30
李四|000002|15
執行如下代碼
#awk -F \| 'NR==FNR{a[$2]=$0;next}{print a[$1]"|"$2}' a b
注釋:
由NR=FNR為真時,判斷當前讀入的是第一個文件a,然后使用{a[$2]=$0;next}
循環將a文件的每行記錄都存入數組a,並使用$2第2個字段作為下標引用.
由NR=FNR為假時,判斷當前讀入了第二個文件b,然后跳過{a[$2]=$0;next},
對第二個文件cdr的每一行都無條件執行{print a[$1]"|"$2},
此時變量$1為第二個文件的第一個字段,與讀入第一個文件時,采用第一個文件第二個字段$2為數組下標相同.
因此可以在此使用a[$1]引用數組。
=========================================================================
下面是CU大神jason680的詳細過程分析
awk -F'|' 'NR==FNR{a[$2]=$0;next}{print a[$1] FS $2}' a b
There is no BEGIN block, and FS="|" by -F'|' argument
沒有開始模塊,直接識別-F選項,加參數
start to first file 'a'
從a文件的第一行開始
1. read file a line 1 and get data 張三|000001
讀取文件a的第一行,得到數據
A: $0=張三|000001
B: $1=張三
C: $2=000001
NR and FNR are the same equal to 1, and run NR=FNR block
此時,NR與FNR的值相等都為1,執行NR=FNR模塊
NR==FNR{a[$2]=$0;next}
A: a[$2]=$0
a[000001]=張三|000001
B: next
next cycle and get next line data
2. read file a line 2 and get data 李四|000002
讀取文件a的第二行,得到數據
A: $0=李四|000002
B: $1=李四
C: $2=000002
NR and FNR are the same equal to 2, and run NR=FNR block
此時,NR與FNR的值相等都為1,執行NR=FNR模塊
NR==FNR{a[$2]=$0;next}
A: a[$2]=$0
a[000002]=李四|000002
B: next
next cycle and get next line data
end of the file a, and get next file b data
讀完文件a,然后讀取下一個文件b的數據
3. read file b line 1, and get data 000001|10
讀取文件b的第一行,然后得到數據
A: $0=000001|10
B: $1=000001
C: $2=10
now, NR is 3 and FNR is 1, they are not eqaul
此時,NR與FNR的值不同,不執行NF=FNR模塊,執行下一個模塊{print a[$1] FS $2}
and didn't run NR=FNR block,
and run next block {print a[$1] FS $2}
a[$1] => a[000001] => 張三|000001
FS => |
$2 => 10
you will see the output
張三|000001|10
4. read file b line 2, and get data 000001|20
A: $0=000001|20
B: $1=000001
C: $2=20
NR is 4 and FNR is 2, they are not eqaul
and didn't run NR=FNR block,
and run next block {print a[$1] FS $2}
a[$1] => a[000001] => 張三|000001
FS => |
$2 => 20
you will see the output
張三|000001|20
cycle to read the file b
5. read file b line 3, and get data 000002|30
...
output==> 李四|000002|30
6. read file b line 4, and get data 000002|15
...
output==> 李四|000002|15