2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

XMLファイルから属性を抽出する方法

Posted at

パスワードなどの変更される可能性の高いものや、ファイルに直書きしたくないものをXMLにまとめて、欲しいものだけ取り出したい!

xmllintとsedを組み合わせて取得できました。

xmlファイルの例

<?xml version="1.0" encoding="utf-8"?>
<piyo>
    <username value=piyo@mail.com/>
    <password value=piyo123/>
</piyo>

xmllintとsedで抽出

属性の抽出はxpathで@を使うと簡単に取れました。

xmllint --xpath "/piyo/username/@value" env.xml | sed -e "s/ value=\"\([^\"]*\)\"/\1\n/g"

結果↓

piyo@mail.com

ちなみに後半のsedなしだと

xmllint --xpath "/piyo/username/@value" env.xml

こうなります↓

value=piyo@mail.com

たくさんある時はfunctionにまとめると便利ですね!

2
1
0

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
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?