2012-11-30 61 views
1

我想知道是否有可能使用grep找出所有在以下兩個字符串之間的文本:grep命令古怪的字符串

mutablePath = CGPathCreateMutable(); 
... 
CGPathAddPath(skinMutablePath, NULL, mutablePath); 

基本上,第一行和最後一行將始終是相同的,之間會有一大堆隨機的東西。我想要計算從上面第一行和最後一行的所有實例之間出現的行數。

這甚至可能嗎?

回答

1

你不能用grep做到這一點,但你可以awk做到這一點。這是完全未經測試,但應該工作:

awk 'BEGIN { state = 0; count = 0; } 
    /^mutablePath = CGPathCreateMutable();$/ { state = 1; } 
    /^CGPathAddPath(skinMutablePath, NULL, mutablePath);$/ 
     { print count; state = 0; count = 0 } 
    { if (state) count++; }' FILE_OF_INTEREST 
1

下面是一個awk解決方案,如果你有機會獲得,除了grep

awk '/^mutablePath = CGPathCreateMutable\(\)\;$/ {in_block=1} 
    in_block==1 {count++} 
    /^CGPathAddPath\(skinMutablePath, NULL, mutablePath\)$/ {in_block==0; count--} 
    END{print count}' input 
2

下面是另一個awk解決方案:

awk '/^mutablePath = CGPathCreateMutable\(\);$/ { m=1; c=0 } 
    /^CGPathAddPath\(skinMutablePath, NULL, mutablePath\);$/ { print c-1; m=0 } 
    m { c++ }' file