Live data from Hacker News

Awk in 20 Minutes (2015)

ferd.ca

11–20 of 128 posts

Re: Awk in 20 Minutes (2015)

#11
post #10

So, I don't use awk right now. This answers "how to awk" more than "why use awk" for me (understandable given the 20 min claim). Does anyone have concrete, practical examples of use cases where awk made your life much easier?

Awk '{ print $2 }' does LWSP gobbling.. cut -d' ' -f2 doesn't and this difference alone makes awk useful to me on a daily basis.

Awk has a hash like perl which is very efficient. I use an awk expression to print uniq as they come in counted through the hash insert on new instead of uniq which prints at end.

Awk count unique over 300,000,000 ips was as fast as perl and python and smaller memory footprint

Re: Awk in 20 Minutes (2015)

#12
post #10

So, I don't use awk right now. This answers "how to awk" more than "why use awk" for me (understandable given the 20 min claim). Does anyone have concrete, practical examples of use cases where awk made your life much easier?

I contribute to a niche game server project and used awk to generate c# class files from an org mode document that I wrote to specify them.

Re: Awk in 20 Minutes (2015)

#13
post #12
post #10

So, I don't use awk right now. This answers "how to awk" more than "why use awk" for me (understandable given the 20 min claim). Does anyone have concrete, practical examples of use cases where awk made your life much easier?

I contribute to a niche game server project and used awk to generate c# class files from an org mode document that I wrote to specify them.

What about awk makes that easier than, say, a Python script?

Re: Awk in 20 Minutes (2015)

#15
post #11
post #10

So, I don't use awk right now. This answers "how to awk" more than "why use awk" for me (understandable given the 20 min claim). Does anyone have concrete, practical examples of use cases where awk made your life much easier?

Awk '{ print $2 }' does LWSP gobbling.. cut -d' ' -f2 doesn't and this difference alone makes awk useful to me on a daily basis. Awk has a hash like perl which is very efficient. I use an awk expression to print uniq as they come in counted through the hash insert on new instead of uniq which prints at end. Awk count unique over 300,000,000 ips was as fast as perl and python and smaller memory footprint

LWSP=Linear White Space "Linear white space is: any number of spaces or horizontal tabs, and also newline (CRLF) if it is followed by at least one space or horizontal tab." [1]

I don't know what LWSP gobbling is, though. Google didn't help.

[1] https://stackoverflow.com/questions/21072713/what-exactly-is...

Re: Awk in 20 Minutes (2015)

#16
post #10

So, I don't use awk right now. This answers "how to awk" more than "why use awk" for me (understandable given the 20 min claim). Does anyone have concrete, practical examples of use cases where awk made your life much easier?

I use awk to write csv-parsers for my personal finance setup. They take transaction logs and convert them into a ledger format (https://www.ledger-cli.org/). There's some pretty trivial if/else branches based on regexp's, which awk is pretty good at expressing.

I'd normally write this sort of thing in python. The awk program is usually smaller than a comparable python program, but for me the main selling point is that awk programs a more "UNIX-pipe-native" than python programs are.

At some point, I'll probably rewrite these programs in a more "serious" programming language, once the file sizes get too big or something, but for now they're working great and are easy to extend.

Re: Awk in 20 Minutes (2015)

#17
post #13
post #12

Earlier quoted context omitted.

I contribute to a niche game server project and used awk to generate c# class files from an org mode document that I wrote to specify them.

What about awk makes that easier than, say, a Python script?

Processing text files line by line is trivial in awk. Maybe it's easy in Python too but at least in awk you don't have to worry about the 2 vs 3 silliness.

Plus I think semantic indentation is not sane language design.

Plus you can pipe it to another command with ease.

Re: Awk in 20 Minutes (2015)

#18
post #13
post #12

Earlier quoted context omitted.

I contribute to a niche game server project and used awk to generate c# class files from an org mode document that I wrote to specify them.

What about awk makes that easier than, say, a Python script?

Awk automatically operates on every line of a text file, so you can easily use it as part of a pipeline in Linux. I'll usually use grep, cut, sort, and awk all together with one line of code and no need to open an editor. Python takes a lot more code to do these basic things. However, Python is much better once the complexity grows past a certain point.

Re: Awk in 20 Minutes (2015)

#19
post #15
post #11

Earlier quoted context omitted.

Awk '{ print $2 }' does LWSP gobbling.. cut -d' ' -f2 doesn't and this difference alone makes awk useful to me on a daily basis. Awk has a hash like perl which is very efficient. I use an awk expression to print uniq as they come in counted through the hash insert on new instead of uniq which prints at end. Awk count unique over 300,000,000 ips was as fast as perl and python and smaller memory footprint

LWSP=Linear White Space "Linear white space is: any number of spaces or horizontal tabs, and also newline (CRLF) if it is followed by at least one space or horizontal tab." [1] I don't know what LWSP gobbling is, though. Google didn't help. [1] https://stackoverflow.com/questions/21072713/what-exactly-is...

It means a contiguous collection of arbitrary white space characters are considered to be a single delimiter.

With the parent example it means that column #2 could have any of number whitespaces between it and column #1.

Re: Awk in 20 Minutes (2015)

#20
post #16
post #10

So, I don't use awk right now. This answers "how to awk" more than "why use awk" for me (understandable given the 20 min claim). Does anyone have concrete, practical examples of use cases where awk made your life much easier?

I use awk to write csv-parsers for my personal finance setup. They take transaction logs and convert them into a ledger format ( https://www.ledger-cli.org/ ). There's some pretty trivial if/else branches based on regexp's, which awk is pretty good at expressing. I'd normally write this sort of thing in python. The awk program is usually smaller than a comparable python program, but for me the main selling point is t…

I've used it for this too, but things tend to go south pretty quick if you have escaped or quoted commas. That, unfortunately, is where I usually break down and pull out python.
Post reply on HN