开发者

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 开发者_如何学JAVAuri="endpoint.php" class="flex.messaging.endpoints.AMFEndpoint"/> -->

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

Thx, martin


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.


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜