Live data from Hacker News

Show HN: Choose – An alternative to cut and sometimes awk

github.com

31–40 of 49 posts

Re: Show HN: Choose – An alternative to cut and sometimes awk

#31
post #29

Earlier quoted context omitted.

Agreed. In fact, anybody promoting cut, please give me the cut version of: echo -e "foo bar baz" | choose -1 -2 It should work on an arbitrary number of spaces, and fields. The oneliner is going to be... interesting. Now you can do it with awk using: echo -e "foo bar baz" | awk '{ print $NF " " $(NF-1)}' But it's neither easy to type, nor to remember. Choose is what cut should have been.

By using only basic functionality that's easy enough to remember I guess I'd go with something like `echo -e "foo bar baz" | tr -s ' ' | rev | cut -d ' ' -f 1-2 | rev | awk '{print $2 " " $1}` Everything except the awk part is something that I use all the time and is easy to type & remember. To be honest I'd use `choose` if it was available everywhere, but for string manipulation I can't justify using nonstandard too…

[deleted]

Re: Show HN: Choose – An alternative to cut and sometimes awk

#32
Can it output fields in an order other than their input order? That's the one thing I regularly wish cut could do. I would like the output of the second cut below to be "3,1", not "1,3".

  $ echo "1,2,3,4,5" | cut -d , -f 1,3
  1,3
  $ echo "1,2,3,4,5" | cut -d , -f 3,1
  1,3

Re: Show HN: Choose – An alternative to cut and sometimes awk

#33
post #32

Can it output fields in an order other than their input order? That's the one thing I regularly wish cut could do. I would like the output of the second cut below to be "3,1", not "1,3". $ echo "1,2,3,4,5" | cut -d , -f 1,3 1,3 $ echo "1,2,3,4,5" | cut -d , -f 3,1 1,3

    awk -F, '{print $3","$1}'

Re: Show HN: Choose – An alternative to cut and sometimes awk

#34
I am not sure why being zero-indexed is considered a feature. I have no problem using a zero-indexed system, but I've never really thought of it as a feature. Is there something I'm missing that makes zero-indexed systems faster, easier to use or otherwise better than one-indexed system?

Re: Show HN: Choose – An alternative to cut and sometimes awk

#35
post #32

Can it output fields in an order other than their input order? That's the one thing I regularly wish cut could do. I would like the output of the second cut below to be "3,1", not "1,3". $ echo "1,2,3,4,5" | cut -d , -f 1,3 1,3 $ echo "1,2,3,4,5" | cut -d , -f 3,1 1,3

Yes:

  $ echo "1,2,3,4,5" | choose -f , 2 0
  3 1
  $ echo "1,2,3,4,5" | choose -f , 2:0
  3 2 1
Note that the indexing starts with 0, "-d" is "-f", and a range is denoted by ":" instead of "-" which is used for indexing from the end.

Re: Show HN: Choose – An alternative to cut and sometimes awk

#36

I am not sure why being zero-indexed is considered a feature. I have no problem using a zero-indexed system, but I've never really thought of it as a feature. Is there something I'm missing that makes zero-indexed systems faster, easier to use or otherwise better than one-indexed system?

There's a better reason than this that I'm forgetting, but never underestimate the power of being the same as what people are already familiar with. Every time I have to write lua or read some Matlab, the mental overhead of having to remember everything is one-indexed is just incredibly annoying.

Re: Show HN: Choose – An alternative to cut and sometimes awk

#37
Inspired by a remark about Python's default split behavior in comment https://news.ycombinator.com/item?id=23446146 I wrote a Python oneliner for field selection works similar to "choose" but throws exceptions when the field cannot be found:

  $ echo " a    b c" | choose 1 2
  b c
  $ echo " a    b c" | python3 -c 'import sys; [print(f[1], f[2]) for line in sys.stdin if (f := line.split()) or True]'
  b c

Re: Show HN: Choose – An alternative to cut and sometimes awk

#39
I love my coreutils replacements...not that they're in Rust but because they're generally faster and easier to use. fd, rg, bat and now i shall use choose! I almost always have to lookup awk's syntax but the defaults in choose seem trivial to remember. Thanks for making this!

Re: Show HN: Choose – An alternative to cut and sometimes awk

#40
I think it is very cool that Rust has led to a renaissance of rewriting classic Unix tools to make them fit more with current use. Unix was never meant to stand still. It just happened that AT&T broke up and it took a while for Linux to catch up, and by then people got used to the idea of a fixed set of POSIX utilities. But their CLIs are often quite bad, and security was never a consideration in the olden days, so it’s good see them re-evaluated.
Post reply on HN