News for hackers. Stuff that matters.
Re:Understanding Awk by foobarian on Thursday September 30, 2021 @11:48AM (#28707725)
The funny thing is, by and large my only use case for awk is to print out whitespace delimited columns where the amount of whitespace is variable. Surprisingly hard to do with other Unix tools.

Neat discussions around that sort of thing at least here: https://news.ycombinator.com/item?id=23427479

Re:Understanding Awk by tyingq on Thursday September 30, 2021 @12:51PM (#28708415)
The syntax isn't nearly as nice, but Perl can be handy if you're doing something more after splitting into columns. And it's usually already there / installed, like awk. For just columns:

  $ printf "a b  c d   e\n1 2  3 4 5" | perl -lanE 'say "$F[2] $F[4]"'
  c e
  3 5
Re:Understanding Awk by adamgordonbell on Thursday September 30, 2021 @01:05PM (#28708571)
It surprized me that AWK had dictionaries and no declaration of vars that make it feel like a modern scripting langauge even though it was written in the 70s.

It turns out though that this is because Perl and later Ruby were inspired by AWK and even support these line by line processing idioms with BEGIN and END sa well.

    ruby -n -a -e 'puts "#{$F[0] $F[1]}"'

    ruby -ne '
    BEGIN { $words = Hash.new(0) }

    $_.split(/[^a-zA-Z]+/).each { |word| 
    $words[word.downcase] += 1 }

    END {
        ...
Re:Understanding Awk by tannhaeuser on Friday October 01, 2021 @01:57AM (#28715128)
I think it's pretty obvious that awk syntax is ultimately the main inspiration for JavaScript syntax, with optional semicolon as stmt terminator, regexp literals, for (x in y), the function keyword, a[x] associative array accessors, etc.
Re:Understanding Awk by popcube on Friday October 01, 2021 @10:20PM (#28725392)
they spend a lot of time to make one line perl can handle most function of awk, sed et al.
Re:Understanding Awk by flandish on Thursday September 30, 2021 @12:14PM (#28708039)
A long while ago I wrote up a little processor to determine field lengths in a given file - I forgot the original reason. ( https://github.com/sullivant/csvinfo )

However, I feel I really should have taken the time to learn Awk better as it could probably be done there, and simply! (It was a good excuse to tinker with rust, but that's an aside.)

Re:Understanding Awk by tyingq on Thursday September 30, 2021 @01:08PM (#28708615)
For some idea, a one liner to find the (last) longest username and length in /etc/passwd:

  $ awk -F: '{len=length($1);if(len>max){max=len;user=$1}}END{print user,max}' /etc/passwd
Re:Understanding Awk by flandish on Thursday September 30, 2021 @01:32PM (#28708874)
Thanks for that reply! It's good to work with an example.
Re:Understanding Awk by genewitch on Thursday September 30, 2021 @01:36PM (#28708923)
I'll mark this on my GitHub when I get back on a computer, I take public datasets and make graphs and transforms and reports. The big survey companies have weird data records and having to write a parser is my least favorite part. I think other people who ingest my content don't appreciate the effort, but that's a near universal feeling I think, heh.
Re:Understanding Awk by twic on Thursday September 30, 2021 @05:16PM (#28711416)
If i don't use awk, i throw tr -s ' ' into the pipeline, and then the delimiter is a single space, so you can just cut.
Re:Understanding Awk by axiolite on Friday October 01, 2021 @04:15AM (#28715901)
That will collapse multiple spaces, but won't handle a mix of spaces and tabs, which awk will handle.
Re:Understanding Awk by adamgordonbell on Thursday September 30, 2021 @11:56AM (#28707818)
choose from your link does look nice for simple column selection.

   echo -e "foo   bar   baz" | choose -1 -2
vs awks

   echo -e "foo   bar   baz" | awk '{ print $2, $3}'
I love the effort people are putting into reinventing the core unix tools.

I think I'll stick with Awk for now though.

Re:Understanding Awk by foobarian on Thursday September 30, 2021 @12:15PM (#28708049)
The problem with new tools is

$ choose

bash: choose: command not found...

Re:Understanding Awk by goohle on Thursday September 30, 2021 @12:48PM (#28708382)

  ls -l | tr -s ' ' | cut -d ' ' -f 5
Re:Understanding Awk by foobarian on Thursday September 30, 2021 @01:11PM (#28708639)
Exactly! Exactly! And now fix it to work with tabs :-)
Re:Understanding Awk by tyingq on Thursday September 30, 2021 @01:16PM (#28708700)
And leading whitespace. Compare:

  $ printf " one two  three"  | tr -s ' ' | cut -d ' ' -f 1

  $ printf " one two  three"  | awk '{print $1}'
  one
Re:Understanding Awk by goohle on Thursday September 30, 2021 @01:44PM (#28709000)

  ps ax | sed 's/^\s\+//; s/\s\+/ /g;' | cut -d ' ' -f 4
Re:Understanding Awk by goohle on Thursday September 30, 2021 @01:41PM (#28708965)

  echo -e '1\t2\t3\t4\t5' | expand -t 1 | cut -d ' ' -f 3