(Re)Learning AWK with ChatGPT
awk is the Swiss army knife of the command line. If you have a text-shaped problem then awk can probably be a solution. Here’s a fabulous talk by Brian Kernighan (the k in awk) on why this may be the case.
But it’s also a tool that I use just rarely enough that I keep forgetting details. So everytime I reach for awk it’s a painful experience of not remembering and having to cobble together 3 different StackOverflow answers to get something resembling the script I want. Only to then realize I’ve been “holding it wrong” and there’s a better approach 🤦.
I still believe it’s supremely useful as it can do everything and is available everywhere so I keep inflicting this pain on myself.
This time it’s different
ChatGPT changes this. It’s training data (presumably) already contains all of the StackOverflow answers that I was combing through and it can cobble them together for me. So let’s try to use it to cobble them together for me and try to learn more efficiently.
Tangent: chatgpt-cli
Since I like to stay in my terminal while I do deep-focus work I want to also access ChatGPT from command line. I couldn’t find a client that I would like so I wrote my own: github.com/edofic/chatgpt-cli. It’s a one shot command that fires off you query and streams the result real-time back to your cli. But also allows you to ask follow up questions.
So… awk?
Motivating example this time was multi-line search. Specifically I wanted to extract a section of a document (multiple lines) with known patterns for the first and the last line.
I remember doing this before… You create what amounts to a state machine. If a
line matches pattern A you go to print
state, in print
state you match for
pattern B, if match then exit, otherwise print the line.
But staring at a blank prompt and trying to transcribe this to awk syntax by heart got me thinking. I reached for ChatGPT
|
|
This is exactly what I wanted. But it includes the explanation for all the syntax.
And it can do one think that an amalgamation of StackOverflow answers cant: follow up questions.
|
|
Takeaway
Key message here is not that you can use ChatGPT to generate awk scripts. It’s that in doing that you can also learn how these scripts work and how to do them yourself.
And at least for me that’s a much faster and frictionless solution that searching for answers and fitting them together.
Last modified on 2023-03-31
Previous Hypermedia driven micro frontendsNext GraphQL ❤️ SQLite