Sed or perl + insert text between two lines

I have the following file: (example.txt showing down)

I need to edit the file, the main problem is to add text between two known lines in the file

first_line = ")"

second_line = "NIC Hr_Nic ("

for instance

The following should be added:

    haattr -add RVG StorageRVG -string
    haattr -add RVG StorageDG -string
    haattr -add RVG StorageHostIds -string
    haattr -delete RVG Primary
    haattr -delete RVG SRL
    haattr -delete RVG RLinks

Between

The first line of matching ")"

AND

second string matching "NIC Hr_Nic ("

As described in example.txt

How to do it with sed ... (If it is difficult to do sed, it is also possible with perl)

note (sed you need to get two arguments, the first argument is the first line of correspondence (first_line arg) the second argument is the second line of correspondence (second_line arg)

file example.txt:

    group Hr_Grp (
    SystemList = { london1 = 0, london2 = 1 }
    AutoStartList = { london1, london2 }
    )

    NIC Hr_Nic (
            Device = qfe0
            )

    IP Hr_Ip(
            Device = qfe0
            Address = "1.1.1.1"         // Virtual IP
            )

    DiskGroup Hr_Dg(
            DiskGroup = hrdg
            )

    RVG Hr_Rvg (
            RVG = hr_rvg
            DiskGroup = hrdg
            )

    Hr_Rvg requires Hr_Dg
    Hr_Rvg requires Hr_Ip
    Hr_Ip requires Hr_Nic

Example file after editing sed:

    group Hr_Grp (
    SystemList = { london1 = 0, london2 = 1 }
    AutoStartList = { london1, london2 }
    )


    haattr -add RVG StorageRVG -string
    haattr -add RVG StorageDG -string
    haattr -add RVG StorageHostIds -string
    haattr -delete RVG Primary
    haattr -delete RVG SRL
    haattr -delete RVG RLinks



    NIC Hr_Nic (
            Device = qfe0
            )

    IP Hr_Ip(
            Device = qfe0
            Address = "1.1.1.1"         // Virtual IP
            )

    DiskGroup Hr_Dg(
            DiskGroup = hrdg
            )

    RVG Hr_Rvg (
            RVG = hr_rvg
            DiskGroup = hrdg
            )

    Hr_Rvg requires Hr_Dg
    Hr_Rvg requires Hr_Ip
    Hr_Ip requires Hr_Nic
+3
1

:

text=$(<file)
sed -e '/[[:blank:]]*)[[:blank:]]*/{:a;n;/NIC Hr_Nic (/i\' -e "$text" -e 'ba}'

i , r (read file) . :

read -d '' -r text<<EOF
haattr -add RVG StorageRVG -string
haattr -add RVG StorageDG -string
etc.
EOF

text="haattr -add RVG StorageRVG -string
haattr -add RVG StorageDG -string
etc."
0

Source: https://habr.com/ru/post/1759118/


All Articles