xq icon indicating copy to clipboard operation
xq copied to clipboard

Add new command line flag -ds / --document-separators

Open jappaleinen opened this issue 2 years ago • 2 comments

Problem Statement

When sending multiple documents to the command line, the documents are not separated with line feeds. In fact, line-feeds between documents are removed. One use case when working with multiple xml documents streamed to console is to get each document on a single line, allowing to pipe result to standard command line tools of interest such as simple grep for selecting / filtering documents.

Adding a new flag -ds / --document-separators with a numeral 0-n (default 0 for backwards compatibility) in a similar way as --indent is currently working.

Expected Result

# Current
➜ echo $'  <element/>  <element/>  ' | xq
<element/><element/>

# Expected
➜ echo $'  <element/>  <element/>  ' | xq
<element/><element/>

➜ echo $'  <element/>  <element/>  ' | xq -ds 1
<element/>
<element/>

➜  echo $'  <element/>  <element/>  ' | xq -ds 2
<element/>

<element/>
# Current
➜ echo $'  <?xml version="1.0"?><element></element>  \
  <?xml version="1.0"?>  <element/>  ' | xq
<?xml version="1.0"?>
<element/><?xml version="1.0"?>
<element/>

# Expected
➜ echo $'  <?xml version="1.0"?><element></element>  \
  <?xml version="1.0"?>  <element/>  ' | xq -ds 1
<?xml version="1.0"?>
<element/>
<?xml version="1.0"?>
<element/>

➜ echo $'  <?xml version="1.0"?><element></element>  \
  <?xml version="1.0"?>  <element/>  ' | xq -ds 2
<?xml version="1.0"?>
<element/>

<?xml version="1.0"?>
<element/>

jappaleinen avatar Aug 10 '23 09:08 jappaleinen

In the case of multiple separate files the outer for loop should do the trick:

for FILE in test/data/xml/unformatted*
do
    cat $FILE | xq
    echo "---separator---"
done

sibprogrammer avatar Aug 17 '23 19:08 sibprogrammer

Yes, in the case you send a single document to the xq command every time and are in control of the document separation outside of the command invocation this can be solved outside the xq tool.

However, if eg a file contains multiple xml-documents (don't ask why, but we have a scenario where this is the case) its harder.

Therefore, a suggestion to be able to instruct xq to separate documents in the the output...

jappaleinen avatar Aug 28 '23 13:08 jappaleinen