我有 3 个文件,每个文件都有一个表格。文件1:
RollNo Name
1 Student1
2 Student2
3 Student3
4 Student4
文件2:
CourseId CourseName
CS11 Discrete Mathematics
CS12 Business Communication
MT15 Business Management
BM13 Linux
文件3:
RollNo CourseID Grade
1 CS11 AB
1 CS12 BC
1 BM13 AB
1 MT15 BB
2 CS11 AA
2 MT15 AB
2 BM13 AB
2 CS12 AA
(注意:文件中没有列名,即 RollNo、Name、CourseId)
这是我从源代码中找到的.awk的脚本文件,与我想要的非常相似:
BEGIN{
FS=" "
}
{
if(getline tmp < "file3.txt"){
split(tmp, arr)
if(arr[1]==$1){
#print arr[2],arr[3]
cc[i++]=arr[2]
while(getline tmp2 < "file2.txt"){
split(tmp2, arr2)
if(arr2[1]==arr[2]){
#print arr2[2],arr[3]
cName[j++]=arr2[2]
}
}
close("file2.txt")
grade[k++]=arr[3]
}
}
#print tmp
print "RollNo",$1
print "Name",$2
for(i in cc){
print cc[i],grade[i],cName[i]
}
}
我的输出应该是这样的:
Roll No: 1
Name: Student1
CourseID: CS11, CourseName: Discrete Mathematics, Grade:AB
CourseID: CS12, CourseName: Business Communication, Grade:BC
.....
similarly for Roll No: 2.