Why spend 30 seconds manually editing some text when you can spend 30 minutes clobbering together a pipeline involving awk, sed and jq
to be fair, out of those three, jq invokes the least existential dread in me
The important part is to learn the limits of any tool. Nowadays I no longer use jq for any long or complicated tasking. Filter and view data? jq is fine. Anything more and I just cook up a python script.
i’m more of a bash fan tbh. Ever since i started using linux, python started to irritate me
How do you get complex data structures to work? I was alienated from scripting on zsh because I wanted something like a dict and realised I would have to write my own implementation. Is there a work around for that?
I mean, there’s a point in data structure complexity where it’s useful to use Python.
But as to dicts, sure. You’re looking for zsh’s “associative array”. Bash has it too.
zsh
$ typeset -A mydict $ mydict[foo]=bar $ echo $mydict[foo] bar $
bash
$ typeset -A mydict $ mydict[foo]=bar $ echo ${mydict[foo]} bar $
This will do nicely - I had several workflows where I’d hit an API and get a massive super nested JSON as output; I’d use jq to get the specific data from the whole thing and do a bunch of stuff on this filtered data. I pretty much resigned to using python because I’d have successively complicated requirements and looking up how to do each new thing was slowing me down massively.
TIL I am an OP wizard.
deleted by creator
In all my years I’ve only used more than that a handful of times. Just don’t need it really
Now jq on the other hand…
jq
is indispensable
I’ve become a person that uses awk instead of grep, sed, cut, head, tail, cat, perl, or bashisms
The stage of your degeneracy will involve learning PERL.
Edit: one-liners FTW! 😁🐪
sort | uniq -c has entered the chat 🤣
I could try to learn awk while also trying to debug the annoying problem I’m trying to solve, orrr…
cut
andgrep
it isI rather do
${line%% *}
and avoid awk.
I used awk for the first time today to find all the MD5 sums that matched an old file I had to get rid of. Still have no idea what awk was needed for. 😅 All my programming skill is in Python. Linux syntax is a weak point of mine.
Probably the very same thing that the post talks about, which is extracting the first word of a line of text.
The output of
md5sum
looks like this:> md5sum test.txt a3cca2b2aa1e3b5b3b5aad99a8529074 test.txt
So, it lists the checksum and then the file name, but you wanted just the checksum.
I use
gawk
all the fucking time, if you spend a lot of time in a terminal or parse text often it is definitely worth the investment. It is a fantastic tool for both one liners and full scripts. Thegawk
manual is short enough to digest in a day or two.I remember when I first stumbled across this manual I was trying to look up a quick awk command and wound up reading the whole thing. It’s really one of the better GNU manuals.
All my homies use dubious regex
What’s an awk?
'awk tuah
It’s a Linux command-line program (
awk
). It’s pre-installed practically everywhere, it’s very powerful for string processing, but it also uses a fairly complex syntax.As a result, not many people know how to really make use of it, but
awk '{print $1}'
is something you encounter fairly quickly when you need to get the first word in each line.awk yeah
You can even do sum with awk, you don’t need excel
num-utils
hasnumsum
.
joke so dark I had to turn up my screen brightness to enjoy it.
Everything you do with
awk
, you can do withpython
, and it will also be readable.and
perl
, if you want it less readableOr you are old and crazy
Or PowerShell if you want it extra verbose
Hmm, but you have to install and run the Python environment for that. AWK is typically present on *NIX systems already. Python seem like overkill for basic text processing tasks.
On Debian the
python
is preinstalled.
cut -d ' ' -f1
master raceMy five thousand line bash script can do things that one hundred thousand lines of code could not do.
On the brightside, at least script monkeys can now look down on vibe coders.
– [x] I’m in this picture and don’t like it.
I think the most I’ve done with awk is write a battery monitor applet with it, it involved parsing data from /sys and making choices based on it so I decided it was a decent choice
Nushell enjoyers represent