4 ms·
awk is quite handy. I often use it in place of other tools like sed and cut depending on what I need. Usually I use it for quick oneliners, and on occasion bi
by gwittel 5y ago
awk is quite handy. I often use it in place of other tools like sed and cut depending on what I need. Usually I use it for quick oneliners, and on occasion bigger things. But typically more than few lines I end up reaching for a shell script or Python/Perl.
Minor simplification on the line numbering example, there's a builtin variable for counting records (NR or FNR) so no need to introduce your own counter:
awk '{print NR "\t" $0}' < somefile
There are other builtins that are quite handy as well. The only thing that bugs me at times is when I want to print a range of records like column 5 to end, where the total number of columns varies. I wish there was a simpler way to do that like a column range selector. This is where I sometimes use 'cut' since its so simple to do column ranges (unless the column separators have consistent whitespace; then cut doesn't work).