Live data from Hacker News

Why Learn Awk? (2016)

blog.jpalardy.com

31–40 of 246 posts

Re: Why Learn Awk? (2016)

#31
Skip learning of sed and awk and jump straight to perl instead.

  $ perl --help
  ...
  -F/pattern/       split() pattern for -a switch (//'s are optional)
  -l[octal]         enable line ending processing, specifies line terminator
  -a                autosplit mode with -n or -p (splits $_ into @F)

  -n                assume "while () { ... }" loop around program
  -p                assume loop like -n but print line also, like sed
  -e program        one line of program (several -e's allowed, omit programfile)

Example. List file name and size:

  ls -l | perl -ae 'print "@F[8..$#F], $F[4]"'

Re: Why Learn Awk? (2016)

#32
post #19

Earlier quoted context omitted.

The reason isn't that bizarre; it's a POSIX utility and must conform to the specification, which you can read here: https://pubs.opengroup.org/onlinepubs/9699919799/utilities/c...

Does POSIX forbid extensions? That would be terrible.

Well... no, it doesn't, obviously, we can take a quick look at gawk to confirm this.

But, just as we can't say "awk offers the -b flag to process characters as bytes", we can't really say that cut offers any extensions not defined in the standard.

An implementation could, sure. I'd prefer that it didn't, writing conformant shell scripts is hard enough.

Re: Why Learn Awk? (2016)

#34
post #9

Because for some bizarre reason, "cut" doesn't ship with any decent column selection logic that is the equivalent of awk's $1, $2, etc., even in 2020. That's like 90% of my use of awk right there. I don't know of any easier equivalent of "awk '{ print $2 }'" for what it does. Posted partially so the Internet Correction Squad can froth at the mouth and set me straight, because I'd love to be showed to be wrong here.

> I don't know of any easier equivalent of "awk '{ print $2 }'" for what it does.

Does `cut -f2` not work? My complaint with cut is that you can't reorder columns (e.g. `cut -f3,2` )

Awk is really great for general text munging, more than just column extraction, highly recommend picking up some of the basics

Edit to agree with the commenters below me: If the file isn't delimited with a single character, then cut alone won't cut it. You need to use awk or preprocess with sed in that case. Sorry, didn't realize that's what the parent comment might be getting at.

Re: Why Learn Awk? (2016)

#35
post #31

Skip learning of sed and awk and jump straight to perl instead. $ perl --help ... -F/pattern/ split() pattern for -a switch (//'s are optional) -l[octal] enable line ending processing, specifies line terminator -a autosplit mode with -n or -p (splits $_ into @F) -n assume "while ( ) { ... }" loop around program -p assume loop like -n but print line also, like sed -e program one line of program (several -e's allowed,…

Yeah, because in love to read line noise.

Re: Why Learn Awk? (2016)

#36
post #2

Honorary mention of Taco Bell Programming. (fits this genre). http://widgetsandshit.com/teddziuba/2010/10/taco-bell-progra... Someone ought to write - Zen and the art of Unix tools usage.

Simple, Composable Pieces is practically the whole ethos of both Unix and Functional Programmers, and they both converged on the same basic flow model: Pipelines with minimal side-effects. This naturally leads to a sequential concurrency, which seems to be easy for humans to reason about: Data flows through the pipeline, different parts of the pipeline can all be active at once, and nobody loses track. It doesn't solve absolutely every possible problem, but the right group of pieces (utility programs, functions) will solve a surprising number of them without much trouble.

Re: Why Learn Awk? (2016)

#37
post #9

Because for some bizarre reason, "cut" doesn't ship with any decent column selection logic that is the equivalent of awk's $1, $2, etc., even in 2020. That's like 90% of my use of awk right there. I don't know of any easier equivalent of "awk '{ print $2 }'" for what it does. Posted partially so the Internet Correction Squad can froth at the mouth and set me straight, because I'd love to be showed to be wrong here.

And awk doesn't offers cut's column range selection ;)

I'm an expert on neither AWK nor cut, but AWK allows you to select a field, or the entire line, and then substrings within those.

Select characters 3-6 (inclusive) in the second field:

$ echo Testing LengthyString | awk '{print substr($2,3,4)}'

> ngth

If you want to select columns from the entire line, then:

$ echo Testing LengthyString | awk '{print substr($0,3,4)}'

> stin

Is that what you meant?

Re: Why Learn Awk? (2016)

#38

> Imagine programming without regular expressions. I live in this future and it's beautiful. Steps to programming without regular expressions: 1) find a PEG library for your language of choice There is no step 2.

Sorry for asking such dub question, but what's a PEG library?

Re: Why Learn Awk? (2016)

#39
post #19

Earlier quoted context omitted.

Does POSIX forbid extensions? That would be terrible.

Well... no, it doesn't, obviously, we can take a quick look at gawk to confirm this. But, just as we can't say "awk offers the -b flag to process characters as bytes", we can't really say that cut offers any extensions not defined in the standard. An implementation could, sure. I'd prefer that it didn't, writing conformant shell scripts is hard enough.

cut -f is in the standard:

https://pubs.opengroup.org/onlinepubs/9699919799/utilities/c...

Re: Why Learn Awk? (2016)

#40
post #19

Earlier quoted context omitted.

Does POSIX forbid extensions? That would be terrible.

Well... no, it doesn't, obviously, we can take a quick look at gawk to confirm this. But, just as we can't say "awk offers the -b flag to process characters as bytes", we can't really say that cut offers any extensions not defined in the standard. An implementation could, sure. I'd prefer that it didn't, writing conformant shell scripts is hard enough.

Before standards happen, creative developers are free to use their imagination and come up with useful features. Then someone makes up a standard, and from thereon progress is halted. The only way you grow new features and functionality is through design-by-committee, if people aren't making extensions that would one day make it to the next revision. I think it is ridiculous.

Tools should improve, and standards should eventually catch up & pick up the good parts.

People who need to work with legacy systems can just opt not to use those extensions, but one day they too will benefit. Others benefit immediately.

Post reply on HN