[HN Gopher] What does $0=$2 in awk do?
       ___________________________________________________________________
        
       What does $0=$2 in awk do?
        
       Author : sorcercode
       Score  : 45 points
       Date   : 2022-09-25 05:45 UTC (17 hours ago)
        
 (HTM) web link (kau.sh)
 (TXT) w3m dump (kau.sh)
        
       | pgporada wrote:
       | Wonderful. My old license plate was awk sed.
        
       | dietrichepp wrote:
       | Awk is such a weird tool--it's powerful and so few people know
       | how to leverage it.
       | 
       | Yesterday, someone in chat wanted to extract special comments
       | from their source code and turn them into a script for GDB to
       | run. That way they could set a break point like this:
       | void func(void) {         //d break       }
       | 
       | They had a working script, but it was slow, and I felt like most
       | of the heavy lifting could be done with a short Awk command:
       | awk -F'//d[[:space:]]+' \         'NF > 1 {print FILENAME ":" FNR
       | " " $2}' \         source/*.c
       | 
       | This one command find all of those special comments in all of
       | your source files. For example, it might print out something
       | like:                 source/main.c:105 break
       | source/lib.c:23 break
       | 
       | The idea of using //d[[:space:]]+ as the field separator was not
       | obvious, like many Awk tricks are to people who don't use Awk
       | often (that includes me).
       | 
       | (One of the other cases I've heard for using Awk is for deploying
       | scripts in environments where you're not permitted to install new
       | programs or do shell scripting, but somehow an Awk script is
       | excepted from the rules.)
        
         | wruza wrote:
         | _it 's powerful and so few people know how to leverage it_
         | 
         | Because otherwise it is useless. It has the same fate as AHK
         | scripting and similar little languages. The language may be
         | okay for its task, but if you cannot or do not use it for other
         | things (unlike e.g. perl) at least from time to time, chances
         | that you will learn _and remember_ it are low. People know sed
         | because regexps are everywhere. People use perl instead of awk,
         | because they have a muscle memory for it. They may know [, find
         | and glob for their relative generic-ness. They ignore awk and
         | ahk because these are too niche to pay enough attention to. You
         | either find a snippet or just move on.
         | 
         | If you are not constrained by a single line in a script, it's
         | easier to feed a heredoc into an interpreter of choice.
        
           | js2 wrote:
           | Network calculator written in awk because it's what was
           | available in a particular environment. It is not useless.
           | 
           | https://gist.github.com/jaysoffian/e41ca479d70e60efe59fded93.
           | ..
        
         | deepsun wrote:
         | Just like Perl. Most people prefer dumb verbose code, not smart
         | terse.
        
           | yakubin wrote:
           | The question I prefer to ask is "how much functionality can I
           | comprehend in a given amount of time" rather than "how many
           | lines of code can I comprehend in a given amount of time".
        
           | bobbylarrybobby wrote:
           | Most people prefer legible code, not hieroglyphics
        
             | scubbo wrote:
             | (Honest question) what do you feel that your comment added
             | to the one above it?
             | 
             | Are you suggesting that "dumb verbose code" might not be
             | legible (I suppose that's technically possible, but seems
             | unlikely to happen by accident)?
             | 
             | Or are you implying that Perl consists of "hieroglyphics"
             | and so is not a suitable language for writing legible code?
             | This, I think, would miss the point - deepsun was saying
             | that, in both Perl and in awk, readers prefer legible code
             | over cleverness - to claim that Perl cannot be legible at
             | _all_ requires a little more justification, and would
             | probably be disputed on the grounds that familiarity with a
             | language's conventions is often a prerequisite for
             | legibility.
        
               | cerved wrote:
               | not OP but perhaps they are trying to say that terse code
               | can be quite illegible
               | 
               | I've written sed sripts and more compilated regular
               | expressions that when I come back I don't remember what
               | all that mess was supposed to accomplish
        
         | statquontrarian wrote:
         | Unfortunately, the universal POSIX standard of awk only
         | supports single-character, non-regular expression field
         | separators (https://pubs.opengroup.org/onlinepubs/9699919799/ut
         | ilities/a...). It's arguable whether one should write POSIX-
         | compliant awk or not (similar arguments apply for shell
         | scripting).
         | 
         | When feasible, I try to write POSIX-compliant awk, so the
         | script could have been written as:                   awk
         | '/\/\/d / {gsub(/.*\/\/d /, ""); print FILENAME ":" FNR " " $0;
         | }' source/*.c
        
           | dietrichepp wrote:
           | I thought about this, but both the macOS Awk and GNU Awk
           | support regexps as field separators.
        
           | xani_ wrote:
           | I just write Perl instead
        
             | d3nj4l wrote:
             | yeah people say perl is write only but I guarantee it's
             | less write only than that awk incantation
        
               | dietrichepp wrote:
               | That "Awk incantation" looks clear to me.
               | 
               | - On lines that contain //d,
               | 
               | - Delete the part of the line up to and including //d,
               | 
               | - Print the file, line number, and the rest of the line.
               | 
               | Maybe it's familiarity with regular expressions? If
               | you're not familiar with regular expressions, that Awk is
               | gonna look a bit funny. The regular expressions look a
               | bit messy just because they have to match literal
               | slashes, so you get /\/\/
               | 
               | There is no use of funny features or clever tricks in the
               | code, it's just kind of straightforward, mindless code
               | that does exactly what it says it does. It's definitely
               | less clever than the Awk invocation that I wrote (which
               | is a good thing).
        
               | xani_ wrote:
               | Well, Perl kinda started where SED and AWK finished, and
               | it does incorporate a lot of syntax from them, for better
               | or worse.
               | 
               | It makes it easy for quick & powerful one-liners but
               | sadly people then put the one-liners into actual programs
               | instead of writing it in nicer way...
        
       | zwkrt wrote:
       | Awk, like vim, is a tool I love dearly that I absolutely would
       | never recommend someone else learn. It's like a form of mental
       | illness but it's so lodged in my brain and I'll give it up when
       | they put me in the grave.
        
         | orwin wrote:
         | Depends on the person and on his specifics. A CS student should
         | absolutely take time to learn to use these vim, awk, gdb etc.
         | For a self-learned dev who is already working and already have
         | his habits, i don't think this is worth his time.
        
       | bspammer wrote:
       | Elegant, but not something you should ever use outside of ad-hoc
       | situations.
       | 
       | I think this is more comprehensible, and is also more robust
       | because it actually specifies the field you're looking for rather
       | than just any line with quotes:
       | 
       | gawk -F'"' '/^ name:/ {print $2}' appVersion.gradle
       | 
       | (hacker news is clobbering the spaces after ^)
        
         | Izkata wrote:
         | > (hacker news is clobbering the spaces after ^)
         | 
         | Indent with 2 spaces to get code formatting:
         | https://news.ycombinator.com/formatdoc
        
         | ISL wrote:
         | That's way more comprehensible and maintainable.
        
         | layer8 wrote:
         | It's a pity that _awk_ doesn't support capture groups in line
         | patterns. Then you could get rid of the field separator and
         | make the script even more comprehensible. (You can simulate
         | this with _match()_ in _gawk_ , but then you must know how
         | _match()_ works.)
         | 
         | Personally, I'd probably rather use _sed_ here:
         | sed -nE 's/^\s*name:\s*"([^"]*)"\s*$/\1/p"
         | 
         | While that regex is more complex, it is also safer and more
         | explicit.
        
       | scubbo wrote:
       | Off-topic, but I _love_ the "sidenote" format for footnotes. I've
       | been meaning to implement that in my own blog for a while, now.
       | I'll check out the source for inspiration.
        
       ___________________________________________________________________
       (page generated 2022-09-25 23:00 UTC)