2011-03-11 70 views
0

匹配特定條件的接合線我需要一個命令,如果將加入行:
-following符合多於5個空格開始
-length接合線將不大於79個字符
-those線不是如上
-same與模式1和模式2線之間,但與另一組的模式,如pattern3和pattern4即在bash

它會在這樣一個文件的工作:

Long line that contains too much text for combining it with following one 
That line cannot be attached to the previous becouse of the length 
This one also 
becouse it doesn't start with spaces 

This one 
    could be 
    expanded 

pattern1 
here are lines 
    that shouldn't be 
    changed 
pattern2 

Another line 
    to grow 

運行命令後,輸出應該是:

Long line that contains too much text for combining it with following one 
That line cannot be attached to the previous becouse of the length 
This one also 
becouse that one doesn't start with spaces 

This one could be expanded 

pattern1 
here are lines 
    that shouldn't be 
    changed 
pattern2 

Another line to grow 

它不能移動線的一部分。

我使用bash 2.05 SED 3.02 AWK 3.1.1和grep 2.5.1,我不知道如何解決這個問題:)

回答

2

這裏是一個開始你:

#!/usr/bin/awk -f 
BEGIN { 
     TRUE = printflag1 = printflag2 = 1 
     FALSE = 0 
} 

# using two different flags prevents premature enabling when blocks are 
# nested or intermingled 
/pattern1/ { 
     printflag1 = FALSE 
} 

/pattern2/ { 
     printflag1 = TRUE 
} 
/pattern3/ { 
     printflag2 = FALSE 
} 

/pattern4/ { 
     printflag2 = TRUE 
} 

{ 
     line = $0 
     sub(/^ +/, " ", line) 
     sub(/ +$/, "", line) 
} 

/^ /&& 
    length(accum line) <= 79 && 
    printflag1 && 
    printflag2 { 
     accum = accum line 
     next 
} 

{ 
     print accum 
     accum = line 
}