Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
jappaleinen opened this issue Aug 10, 2023 · 2 comments
Open

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

jappaleinen opened this issue Aug 10, 2023 · 2 comments
Assignees
Labels
enhancement New feature or request

Comments

@jappaleinen
Copy link

jappaleinen commented Aug 10, 2023

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 jappaleinen added the enhancement New feature or request label Aug 10, 2023
@sibprogrammer
Copy link
Owner

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

@jappaleinen
Copy link
Author

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...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants