2014-01-21 53 views
0

我需要comapre兩個文件f1.txtf2.txt並獲得比賽和非比賽,對於這種情況 我期待檢查的第二場f2.txt位於StartRange和EndRange之間f1.txt,如果是,則先打印f2.txt, 的第二個字段,然後打印整行f1.txt。並且在f1.txt上找不到匹配,表示「未找到」,然後打印f2.txt整行。awk的基礎上開始範圍和結束範圍比較2個文件,打印匹配和區別:

f1.txt

Flag,StartRange,EndRange,Month 
aa,1000,2000,cc,Jan-13 
bb,2500,3000,cc,Feb-13 
dd,5000,9000,cc,Mar-13 

f2.txt

ss,1500 
xx,500 
gg,2800 
yy,15000 

所需的輸出

ss,1500,aa,1000,2000,cc,Jan-13 
xx,500,Not Found,Not Found,Not Found,Not Found 
gg,2800,bb,2500,3000,cc,Feb-13 
yy,15000,Not Found,Not Found,Not Found,Not Found 
+0

你還試過了什麼?那是如何失敗的? –

+0

抱歉不知道如何在Ranges – SVR

回答

0

這可能會爲你工作:

gawk 'BEGIN { 
     FS="," # Field separator 
     c=1 # counter 
     while ((getline line < ARGV[1]) > 0) { 
      if (line !~ "Flag,StartRange,EndRange,Month") { # No need for header 
       F[c]=line;     # store line 
       split(line,a,",")   # split line 
       F2[c]=a[2] ; F3[c]=a[3]  # store the lines' range parts 
       c++ 
      } 
     } 
     } 
FILENAME==ARGV[2] { 
    # Work on second file 
    for (i in F) { # For every line scan the first file 
     # if within a range, step out 
     if ($2>=F2[i] && $2<=F3[i]) {found=i ; break} 
     # else check next 
     else {found=0} 
    } 
    # if the above found anything print the line from second file 
    # with the relavant line from the first 
    if (found>0) { 
     print $0 "," F[found] 
    } 
    # otherwise the not found message 
    else { 
     print $0 ",Not Found,Not Found,Not Found,Not Found" 
    } 
}' f1.txt f2.txt 
+0

上工作,謝謝,在運行這個sytax時,得到了下面的錯誤gawk:cmd。 line:8:F2 [c] = a [2]; F3 [c] = a [3]#存儲行 gawk:cmd。行:8:^語法錯誤 range.sh:第10行:C++:命令未找到 range.sh:第11行:語法錯誤附近的意外令牌'}' range.sh:第11行:'}' – SVR

+0

謝謝很多Zsolt Botyki,它工作正常... – SVR