Wednesday, April 6, 2011

Write a cmd line script to parse an xml file

What would be the best solution to check (from the command line with a script), if a certain xml file contains this line:

<endpoint uri="endpoint.php" class="flex.messaging.endpoints.AMFEndpoint"/>

or this line

<!-- <endpoint uri="endpoint.php" class="flex.messaging.endpoints.AMFEndpoint"/> -->

and stop execution if the second one (commented out) is found?

Thx, martin

From stackoverflow
  • Single line or across multiple lines? If the former, you can use grep.

    Update: There seem to be some XML aware variants like xgrep, xmltwig and xmlstarlet.

    Maxim Veksler : +1 for xmlstarlet
  • assuming pattern occurs at single line

    #!/bin/bash
    awk '
    /<endpoint uri=.*endpoint.php.*class.*flex.messaging.endpoints.AMFEndpoint/ && /<!--/{
        exit
    }
    /<endpoint uri=.*endpoint.php.*class.*flex.messaging.endpoints.AMFEndpoint/{
        # to execute external script inside awk, uncomment below
        #cmd = "myscript.sh"
        #system(cmd)
    } 
    ' file
    

    OR you can return a code back to shell

    #!/bin/bash
    var=$(awk '
    /<endpoint uri=.*endpoint.php.*class.*flex.messaging.endpoints.AMFEndpoint/ && /<!--/{
        print 1
    }
    /<endpoint uri=.*endpoint.php.*class.*flex.messaging.endpoints.AMFEndpoint/{
        print 0
    } 
    ' file)
    [ "$var" -eq 1 ] && exit
    [ "$var" -eq 0 ] && ./myscript.sh
    
    Noufal Ibrahim : These are not totally reliable. A node called
    ghostdog74 : its OP's sample, not mine. If there is possibility of "s" in endpoint, then matching it exactly will do.
    martin : this looks good! It is sufficient how it is checked. I am trying to execute a script in the second case... just putting a command in there does not work for me e.g. (./doSomething.sh) do I need to use exec()?

0 comments:

Post a Comment