Announcement

Collapse
No announcement yet.

BASH: remove multi-line regex pattern?

Collapse
This topic is closed.
X
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    BASH: remove multi-line regex pattern?

    I'm not really sure if this is possible or not, but I'm sure someone here will know!

    I have a file that contains text like this that has been produced by a script I wrote:

    Code:
    <LocationMatch "^/misc/favicon.ico.*">
    SecRuleRemoveById 960015
    SecRuleRemoveById 981203
    </LocationMatch>
    
    <LocationMatch "^/misc/form.js.*">
    </LocationMatch>
    I would like to remove empty LocationMatch statements like the second one. Is there a way to use regex to match multiple lines and remove them, i.e. search for <LocationMatch ".*">/n</LocationMatch> and remove it? From what I've read, grep & awk don't seem to be able to do this, as they process files one line at a time.

    If it's not possible with grep, awk etc. then what should I be using? Perl?
    samhobbs.co.uk

    #2
    Well, not sure yet how to do this; but I know sed, awk, perl, and tr can remove newline characters.

    Basically, you want:

    if line = "<LocationMatch...> and nextline = "</LocationMatch..."
    then remove both lines (and maybe a third - empty line?).

    Does that describe what you want? No chance your script can be improved to do this by itself?

    Please Read Me

    Comment


      #3
      sed can surely do this, you can stash stuff in its hold space and print it when you know you want it. But, the skill is arcane, not worth learning IMHO. Better to learn a more easily understood tool like awk, and use somewhat more easily understood code:
      Code:
      awk '/^<LocationMatch/ { getline following
                          if ( following !~ "</LocationMatch" ) {
                              print
                              print following }
                          next }
                        { print }'  [I]file[/I]
      BTW, If your file is small, say up to a few megabytes, here's a vim one liner:
      :g/<LocationMatch.*>\n<\/LocationMatch>/d 2
      Regards, John Little

      Comment


        #4
        Thank you both for the replies

        Originally posted by oshunluvr View Post
        if line = "<LocationMatch...> and nextline = "</LocationMatch..."
        then remove both lines (and maybe a third - empty line?).

        Does that describe what you want?
        Yep that's it!

        No chance your script can be improved to do this by itself?
        After posting I actually managed to edit the script so that it checks if the statement would be empty before printing it, and doesn't print it if it is.

        Still, I think removing multiple lines is an interesting question, and something worth learning how to do for the future!

        John, that's a nice piece of code, thank you. Easily understood, too!
        samhobbs.co.uk

        Comment

        Working...
        X