An awkward realization
An awkward realization
An awkward realization
Why spend 30 seconds manually editing some text when you can spend 30 minutes clobbering together a pipeline involving awk, sed and jq
Or 60 minutes making it all work just with jq functions.
TIL I am an OP wizard.
cut -d ' ' -f1
master race
Ok, þe quote misplacement is really confusing. It's
awk '{print $1}'
How can you be so close to right about þis and still be wrong?
How can you be so close to right about þis and still be wrong?
Honest answer: I’m sloppy on mobile
Better answer:
I see what you did þere
Ssshhh, maybe no-one will notice!
Hey I throw a /^regexp.*/ {print $NF}
in there sometimes!
...but yes, it's mostly print $1
—but only because I mix up the parameters whenever I try to use cut
!
my favorite awk snippet is !x[$0]++
which is like uniq
but doesn't care about order. basically, it's equivalent to print_this_line = line_cache[$current_line] == 0; line_cache[$current_line] += 1; if $print_this_line then print $current_line end
.
really useful for those long spammy logs.
Oh that's very interesting. I usually do sort --unique
or sort [...] | uniq
if I need specific sorting logic (like by size on disk, etc).
Looking at the above awk snippet, it'll retain order, though. So, sort will normally change the order. The awk snippet won't, just skip occurrences of a given line after the first. Depending upon the use case, that order retention could be pretty desireable.
same, that statement saved me so much effort
All my homies use dubious regex
You can even do sum with awk, you don't need excel
num-utils
has numsum
.
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
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
Honestly I think 90% of people would never use awk if there was a simple preinstalled command for "print the nth column"
cut?
To be fair, a lot of the programs don't use a single character, have multiple spaces between fields, and cut
doesn't collapse whitespace characters, so you probably want something more like tr -s " "|cut -d" " -f3
if you want behavior like awk
's field-splitting.
$ iostat |grep ^nvme0n1 nvme0n1 29.03 131.52 535.59 730.72 2760247 11240665 15336056 $ iostat |grep ^nvme0n1|awk '{print $3}' 131.38 $ iostat |grep ^nvme0n1|tr -s " "|cut -d" " -f3 131.14 $
This is definitely somewhere that PowerShell shines, all of that is built in and really easy to use
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! 😁🐪
I could try to learn awk while also trying to debug the annoying problem I'm trying to solve, orrr.... cut
and grep
it is
sort | uniq -c has entered the chat 🤣
I rather do ${line%% *}
and avoid awk.
joke so dark I had to turn up my screen brightness to enjoy it.
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.
10 PRINT BUTTS
20 GOTO 10
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. The gawk
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.
Everything you do with awk
, you can do with python
, and it will also be readable.
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.
and perl
, if you want it less readable
Or you are old and crazy
Or PowerShell if you want it extra verbose
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.
awkward
That's where they send you when the syntax drives you insane.
awk yeah
Amazon Web Keynotes. It's a programming language for server administration.
ITT: whooosh
uh, no.
Wut
Nushell enjoyers represent
-- [x] I'm in this picture and don't like it.
My 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.