Sunday, January 28, 2024

[SOLVED] sed/awk to merge multiple line in single line

Issue

I have file which contain following

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
}

I want to merge them in single line like following example but look like something i missing to understand

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

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 }

My first attempt to use following command but its not producing what I want

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

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


Solution

This might be what you're trying to do, using any 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 }


Answered By - Ed Morton
Answer Checked By - Mary Flores (WPSolving Volunteer)