awk 101
Previously I mentioned the unix tool awk
, but I didn’t really understand it. Today I heard something that really made me understand what this tool is all about:
awk
treats each row in a text file as a “record”- Each word in a row is a “field”
- The default field separator is whitespace, but it can easily be changed to commas or whatever.
The basic awk
command looks like this: awk '{ }' file.txt
Inside the {}
is where we enter the commands.
Each “record” (remember - row) in awk
contains 4 special variables that can be useful:
Additionally, each field in a record can be referenced by its index position, starting with 1.
Let’s pretty-print a csv
file using awk
.
Input:
awk appears to have all the power of a scripting language, and its syntax is also flexible. Check out how we can create a “function”-like syntax that changes the FS
(field separator) and OFS
(output field separator), and also use a conditional to only print our header and lines that contain a specific word (“dropbox” in our example)