sed/awk 将多行合并为单行

问题描述 投票:0回答:1

我有包含以下内容的文件

ltm pool /Common/foo_mim_pool {
    members {
        /Common/mim-foo-010101-1:5222 {
            address 10.22.1.161
        }
    }
}
ltm pool /Common/foo_ts_pool {
    members {
        /Common/ts-vx-010101-1:6698 {
            address 10.20.1.68
        }
        /Common/ts-vx-010101-1:6699 {
            address 10.20.1.68
        }
        /Common/ts-vx-010101-2:6698 {
            address 10.21.1.199
        }
        /Common/ts-vx-010101-2:6699 {
            address 10.21.1.199
        }
    }
    monitor /Common/ts_monitor
}

我想将它们合并为单行,如下例所示,但看起来像是我无法理解的东西

ltm pool /Common/foo_mim_pool { members add { /Common/mim-foo-010101-1:5222 { address 10.22.1.161 } } monitor /Common/tcp}

ltm pool /Common/foo_ts_pool { members add { /Common/ts-vx-010101-1:6698 { address 10.20.1.68 } /Common/ts-vx-010101-1:6699 { address 10.20.1.68 } /Common/ts-vx-010101-2:6698 { address 10.21.1.199 } /Common/ts-vx-010101-2:6699 { address 10.21.1.199 } } monitor /Common/ts_monitor }

我第一次尝试使用以下命令,但它没有产生我想要的结果

paste -d " " - - - - - - - - < file.txt

awk 'NR%2{printf "%s ",$0;next;}1' file.txt

linux awk sed
1个回答
0
投票

这可能是您想要做的,使用任何 POSIX awk:

$ awk '{ORS=(/^}$/ ? RS : OFS); sub(/^[[:space:]]+/,"")} 1' file
ltm pool /Common/foo_mim_pool { members { /Common/mim-foo-010101-1:5222 { address 10.22.1.161 } } }
ltm pool /Common/foo_ts_pool { members { /Common/ts-vx-010101-1:6698 { address 10.20.1.68 } /Common/ts-vx-010101-1:6699 { address 10.20.1.68 } /Common/ts-vx-010101-2:6698 { address 10.21.1.199 } /Common/ts-vx-010101-2:6699 { address 10.21.1.199 } } monitor /Common/ts_monitor }
© www.soinside.com 2019 - 2024. All rights reserved.