11 ms·
Show HN: Tuc – When cut doesn’t cut it
Announcing `tuc`, a utility similar to coreutils `cut`, but more powerful.
It allows to split text or bytes into parts and reassemble them in any order.
I always found `cut` very practical for some tasks where `sed` or `awk` were overkill or awkward to use, but I also felt the need for more features.
Some key differences from `cut`:
- parts can be referenced by negative indexes
- delimiters can be any number of characters long, or match a regex
- can split text into lines, and reassemble them
- deleted 4y ago[deleted]
- bitcraft 4y agoThis is great! I always felt like cut was really handicapped by lack of negative indexes.
- tyingq 4y agoawk can do something like negative indexes if needed: $ echo "a b c" | awk '{print $(NF-1)}' b
- kseistrup 4y agoThe biggest handicap of cut for me has always been that it cannot split on blanks (TABs or SPACEs), you have to choose between TAB or SPACE. So I wrote an awk script that can print field ranges like cut, but recognizes blanks. Now I will see if I can get tuc worked into my muscle memory.
- layer8 4y agoI usually just pipe through sed to normalize the separators before applying cut.
- kseistrup 4y agoThat's also a little awkward, when there could easily be an option to split by all blanks.
- layer8 4y agoI’m not defending cut here, but using sed is also pretty straightforward and fits its purpose. I’d argue that using the existing general-purpose tools is better than creating custom narrow-purpose tools in simple cases like this one. Besides maintainability and familiarity, it also exercises your proficiency in applying the standard tools.
- kseistrup 4y agoYou have a point, of course.
- samwhiteUK 4y agoWhen I want to use negative indices, I pipe the string through rev first, then do my cut, then rev again
- mprovost 4y agoThat's the classic solution but blows up when using multibyte characters since rev just reads the bytes in each line in reverse.
- pimlottc 4y agoWhat do you mean by negative indexes?
- ratrocket 4y agoI believe "negative index" means array[-1] is the last element in array, array[-2] is the second-to-last element, etc. In the context of "cut", it would mean being able to do something like: cut -d" " -f1--2 the "-f1--2" (read: fields from 1 to minus 2; it's a range) means to select from the first field to the second-to-last field. (that double "--" is pretty awkward, to be sure!) Some programming languages (ruby is the one that I know) have this feature for accessing array elements.
- overlordalex 4y agoJust want to say thank you for the unlimited delimiters - this was something that always limited my usage of cut so just this feature alone makes tuc worth it
- masklinn 4y agoSeems similar in intent to choose (https://github.com/theryangeary/choose https://github.com/theryangeary/choose) as a cut which doesn't suck. The features outlined are very close, I just don't understand what "can split text into lines" is, do you mean that the selected fields can be split into lines? The main advantage of tuc seems to be "templated" outputs.
- riquito 4y ago> I just don't understand what "can split text into lines" is, do you mean that the selected fields can be split into lines? Good question, I struggle to word it properly, any help is appreciated. Assume a file (we will call it "input"), such as first line here followed by second line You can use a delimiter and cut inside each line e.g. $ tuc -d ' ' -f 2 < input line by or you can cut it "by lines", practically considering the whole file as your single "line" and using newline as delimiter $ tuc -l -f 2 < input followed by second line If you want to remove a line, or keep something inbetween it can be more practical/intuitive than head/tail or sed
- forty 4y agoThis feature seems more like a replacement for head and tail (and combination of both) rather than cut. Maybe a good way to explain it would be to show how to achieve the same thing with those well known commands (comparison which should certainly be in favor of tuc ^^) EDIT: sorry you just said more or less the same thing, I need to read better :)
- renewiltord 4y agoYou could have it do "every 5th line". Easy with awk but this is more compact
- tyingq 4y agoSed is terse for that, but not very memorable...sed -n '0~5p' filename
- shreve 4y agoI am in love with this trend of replacing old school unix utilities with new rust projects that stay just as fast or faster, but increase the usability or feature set tenfold. rg, fd, exa, bat, and now tuc.
- lillecarl 4y agoI use rg, exa, bat, and zellij as my "rust replaced old stuff". Zellij isn't yet as polished as I'd like it, but it's way more intuitive than tmux.
- hultner 4y agoOh god, is tmux considered “old stuff” now? I barely finished replacing my screens.
- tomjakubowski 4y agoIt happens to all of us. Did you know Interstellar came out eight years ago?
- lillecarl 4y agoI actually started with screen, but I only used it for daemonising some foreground processes, I work in Zellij daily.
- tomjakubowski 4y agoUsually you have to install tmux on a remote machine anyway, so Zellij seems like a good one of these to try.
- krylon 4y agoOpenBSD ships tmux in the base system. I would be very pleased if more systems did this.
- apgwoz 4y agoThe names of these utilities are bad. I have literally no idea what any of them do. The same could be said for standard unix utilities, true, but they have 50+ years (in some cases) of brain bake in, and have the advantage of names that have _some_ relation to their function (ls : list files :: exa : “extract the list of files from a dirent?”)
- matteo-biagetti 4y agoawesome!
- mmastrac 4y agoThis is an incredibly useful improvement over cut, thank you. The mental distance from cut to awk/sed is often just too high and having a more useful utility will drastically reduce how much I reach for those tools.
- omoikane 4y agoI would still reach for awk/sed, for the fact that they tend to be preinstalled. I might fall back on perl/ruby/python if awk/sed were insufficient, which also tend to be preinstalled.
- ur-whale 4y agoTriple thumbs up for -e Never understood why that was never added to cut in the first place.
- blackfede 4y agoThanks! I am already using it!
- Aardwolf 4y ago> cargo install tuc Slightly off topic question about this: in Linux, are rust programs always installed like this, or should these also be made available in the regular package manager of your distro?
- elsjaako 4y agoIn general you want to install it with a package manager. (But also it's your system. What's the point of Linux if you can't do it your way)
- masklinn 4y ago> or should these also be made available in the regular package manager of your distro? Getting a package in the official repositories is quite a high bar to clear. Plus it's... not great experience for early development, as many distros will lock in the program entirely, leaving you with a very long tail of extremely outdated installs. So generally the expectation is that once a program is popular or desirable enough, and is somewhat stable, it gets integrated into the base repos.
- Aardwolf 4y agoIs there an equivalent for C/C++ programs? There's pip for python, npm for JS, cargo for rust For C/C++ all I know are multiple different possible build and make systems, but none works like a package manager, as far as I know Note that I don't always love when something installed with pip or npm puts files all over your OS or homedir without being managed by the package manager, though
- gleenn 4y agoThis is awesome. Especially the ability to compress delimiters. I can't tell you how many times I want to grab a couple fields from the output of another command but can't get delimiters to work correctly or they're using custom spacing to align columns and it blows everything up and then I'm crying in awk land.
- ChicagoBoy11 4y agoI've found this seriously cool! While not my primary role at my job, I often find myself dealing with lots of disparate data sets, usually needing to do some sort of manipulation, cleaning, searching, etc. Every now and then encounter something like this and it seems to me that there are potentially a nice set of command line tools/utilities that I should be adding to my belt. Anywhere I should particularly start taking a look? Like, if my goal is to because much better at wrangling CSV/text-delimited files, searching across folders of docs for numbers, etc., where is my first entry point into trying to become much more proficient at it?
- pdimitar 4y agoHere's a recent gateway post: https://jvns.ca/blog/2022/04/12/a-list-of-new-ish--command-line-tools/ https://jvns.ca/blog/2022/04/12/a-list-of-new-ish--command-l... There are even more. :)
- niccl 4y agoI do a lot of that sort of thing, and my go-to tools are grep (with full regular expressions), sort, uniq, head, tail, sed, and, the 200 pound gorilla (not as heavy as it could be) awk. Then if all else fails, Python
- NegativeLatency 4y agofd, bat, fzf are good starting points also mdfind if you're on macos I'm hesitant to go fully into "new" stuff so I can maintain skills on random *nix boxes I need to ssh into.
- benhoyt 4y agoI highly recommend you learn the basics of AWK, which is a little language that lets you match on and run commands on every line of input (so AWK "scripts" are usually one-liners). The very basics can be learn in 5-10 minutes, the whole language in a couple of hours (read the first three chapters of "The AWK Programming Language" by A, W, K.) For searching recursively through folders, I recommend ripgrep (rg), though it won't do number conversions and the like. Self-promotion (but I think it's useful here) is my GoAWK project, which is a POSIX-compatible version of AWK, with the recent addition of proper CSV support: https://benhoyt.com/writings/goawk-csv/ https://benhoyt.com/writings/goawk-csv/
- seanw444 4y agoHacker News comes through again. Been looking for tool exactly like this.
- bradwood 4y agoAwk?
- emmelaich 4y agoSeems a bit of functionality overlap with lam/jot/rs; have you looked at those? They're from BSD originally. Included in macos, and in Linux distros as bsd-utils.
- linsomniac 4y agoThis is the "cut" I've always wanted!
- alex_hirner 4y agoDid you find any limitations of `pico-args` turned into a caveat for tuc?
- riquito 4y ago> Did you find any limitations of `pico-args` turned into a caveat for tuc? Quite the opposite, early on I started with `clap`, then moved briefly to `argh` before settling with `pico-args`. Compilation time and size where the main driving factors, alonside support for non-spaced values (e.g. -d' '). Maybe if tuc had subcommands it would have been a different story, but I didn't find enough value in more blasoned arg libraries.
- nijave 4y agoIt'd be nice if it could split and support escapes and quoted strings. I often run into issues with things like csv where fields might be quoted or quoted strings where quotes are escaped
- nathants 4y agothis is excellent.
- donio 4y agoDoesn't seem to fix the #1 thing missing from cut: an easy syntax for splitting on whitespace sequences (rather than a single space), like awk does by default. (I see that it support splitting on regex but I was hoping for it to be the default or a single character switch)
- vultour 4y agoIsn’t that what the —-greedy-delimiter is for?
- donio 4y agoNice, missed that, and it has a single character shorthand too: -g Edit: or maybe not, I think I'd still have to use --regex for real whitespace sequences that can be a mix of spaces and tabs.
- riquito 4y agoAs you figured out, -g (--greedy) matches the same delimiter multiple times (e.g. one or more spaces). If you want to match different delimiters (e.g. a mix of spaces), one or more times, you must use -e (--regex).
- deleted 4y ago[deleted]
- deleted 4y ago[deleted]
- mttjj 4y agoI get that ‘tuc’ is just ‘cut’ backwards but I was kind of hoping it also stood for ‘the ultimate cutter’ or something like that. :) In either case, very cool. Thanks for sharing. I’ll be using this for sure!
- asicsp 4y agoNice, especially the format output. See also: * hck (https://github.com/sstadick/hck https://github.com/sstadick/hck) - close to drop in replacement for cut that can use a regex delimiter instead of a fixed string * rcut (https://github.com/learnbyexample/regexp-cut https://github.com/learnbyexample/regexp-cut) - my own bash+awk script, supports regexp delimiters, field reordering, negative indexing, etc