e.g.:
$ echo "key: value" | awk '{print $1}'
value
Open to a simpler replacement :-)e.g.:
$ echo "key: value" | awk '{print $1}'
value
Open to a simpler replacement :-)That's in Nim, though that may not be much a barrier. (There may also be other tools in bu/ of interest.)
$ 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
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
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.
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
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.
SQL is a great example too where I will use an AI even though it’s not necessary. I’ve probably written many tens or hundred thousands of lines of SQL in my life and I would still prefer to just toss my requirements into an AI and have it write the query for me so I don’t have to cross reference things and look up syntax. Easier to do that and iterate on it once or twice than comb through some bigquery or Postgres docs because I can’t remember that particular flavor of sql today
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}'I wrote a script (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.
echo "key: value" | awk '{print $2}'