Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grep just one line, then remove

Tags:

grep

bash

xml

sed

awk

I'd like to know a command to extract just the value from line 8 of this file, minus the <string> and </string>, in other words output only 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>

Your suggestions are much appreciated! Thanks, Dan

like image 666
Dan Avatar asked Mar 14 '26 03:03

Dan


1 Answers

As stated by Steven Penny and the link RegEx match open tags except XHTML self-contained tags, to parse XML, a proper xml parser is required, one of them is xmllint

$ xmllint --xpath '/plist/dict/string[2]/text()' file.xml

or with xmlstarlet :

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

or with saxon-lint :

$ saxon-lint --xpath '/plist/dict/string[2]/text()' file.xml

And an even better XPath expression if you want the version number after CFBundleShortVersionString :

'//key[text()="CFBundleShortVersionString"]/following-sibling::string[1]/text()'
like image 145
Gilles Quenot Avatar answered Mar 16 '26 17:03

Gilles Quenot



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!