2013-05-09 57 views
0

設置不同的文件,我有兩個文件讓說......具有類似於ID,名稱和城市 2)學生標記爲ID,totalmarks,百分比信息 1)學生檔案如何使用filename在AWK

學生檔案(文件)

 
101 | nik | mumbai 
102 | zeel | chennai 
103 | gurav | delhi 

學生商標

 
101 | 80 | 80 
102 | 90 | 90 
103 | 90 | 90 

,我想找回像學生姓名與形式的一個信息(文件)這是個

 
nik | 80 
zeel | 90 
gurav | 90 

如何寫這個使用awk命令

+0

zeel的標誌應該是90(輸出) – Kent 2013-05-09 16:09:37

回答

1
awk -F'|' -v OFS="|" 'NR==FNR{a[$1]=$2;next}$1 in a{print a[$1],$3}' studentfile markfile 

沒有測試,但應該工作。

輸出會是這樣:

nik|80 
zeel|90 
.... 

編輯

-F'|'     #Field separator 
-v OFS="|"    #output field separator 
'NR==FNR{a[$1]=$2;next} #NR is overall record line number, FNR is record line number from one Input File. see man page for detail. this part was processing the first input file, and save id and name in an array 
$1 in a{print a[$1],$3}'#now processing the 2nd input file, the marks. if id in the array, print the value of the array (name), and the mark percentage field ($3) 
+0

你能提供一些關於它的信息是怎麼回事功能.. 什麼是平均數NR = FNR – nik 2013-05-09 16:02:05

+0

請參閱編輯回答 – Kent 2013-05-09 16:07:50

+0

在此代碼中首先NR == FNR {a [$ 1] = $ 2; next}將整個執行或整個動作寫入''逐個執行....什麼哈你可以告訴我 – nik 2013-05-09 16:21:13