by llimllib · 3y ago · 0 comments
$ echo "key: value" | cut -wf 2
value
but whether it's actually "simpler" is open to debateedit: actually gnu cut lacks -w, so this is bsd-only. lol computers, stick with awk
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.
For the sake of the argument, say I have the following fixed output and want the sizes:
$ ls -l
-rw-rw-r-- 1 userAAA group 588 Aug 29 00:25 file1
-rw-rw-r-- 1 userAA groupB 11870 Aug 29 00:24 file2
-rw-rw-r-- 1 userA groupBB 1166 Aug 28 23:56 file3
-rw-rw-r-- 1 user groupBBB 195 Aug 28 23:56 file4
I would just do: $ ls -l | awk '{print $5}'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
EDIT: I see you discovered this. lol computers indeed. ;-)
Edit, found it, "use whitespace as the delimiter"
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
At that point, awk is vastly simpler