bashawksedemcmultipath

How to delete lines before and after matching pattern


i have a file like below. I want to search pattern ABC_DATA as a variable and after match, i want to delete from first previous "multipath" line to the line including "}" character. "}" could be after or in the same line with ABC_DATA.

SAMPLE OUTPUT

multipaths {
multipath {
wwid 360000970000267600432533030353944
alias ABC_DATA_11
}
multipath {
wwid 360000970000267600432533030353945
alias DEF_DATA_11
}
multipath {
wwid 360000970000267600432533030353946
alias ABC_DATA_12 }

multipath {
wwid 360000970000267600432533030353943
alias DEF_DATA_10
}
}

EXPECTED OUTPUT

multipaths {
multipath {
wwid 360000970000267600432533030353945
alias DEF_DATA_11
}
multipath {
wwid 360000970000267600432533030353943
alias DEF_DATA_10
}
}

Solution

  • $ awk -v RS='multipath\\s*{[^}]*ABC_DATA[^}]*}\\s*(\n|$)' -v ORS= '1' file
    multipaths {
    multipath {
    wwid 360000970000267600432533030353945
    alias DEF_DATA_11
    }
    multipath {
    wwid 360000970000267600432533030353943
    alias DEF_DATA_10
    }
    }