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

'plain' output with a defined field-separator #57

Closed
Jamie-Landeg-Jones opened this issue Dec 30, 2017 · 9 comments
Closed

'plain' output with a defined field-separator #57

Jamie-Landeg-Jones opened this issue Dec 30, 2017 · 9 comments

Comments

@Jamie-Landeg-Jones
Copy link

Jamie-Landeg-Jones commented Dec 30, 2017

Hi. I notice that the output from "df" is tricky to parse, when the mount "device" and/or the mount-point contain spaces.. (yeah, i know... don't ask!!! )

"mount -p" uses (one or more) tab characters to separate fields, UNLESS the field is over 24 character in which case it uses space. (arrrrgh)

I see that the idea behind libxo is to enable processing of data, by not having to try and parse human-readable output. This is great, and just what I'd want.

'df' does indeed have libxo functionality, and whilst this would do the trick, parsing xml or json is overkill.
I was hoping it would be possible to have something like:

--libxo=delimited,delimiter="",record-separator=""

e.g.

--libxo=delimited,delimiter="\0",record-separator="\n"

or maybe being able to pass a format string in printf format?

Is this kind of thing generally useful?

In my particular case, I ended up writing a small C function just to grab the mount "device" and mount-point I needed, but it seemed to me that this what exactly the sort of thing libxo was meant to solve.

Am I missing something?

Cheers, Jamie

@philshafer
Copy link
Contributor

philshafer commented Jan 12, 2018 via email

@philshafer
Copy link
Contributor

philshafer commented Oct 25, 2019

I've been thinking about this and realized that with a little work, I can make a "csv" encoder that does what you need. Sort of. If I'm seeing what you need correctly.

So imagine:

  • a customer "csv" encoder
  • the ability to pass "options" to encoders
  • the csv encoder supports three options:
    • path=some/path/in/the/hierarchy (emit only content under this specific hierarchy)
    • leafs=some.leafs.that.you.want (emit only leafs "some", "leafs", "that", "you", and "want")
    • no-header (so avoid the normal CSV header)

With no explicit "path", it emits all "lists". With no explict "leafs", it emits the set of fields under the first instance of the first list. Without the no-header, it emits a list of leafs that it's emitting.

Using the example of tests/core/test-01.c:

% test_01.test --libxo warn,encoder=csv:path=top/data/item
sku,name,sold,in-stock,on-order
GRO-000-415,gum,1412,54,10
HRD-000-212,rope,85,4,2
HRD-000-517,ladder,0,2,1
HRD-000-632,bolt,4123,144,42
% test_01.test --libxo warn,encoder=csv:path=top/data/item+leafs=name.sold
name,sold
gum,1412
rope,85
ladder,0
bolt,4123
water,17

So add the "no-header" to strip the header and "leafs=xxx" and you can trim the content to what you need.

Does that sounds reasonable and usable?

Check out commit a8e2070 (and the one before that). To see where I'm at. It's mostly coded, but needs some finishing touches and documentation.

Thanks,
Phil

@Jamie-Landeg-Jones
Copy link
Author

Apologies for the delay in replying.

That's brilliant - better than what I was hoping for (the ability to filter out leafs)

Hopefully this will get libxo added to more utilities - this is much better for shell scripts than parsing human orientated output!

I notice you've committed this to FreeBSD-Current. Are there plans to MFC to 12-stable?

Thanks again, Jamie

@philshafer
Copy link
Contributor

philshafer commented Nov 11, 2019 via email

@philshafer
Copy link
Contributor

philshafer commented Nov 11, 2019 via email

@Jamie-Landeg-Jones
Copy link
Author

Ha! You must have been terrible as a kid leading up to Christmas, and presents!

As for freebsd, I'm on 12-stable, I just installed the latest from git (seeing as you'd released it by the time I got around to it). It installed in /usr/local/... and seems ok to me (df --libxo=encoders=csv works fine)

I used your install instructions - I did have to use "gmake" rather than "make" to get it to install though.

I couldn't get "xo" command line to work with the csv encoder, but I haven't really used that command before, so put that down to my own stupidity!

I am a bit puzzled by why it's --html/--xml/--json/--text/--encoder=csv and not --encoder=html/xml/json/text/csv but it's not important!

By the way, talking of Christmas, I don't want to be the guy who tells you that santa doesn't exist.... but apparently, the rail-gauge size is a myth! https://www.snopes.com/fact-check/railroad-gauge-chariots/

@philshafer
Copy link
Contributor

philshafer commented Nov 13, 2019 via email

@philshafer
Copy link
Contributor

philshafer commented Nov 13, 2019 via email

@philshafer
Copy link
Contributor

The functionality has been added to the current libxo release.

Thanks,
Phil

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

No branches or pull requests

2 participants