5 ms·
You 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 la
by llimllib 3y ago
You 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.
- Galanwe 3y agoRight, but you are bending the argument to your will. What is a "decently complex one liner"? If it's decently complex, then it's probably not a one liner, so indeed chatgpt may be faster. If it's a one liner, then it's probably not complex, so I would be quite confident in being faster than chatgpt. The reality is, 99.9% of one liners are just series of pipes and filters to extract specific fields from an output, and act on it. I'm quite confident I would be faster than chatgpt for any of these. And no, I don't write bash "all day, every day". I tend to see "every day bash" as very similar to SQL. Once you know cut, grep, find, sed and awk, even at a basic level, then you can combine them and extract pretty much anything.