[Toybox] Has anybody ever actually used cut -f?

Samuel Holland samuel at sholland.org
Thu Sep 1 13:29:28 PDT 2016


Hello,

On 09/01/2016 02:58 PM, Rob Landley wrote:
> In theory:
>
> echo "one two three four five" | cut -f 2-4
>
> Should be really useful, and mean you don't need awk. In practice,
> posix specifies that the default separator of cut -f is TAB, and that
> the -d delimiter specifier has no way to specify 'arbitrary run of
> whitespace'.

Yes, I use cut all the time. In fact, I have never intentionally used
awk on my own--only when copied from somebody else's one-liners.
Usually if there's a variable run of space I cut on the punctuation next
to it, or failing that, pipe through `sed 's/\s\+/\t/g'`. Of course,
this probably defeats the whole advantage of using cut over awk
(simplicity), but it's habit at this point.

> So I propose 2 changes to toybox cut:
>
> 1) -d "" means arbitrary run of whitespace.
>
> 2) It's the default.

I'm sure people besides me use `cut -f`, but I also assume they use -d.
So changing the default delimiter to arbitrary whitespace shouldn't be a
problem...

I tried to search GitHub, but they broke global code search; Google
got me to https://github.com/stephenturner/oneliners and
https://gist.github.com/j3tm0t0/4122817 which apparently don't use -d.
On the other hand, I see a lot of instances of -d " " which would be 
simplified by the proposed change.

> As has been noted before, this makes about 90% of the uses of awk go
> away. The downside is, if you're _not_ using toybox cut, it won't
> work.
>
> Any opinions?

If you want to avoid breaking existing code, but make cut more useful,
accept multiple characters for -d and match any of them. Then at least
you could do cut -d "$IFS" or similar if you don't know if the output is
spaces or tabs.

This got me thinking, since \n is in $IFS...

$ printf "1234\n5678\n\n90\n" | cut -s -f2 -d$'\n'
5678
$ printf "1234\n5678\n\n90\n" | cut -f2 -d$'\n'
5678
$ printf "1234\n5678\n\n90\n" | ./toybox cut -s -f2 -d$'\n'
$ printf "1234\n5678\n\n90\n" | ./toybox cut -f2 -d$'\n'
1234
5678

90
$

I'm not sure what to make of that.

> Rob

Samuel



More information about the Toybox mailing list