从mac中的bash中的xml中提取值 [英] Extract value from xml in bash on a mac

查看:314
本文介绍了从mac中的bash中的xml中提取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从这个xml中提取名称值(Product Finder):

File:config.xml

 <?xml version =1.0encoding =utf-8?> 
< widget id =com.abc.appversion =1.3.1xmlns =http://www.w3.org/ns/widgetsxmlns:android =http:// schemas .android.com / apk / res / androidxmlns:cdv =http://cordova.apache.org/ns/1.0ios-CFBundleVersion =1.3.1.5android-versionCode =5>
<名称>产品搜寻器< /名称>
< description>
说明
< / description>
< / widget>

我试过了:

  mles $ cat config.xml | grep'< name>'
<名称>产品搜寻器< /名称>

其他答案建议使用 grep -oPm1(?< =< ; xmltag>)[^<] +,但会产生错误:

  mles $ cat config.xml | grep -oPm1(?< =< name> [^<] +
usage:grep [-abcDEFGHhIiJLlmnOoqRSsUVvwxZ] [-A num] [-B num] [-C [num]]
[-e pattern] [-f file] [--binary-files = value] [--color = when]
[--context [= num]] [--directories = action] [ --label] [--line-buffered]
[--null] [pattern] [file ...]

如何获取名称值?我需要一个没有依赖关系的解决方案,所以 grep 将会是首选的解决方案

grep 只能找到该行,您必须使用其他工具来提取名称,例如 sed (不是额外的依赖项):

  grep'< name>'config.xml | seds @。*< name> \(。* \)< / name>。* @ \1 @

这里的 sed < name>< / name> ,并将整行替换为标签之间找到的文本


I need to extract the name value (Product Finder) from this xml:

File: config.xml

<?xml version="1.0" encoding="utf-8"?>
<widget id="com.abc.app" version="1.3.1" xmlns="http://www.w3.org/ns/widgets" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:cdv="http://cordova.apache.org/ns/1.0" ios-CFBundleVersion="1.3.1.5" android-versionCode="5">
    <name>Product Finder</name>
    <description>
        Description
    </description>
</widget>

I've tried:

mles$ cat config.xml | grep '<name>'
    <name>Product Finder</name>

Some other answers suggest using grep -oPm1 "(?<=<xmltag>)[^<]+", but that yields an error:

mles$ cat config.xml | grep -oPm1 "(?<=<name>)[^<]+"
usage: grep [-abcDEFGHhIiJLlmnOoqRSsUVvwxZ] [-A num] [-B num] [-C[num]]
[-e pattern] [-f file] [--binary-files=value] [--color=when]
[--context[=num]] [--directories=action] [--label] [--line-buffered]
[--null] [pattern] [file ...]

How can I get the name value? I need a solution without dependencies, so grep would be preferred

解决方案

grep only finds the line, you have to use an additional tool to extract the name, like sed (not an additional dependency):

grep '<name>' config.xml | sed "s@.*<name>\(.*\)</name>.*@\1@"

What sed does here is takes everything between <name></name> and substitutes the whole line with the found text between the tags

这篇关于从mac中的bash中的xml中提取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆