Dasel: Difference between revisions

From wikinotes
No edit summary
 
(6 intermediate revisions by the same user not shown)
Line 57: Line 57:
See https://github.com/TomWright/dasel-docs/blob/master/examples/xml.md
See https://github.com/TomWright/dasel-docs/blob/master/examples/xml.md


Query
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
# replace the contents of the LAST <configuration><device>...</devic></configuration> with <a>1</a>
# COUNT number of matching elements
cat config.xml | dasel put -r xml -v '<a>1</a>' '.configuration.device'
cat config.xml \
  | dasel -r xml '.configuration.device.len()'
 
# QUERY property value (ex. <device id="foo">)
cat config.xml \
  | dasel -r xml '.configuration.device.[0].-id'
 
# FILTER devices, with a 'name' property, that equals 'mordin'
# (ex. <device name="mordin"></device>...<device name="foo"></device>
cat config.xml \
  | dasel -r xml '.configuration.device.all().filter(equal(-name,mordin))'
 
# COUNT devices, with a 'name' property, that equals 'mordin'
# (ex. <device name="mordin"></device>...<device name="foo"></device>
cat config.xml \
  | dasel -r xml '.configuration.device.all().filter(equal(-name,mordin)).count()'
</syntaxhighlight>
 
Mutation
<syntaxhighlight lang="bash">
# REPLACE the contents of the LAST
# <configuration><device>...</device></configuration> with <a>1</a>
cat config.xml \
  | dasel put -r xml -v '<a>1</a>' \
  '.configuration.device'
 
# REPLACE the contents of the FIRST
# <configuration><device>...</device></configuration> with <a>1</a>
cat config.xml \
  | dasel put -r xml -v '<a>1</a>' \
    '.configuration.device.[0]'
 
# REPLACE the contents of the MATCHING element
cat config.xml \
  | dasel put -r xml -v '<foo></foo>' \
    '.configuration.device.all().filter(equal(-name,mordin))'
 
# APPEND a new element
cat config.xml \
  | dasel put -r xml -v '<a>1</a>' \
  '.configuration.device.append()'
</syntaxhighlight>
</syntaxhighlight>
</blockquote><!-- XML -->
</blockquote><!-- XML -->
</blockquote><!-- File Formats -->
</blockquote><!-- File Formats -->

Latest revision as of 05:29, 29 January 2023

Inspired by jq and yq, but operates on json, yaml, toml, xml, csv.
Totally standalone, no runtime dependencies.

I'm not certain you can append to a list,

Documentation

official docs https://daseldocs.tomwright.me/
github https://github.com/TomWright/dasel

Examples

# modify 'foo.yml', adding obj['category']['three'] = "3"
dasel put string \
    -f foo.yml \
    '.category.three'  '3'

# print 'foo.yml',
#   with obj['category']['three'] = "3" added,
#   while also preserving comments
#
# see: https://stackoverflow.com/questions/14282617/hunk-1-failed-at-1-whats-that-mean
diff \
    <(dasel -f foo.yml) \
    <(dasel put string -f foo.yml -o - '.category.three' '3') \
    | patch -o - foo.yml

Datatypes

string:    string
int:       integer
bool:      true/false
object:    a set of key/value pairs (can be assigned to objects of various types)
document:  a json/yaml/toml/... object (can be put into documents of other types)

File Formats

CSV

cat out.csv | dasel -p csv -m '.-'

XML

See https://github.com/TomWright/dasel-docs/blob/master/examples/xml.md

Query

# COUNT number of matching elements
cat config.xml \
  | dasel -r xml '.configuration.device.len()'

# QUERY property value (ex. <device id="foo">)
cat config.xml \
  | dasel -r xml '.configuration.device.[0].-id'

# FILTER devices, with a 'name' property, that equals 'mordin'
# (ex. <device name="mordin"></device>...<device name="foo"></device>
cat config.xml \
  | dasel -r xml '.configuration.device.all().filter(equal(-name,mordin))'

# COUNT devices, with a 'name' property, that equals 'mordin'
# (ex. <device name="mordin"></device>...<device name="foo"></device>
cat config.xml \
  | dasel -r xml '.configuration.device.all().filter(equal(-name,mordin)).count()'

Mutation

# REPLACE the contents of the LAST
# <configuration><device>...</device></configuration> with <a>1</a>
cat config.xml \
  | dasel put -r xml -v '<a>1</a>' \
   '.configuration.device'

# REPLACE the contents of the FIRST
# <configuration><device>...</device></configuration> with <a>1</a>
cat config.xml \
  | dasel put -r xml -v '<a>1</a>' \
    '.configuration.device.[0]'

# REPLACE the contents of the MATCHING element
cat config.xml \
  | dasel put -r xml -v '<foo></foo>' \
    '.configuration.device.all().filter(equal(-name,mordin))'

# APPEND a new element
cat config.xml \
  | dasel put -r xml -v '<a>1</a>' \
   '.configuration.device.append()'