2013-05-30 50 views
2

我有2個單獨的文件lostType.txt(顯示選定的選項)和lost.txt(顯示內存泄漏)我使用dialog來創建GUI。使用複選框,我正在從用戶處獲取一些數字間隔來限制將要打印的內存泄漏。我要做的就是,打印的內存泄漏是在選定的尺寸的線,和泄漏類型是lostType.txt如何在awk中搜索文件

例如,這是lost.txt

LEAK SUMMARY 
100 bytes definitely lost 
0 bytes indirectly lost 
0 bytes possibly lost 
100 bytes still reachable 
0 bytes suppressed 

如果用戶想要查看definetely丟失和間接丟失,lostType.txt具有選擇爲看到泄漏,其中大小爲0和10個字節或100個和250個字節之間的以下

definetely 
indirectly 

假設用戶。現在,輸出必須是100 bytes in 1 blocks definitely lost

我做了什麼至今如下,

for choice in $choices 
do 
    case $choice in 
    1) 
     #"Memory Leak <= 10 Byte" 
     cat lost.txt | awk '{ 
      if ($1 <= 10 && $1 > 0) 
       print $1,$2" ==>",$3,$4  
      }' 
     ;; 

    2) 
     #"10 Byte < Memory Leak <= 50 Byte" 
     cat lost.txt | awk '{ 
      if ($1 <= 50 && $1 > 10) 
       print $1,$2" ==>",$3,$4  
      }' 
     ;; 

也就是說,根據所選擇的大小間隔,我可以打印值。但是,我找不到找到lostType的方法(上面的代碼爲$ 3)。我需要做的是尋找像

cat lost.txt | awk '{ 
if ($1 <= 50 && $1 > 10 AND IF $3 IS IN lostType.txt) 
     print $1,$2" ==>",$3,$4 
}' 
;; 

所以我的問題是在awk中的文件,我怎麼可以搜索裏面AWK文件?

+0

PLS。避免使用'cat file | awk ...'。改用'awk ... file'。 – TrueY

回答

1

如果我理解你的問題,以下awk命令應該工作。您必須提供兩個輸入文件。丟失的類型保存在散列中,並將鍵與另一個的第三個字段進行比較。

awk ' 
    FNR == NR { type[ $1 ] = 1; next } 
    $1 > 10 && $1 < 200 && type[ $3 ] { print $1,$2 " ==> " $3,$4 } 
' lostType.txt lost.txt 

可是這樣你會解析既爲每case選項。在我看來應該是一個更好的解決方案,將這些變量傳遞給awk命令並僅運行一次。

case $choice in 
1) 
    limit_inf=0 
    limit_sup=10 
    break 
    ;; 
2) 
    limit_inf=10 
    limit_sup=50 
    break 
    ;; 
... 

然後:

awk -v li="$limit_inf" -v ls="$limit_sup" ' 
    FNR == NR { type[ $1 ] = 1; next } 
    $1 > li && $1 < ls && type[ $3 ] { print $1,$2 " ==> " $3,$4 } 
' lostType.txt lost.txt