簡體   English   中英

加入符合bash中特定條件的行

[英]Joining lines that matches specific conditions in bash

如果出現以下情況,我需要一個將連接線的命
- 以下行以超過5個空格開頭
連接線的長度不會超過79個字符
-those線不在具有pattern1和pattern2的行之間
- 與上面相同,但使用另一組模式,如pattern3和pattern4

它將在這樣的文件上工作:

 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,我不知道如何解決這個問題:)

這是一個開始:

#!/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
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM