Reputation: 27
i have two files lets say... 1) student records having information like id,name and city 2) student marks having id ,totalmarks,percentage
Student Records (file)
101 | nik | mumbai 102 | zeel | chennai 103 | gurav | delhi
Student Marks (file)
101 | 80 | 80 102 | 90 | 90 103 | 90 | 90
and i want to retrieve a information in the forms like student name with it's percentage
nik | 80 zeel | 90 gurav | 90
how to write this using awk command
Upvotes: 0
Views: 83
Reputation: 195289
awk -F'|' -v OFS="|" 'NR==FNR{a[$1]=$2;next}$1 in a{print a[$1],$3}' studentfile markfile
didn't test, but should work.
output would be like:
nik|80
zeel|90
....
Edit
-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)
Upvotes: 1