2013-08-29 87 views
0

我有3個文件,每個文件都有一個表。 文件1:
使用Awk命令過濾來自多個表的數據

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,名稱,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. 

回答

1

這應該做的工作,並應該是簡單的跟隨 (需要GNU AWK)

FILENAME=="file1"{     # When reading file1 
    SID[$1] = $2      # Student ID/Name key value pair 
} 
FILENAME=="file2"{     # When reading file2 
    ID = $1       # Store the course ID 
    sub(/[A-Z]+[0-9]+\s+/,"",$0)  # Remove the course ID from the line 
    CID[ID] = $0      # Coursename/ID key value pair 
} 
FILENAME=="file3"{     # When reading file3 
    GRADE[$1][$2] = $3    # (StudentID/CourseID)/Grade key value pair 
} 
END {        # After all the files have been read 
    n = asorti(SID,SSID)    # Sort by student ID 
    for (i=1;i<=n;i++) {    # For all the students 
    s = SSID[i]      # Store the current student ID 
    print "Roll No:",s    # Print student ID 
    print "Name:",SID[s]    # Print student name 
    for (c in CID)     # For all the courses 
     if (GRADE[s][c])    # If the student has a grade for the course 
     printf "CourseID: %s, CourseName %s, grade:%s\n", c, CID[c], GRADE[s][c] 
    } 
} 

在您的輸入上運行:

$ awk -f report.awk file1 file2 file3 
Roll No: 1 
Name: Student1 
CourseID: BM13, CourseName Linux, grade:AB 
CourseID: MT15, CourseName Business Management, grade:BB 
CourseID: CS11, CourseName Discrete Mathematics, grade:AB 
CourseID: CS12, CourseName Business Communication, grade:BC 
Roll No: 2 
Name: Student2 
CourseID: BM13, CourseName Linux, grade:AB 
CourseID: MT15, CourseName Business Management, grade:AB 
CourseID: CS11, CourseName Discrete Mathematics, grade:AA 
CourseID: CS12, CourseName Business Communication, grade:AA 
Roll No: 3 
Name: Student3 
Roll No: 4 
Name: Student4 
+0

我不能得到任何輸出,但一個空殼。 – vishalkin

+0

您正在使用的3個文件的確切名稱是什麼?您是否閱讀並嘗試理解該腳本?我評論過每一行,對你來說不應該太難!注意腳本檢查要讀取的文件,所以如果你的文件沒有命名爲'file1','file2'和'file3',那麼改變腳本來讀取你的文件! –

+0

對不起傢伙...我沒有提到文件擴展,而閱讀文件... 我知道了.... :)謝謝很多親... – vishalkin

3

有一個一襯墊可以爲你工作:

awk 'ARGIND<3{d[$1]=$2;next}FNR>1{if($1!=x){printf "RollNo:%s\nName:%s\n", $1,d[$1];x=$1} printf "CourseID:%s, CourseName:%s, Grade:%s\n",$2,d[$2],$3}' f1 f2 f3 

更好的閱讀:

awk 'ARGIND<3{d[$1]=$2;next} 
    FNR>1{ 
       if($1!=x){printf "RollNo:%s\nName:%s\n", $1,d[$1];x=$1} 
       printf "CourseID:%s, CourseName:%s, Grade:%s\n",$2,d[$2],$3 
      }' f1 f2 f3 

測試你的榜樣:

kent$ head f* 
==> f1 <== 
RollNo Name 
1  Student1 
2  Student2 
3  Student3 
4  Student4 

==> f2 <== 
CourseId CourseName 
CS11  Discrete Mathematics 
CS12  Business Communication 
MT15  Business Management 
BM13  Linux 

==> f3 <== 
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 

kent$ awk 'ARGIND<3{d[$1]=$2;next}FNR>1{if($1!=x){printf "RollNo:%s\nName:%s\n", $1,d[$1];x=$1} printf "CourseID:%s, CourseName:%s, Grade:%s\n",$2,d[$2],$3}' f1 f2 f3 
RollNo:1 
Name:Student1 
CourseID:CS11, CourseName:Discrete, Grade:AB 
CourseID:CS12, CourseName:Business, Grade:BC 
CourseID:BM13, CourseName:Linux, Grade:AB 
CourseID:MT15, CourseName:Business, Grade:BB 
RollNo:2 
Name:Student2 
CourseID:CS11, CourseName:Discrete, Grade:AA 
CourseID:MT15, CourseName:Business, Grade:AB 
CourseID:BM13, CourseName:Linux, Grade:AB 
CourseID:CS12, CourseName:Business, Grade:AA 
+0

爲什麼它在一個廁所?並抱歉,但我想它作爲一個單獨的文件,即awkfile.txt – vishalkin

+0

@vishalkin你的問題不需要循環。我也沒有寫回路,是嗎?要獲得awk文件,只需將''....''之間的awk代碼複製到一個文件(當然帶有正確的「#!」)。我認爲這將是一件非常容易的事。 – Kent

+0

非常感謝Kent,sudo_O的回答解決了我的查詢 – vishalkin

1

這會給您的格式:

#!/usr/bin/awk -f 

BEGIN { 
    file1 = ARGV[1] 
    file2 = ARGV[2] 
    file3 = ARGV[3] 
    getline < file1 
    i = 0 
    while ((getline < file1) > 0) { 
     r = $1 
     rollno_in_order[i++] = r 
     t = $0 
     sub(/^[^[:blank:]]*[[:blank:]]*/, "", t) 
     rollno_to_studentname[r] = t 
    } 
    getline < file2 
    while ((getline < file2) > 0) { 
     courseid = $1 
     t = $0 
     sub(/^[^[:blank:]]*[[:blank:]]*/, "", t) 
     courseid_to_coursename[courseid] = t 
    } 
    i = 0 
    getline < file3 
    while ((getline < file3) > 0) { 
     i_to_courseid[i] = $2 
     i_to_grade[i] = $3 
     rollno_i_array[$1] = rollno_i_array[$1] i " " 
     ++i 
    } 
} 

END { 
    for (i = 0; i in rollno_in_order; ++i) { 
     r = rollno_in_order[i] 
     printf("Roll No: %d\n", r) 
     printf("Name: %s\n", rollno_to_studentname[r]) 
     c = split(rollno_i_array[r], a,/*/) 
     for (j = 1; j < c; ++j) { 
      k = a[j] 
      course_id = i_to_courseid[k] 
      course_name = courseid_to_coursename[course_id] 
      grade = i_to_grade[k] 
      printf("CourseID: %s, CourseName: %s, Grade: %s\n", course_id, course_name, grade) | "sort" 
     } 
     close("sort") 
     print "" 
    } 
} 

運行

awk -f script.awk -- file1 file2 file3 

還會送

Roll No: 1 
Name: Student1 
CourseID: BM13, CourseName: Linux, Grade: AB 
CourseID: CS11, CourseName: Discrete Mathematics, Grade: AB 
CourseID: CS12, CourseName: Business Communication, Grade: BC 
CourseID: MT15, CourseName: Business Management, Grade: BB 

Roll No: 2 
Name: Student2 
CourseID: BM13, CourseName: Linux, Grade: AB 
CourseID: CS11, CourseName: Discrete Mathematics, Grade: AA 
CourseID: CS12, CourseName: Business Communication, Grade: AA 
CourseID: MT15, CourseName: Business Management, Grade: AB 

Roll No: 3 
Name: Student3 

Roll No: 4 
Name: Student4 

它與任何標準的awk兼容。

+0

@vishalkin我希望你也可以嘗試我的腳本。 – konsolebox

+1

RollNo:1即Student1應該只有4個CousreID,C_Name和Grade ..相同的RollNo:2 – vishalkin

+0

@vishalkin我對此感到抱歉。現在已修好。 – konsolebox