5 ms·
99.9% of my awk use case is to split a line (a la "cut - d\ - f) while discarding successive spaces. e.g.: $ echo "key: value" | awk '{print $1}'
by Galanwe 3y ago
99.9% of my awk use case is to split a line (a la "cut - d\ - f) while discarding successive spaces.
e.g.:
$ echo "key: value" | awk '{print $1}'
value
Open to a simpler replacement :-)
- cb321 3y agoYou might consider: https://github.com/c-blake/bu/blob/main/doc/cols.md https://github.com/c-blake/bu/blob/main/doc/cols.md That's in Nim, though that may not be much a barrier. (There may also be other tools in bu/ of interest.)
- llimllib 3y agoYou can do it with cut too: $ echo "key: value" | cut -wf 2 value but whether it's actually "simpler" is open to debate edit: actually gnu cut lacks -w, so this is bsd-only. lol computers, stick with awk
- cb321 3y agoOnly with FreeBSD `cut`.. coreutils `cut` (Linux) is missing -w as is at least OpenBSD? EDIT: I see you discovered this. lol computers indeed. ;-)
- version_five 3y agoWhat does -w do? This works without it, no? Edit, found it, "use whitespace as the delimiter" https://www.unix.com/man-page/FreeBSD/1/cut/ https://www.unix.com/man-page/FreeBSD/1/cut/ For most cases like the OP you'd know the delimiter anyway so I don't think the absence is a big deal, and if not it would be easy to use tr or sed to make it consistent
- llimllib 3y agothe important thing is that it uses _consecutive_ whitespace as a the delimiter, so you'd have to use sed to collapse all the whitespace down to one tab. At that point, awk is vastly simpler
- rigelina 3y agoI can't tell you how many times I pipe in rev to put my text where I want it for cut (then rev it again). Abbreviated example, getting the service names from a k8s cluster looks roughly like (actual command does a bit more processing): kubectl get deployments -o wide | rev | cut -d'=' -f1 | rev But if it's just gobbling whitespace, xargs without a command can be your friend. $ echo "key: value" | cut -d: -f2 | xargs value My brain generally goes "rev sed head tail xargs cut tr ... screw it, I'll use python ... someday I shall learn awk." There's a young engineer on my team that knows awk, and I'm envious.
- SOLAR_FIELDS 3y agoYou don’t even need to know awk these days. Just say “how to do x munging task” in ChatGPT and you’ll get a one liner that will be just as good as if you’d say there squinting at man pages for 30 minutes
- llimllib 3y agothis is exactly the sort of case where you get non-portable bullshit you don't understand out of it! It spits out something that works on BSD but not on GNU, you put it in your script and _boom_ wonder why the thing blew up in prod, and oh btw you also lack the ability to debug it because you never understood it in the first place
- Galanwe 3y agoMore importantly it's just mega slow. The whole point of bash one liners is not to write short bash, it's to type it in your shell to get a result quickly. Just typing the url to chatgpt in my browser I would have had the time to write my one liner in she'll XD
- SOLAR_FIELDS 3y agoI sincerely doubt unless you are writing shell all day every day that you will get a decently complex working one liner out faster than GPT4.
- asicsp 3y agoCheck out https://github.com/sstadick/hck https://github.com/sstadick/hck and https://github.com/theryangeary/choose https://github.com/theryangeary/choose - both are alternatives for cut/awk and allows regex based split as well. Though, they don't remove starting/ending whitespaces IIRC. I wrote a script (https://github.com/learnbyexample/regexp-cut https://github.com/learnbyexample/regexp-cut) that uses `awk` to provide a `cut`-like tool with regex-based split, negative index, etc. And this will take care of starting/ending whitespaces as that's the default `awk` behavior.
- Narishma 3y agoI think you mean echo "key: value" | awk '{print $2}'