LoginSignup
6

More than 5 years have passed since last update.

ShellScriptとxmllintでxmlを解析

Posted at

ShellScriptド素人メモ
xmllintを使って以下のxmlを解析して値&属性値を取り出す

sample.xml
<?xml version='1.0' encoding='utf-8' ?>
<top>
    <int name="hoge" value="327">100</int>
</top>

属性値を出力

327を取り出す場合、以下のようにする

echo 'cat /top/int/@value' | xmllint --shell sample.xml | grep value= | sed 's/[^"]*"\([^"]*\)"[^"]*/\1/g'

要素の内容を取得

100を取り出す場合、以下のようにする

echo 'cat /top/int' | xmllint --shell sample.xml | grep value= | sed 's/^.*<int.*>\(.*\)<\/int>.*$/\1/' 

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6