2012-07-17 70 views
2

我有一個看起來像這樣的文本文件。我想提取「A」和「E」字符的總數。使用awk從文本文件中提取特定字符的數量

>pr1 
FSVSQNNPAE 
>pr2 
MAKERAHSQ 
>pr3 
RRRDKINNWIVQL 

我想獲得這樣的

>pr1 
Total number of A - 1 
Total number of E - 1 

>pr2 
Total number of A – 2 
Total number of E - 1 

>pr3 
Total number of A – 0 
Total number of E – 0 

我怎樣才能做到這一點使用awk輸出?

回答

4

單程。當找到以>開頭的行時,請閱讀下一行,將其保存在str變量中,並計算每個字母的替換次數。

awk ' 
    $1 ~ /^>/ { 
     getline str 
     num_a = gsub(/A/, "", str) 
     num_e = gsub(/E/, "", str) 
     printf "%s\nTotal number of A - %d\nTotal number of E - %d\n\n", $0, num_a, num_e 
    } 
' infile 

輸出:

>pr1                                                           
Total number of A - 1                                                       
Total number of E - 1                                                       

>pr2                                                           
Total number of A - 2                                                       
Total number of E - 1                                                       

>pr3                                                           
Total number of A - 0                                                       
Total number of E - 0 
+0

或多或少相同的想法,但沒有,則對getline ... – 2012-07-17 12:26:56

+0

@Birei非常感謝你。 – user1510708 2012-07-17 14:40:10

3

UPDATE:這將在飛行中改變FSfield seperator)工作:

{ 
    if ($0 ~ /^>/) 
    printf("\n%s\n", $0); 
    else 
    { 
    FS="A" 
    nl = $0; 
    $0 = nl; 
    print "Total number of A - ", NF-1; 

    FS="E" 
    $0 = nl; 
    print "Total number of E - ", NF-1; 
    } 
} 

給出:

>pr1 
Total number of A - 1 
Total number of E - 1 

>pr2 
Total number of A - 2 
Total number of E - 1 

>pr3 
Total number of A - 0 
Total number of E - 0 

以前的解決方案

{ 
    if ($1 ~ /^>/) 
    printf("\n%s\n", $0) 
    else 
    { 
    print "total number of A - ", gsub(/A/,"A") 
    print "total number of E - ", gsub(/E/,"E") 
    } 
} 

類似@ Birei的

+0

改變FS的思想是什麼? – 2012-07-17 12:50:22

+0

@KarlNordström工作解決方案張貼..我*知道*這是可能的'FS' ..只是得到正確的序列。 – Levon 2012-07-17 13:16:07

+0

@ user1510708工作解決方案已發佈(更新) – Levon 2012-07-17 13:17:43

相關問題