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…
Show HN: Choose – An alternative to cut and sometimes awk
31–40 of 49 posts
Re: Show HN: Choose – An alternative to cut and sometimes awk
#32 $ 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,3Re: Show HN: Choose – An alternative to cut and sometimes awk
#33Can 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
#34Re: Show HN: Choose – An alternative to cut and sometimes awk
#35Can 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
$ 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
#36I 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
#37 $ 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