![](/img/trans.png)
[英]How to compare two files, one has different fields and other is a c file, using shell script?
[英]How to extract fields from 2 different files and store in an output file using shell script?
我發現以下命令有效。 但是有人可以解釋一下它的實際作用嗎?
awk 'NR==FNR{a[NR]=$2; next} {print a[FNR], $2}' file1 file2
它從文件中提取命令中傳遞的字段,並將其顯示在終端中。
awk 'NR==FNR{a[NR]=$2; next} {print a[FNR], $2}' file1 file2
由於NR
代表當前輸入文件中的記錄號和FNR
代表記錄號,所以在處理第一個文件時它們是相等的。 因此, awk 'NR==FNR { things1; next } { things2 }' file1 file2
awk 'NR==FNR { things1; next } { things2 }' file1 file2
意思是:
things1
1。 things2
。 a[NR]=$2; next
a[NR]=$2; next
意思是:
a[]
,索引為記錄數(通常為行數,在這種情況下為行數)。 print a[FNR], $2
表示:
這樣,這將產生並排包含兩個文件的第二個字段的輸出。
$ cat f1
1 2 3
4 5 6
7 8 9
10 11 12
$ cat f2
a1 a2 a3
a4 a5 a6
a7 a8 a9
a10 a11 a12
$ awk 'NR==FNR{a[NR]=$2; next} {print a[FNR], $2}' f1 f2
2 a2
5 a5
8 a8
11 a11
您可以在Idiomatic awk中找到更多信息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.