241 points · by riquito · 4y ago · 115 comments
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
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?
There are even more. :)
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/
For a 45-year old language, it's actually surprisingly good. There are a few annoyances and clumsy things (getting substrings is a function call without nice str[start:end] syntax, regex matches are harder than they need to be, string concatenation syntax is weird, arrays aren't nestable (except in Gawk), and so on).
I've actually thought it'd be great to "fix" these things and make a nicer AWK language, but the problem is then no one would use it. :-)
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.
See also:
* 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) - my own bash+awk script, supports regexp delimiters, field reordering, negative indexing, etc
"Modern" dev environments are often tied to their own package manager. JS has Npm, Rust has Cargo, etc... These have their own dependency managers, version systems, etc... and they don't always get along with the way Linux package managers work.
IIRC, you don't even need Cargo to do Rust, so it should be possible to compile Rust projects like you compile C projects and essentially mirroring crates.io but with .deb packages but it looks like a lot of work.
If you get anywhere in the neighborhood of a proper superset of the old application, we do occasionally retire the old ones.
So maybe don't project about "hip" or "young" because it does your otherwise decent argument a disservice.
>Here, I don't have to chase the next bride.
Who hurt you? :)
Or you can just stick with the old tools if you prefer not learning a new thing - that's a perfectly valid option.
The main advantage of tuc seems to be "templated" outputs.
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 linee.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 sedMaybe 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 :)
It's not. `tuc` is a superset of cut and in that particular example there's no difference. If you wanted instead to cut on multibyte delimiters, or on a random amount of spaces, `tuc` would work, while `cut` would fall short
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?
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.
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
Downloading the source by hand, trying to wrangle what dependencies it has not vendored (which may or may not be available through your system package managers, in versions which may or may not be recent enough), and trying to find out how to build it.
Though do note that this issue can also hit when installing python, js, or rust package, if they ultimately have native dependencies. Their respective build systems will generally try to make it work out of the box, but if your configuration was not specifically tested / supported it can break with fun C-level compilation errors.
./configure --prefix=/usr/local && make && sudo make install
(I consider it a feature that this doesn't automatically download and install hundreds to thousands of things I haven't even heard of)In general, end users should use some other method that doesn't require having a Rust toolchain pre-installed, but doing that can take work, and so not every program pursues it.
(But also it's your system. What's the point of Linux if you can't do it your way)
$ echo "a b c" | awk '{print $(NF-1)}'
bIn 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.
In either case, very cool. Thanks for sharing. I’ll be using this for sure!
(I see that it support splitting on regex but I was hoping for it to be the default or a single character switch)
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.
Never understood why that was never added to cut in the first place.
They're from BSD originally. Included in macos, and in Linux distros as bsd-utils.
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.