Grep只有一行,然后删除

Dan*_*Dan 2 xml bash awk grep sed

我想知道一个命令,只提取该文件第8行的值,减去<string></string>,换句话说只输出3.2.2

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>BuildVersion</key>
    <string>8</string>
    <key>CFBundleShortVersionString</key>
    <string>3.2.2</string>
    <key>CFBundleVersion</key>
    <string>399.12</string>
    <key>ProjectName</key>
    <string>ServerApp</string>
    <key>SourceVersion</key>
    <string>399012000000000</string>
</dict>
</plist>
Run Code Online (Sandbox Code Playgroud)

非常感谢您的建议!谢谢,丹

Gil*_*not 9

正如Steven Penny和链接RegEx匹配除XHTML自包含标记之外的开放标记所述,要解析XML,需要一个合适的xml解析器,其中一个是

$ xmllint --xpath '/plist/dict/string[2]/text()' file.xml
Run Code Online (Sandbox Code Playgroud)

或者使用:

$ xmlstarlet sel -t -v '/plist/dict/string[2]/text()' file.xml
Run Code Online (Sandbox Code Playgroud)

或者使用撒克逊棉绒:

$ saxon-lint --xpath '/plist/dict/string[2]/text()' file.xml
Run Code Online (Sandbox Code Playgroud)

如果你想要以下的版本号,还有一个更好的XPath表达式CFBundleShortVersionString:

'//key[text()="CFBundleShortVersionString"]/following-sibling::string[1]/text()'
Run Code Online (Sandbox Code Playgroud)