我需要从此 xml 中提取名称值(产品查找器):

文件: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> 

我试过:

mles$ cat config.xml | grep '<name>' 
    <name>Product Finder</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 | sed "s@.*<name>\(.*\)</name>.*@\1@" 

什么 sed这里是采取<name></name>之间的一切并用标签之间找到的文本替换整行


评论关闭
IT序号网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!