7 ms·
What does $0=$2 in awk do?
- zwkrt 4y agoAwk, 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 4y agoDepends 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.
- capableweb 4y agoWhy is it worth the time of a CS student and not worth the time for a self-learned developer? Feels like the reasoning should be something like; either learning it makes you more efficient, or it doesn't make you more efficient. If it does, learn it, if it doesn't, don't, regardless of your background.
- fragmede 4y agoThe opportunity cost of time is not zero. If you already have tools which are working for you, time is (possibly) better spent learning other things.
- chii 4y ago> For a self-learned dev who is already working and already have his habits this implies that the self-learned dev has habits that are just as efficient as the unix toolset being recommended for the CS student. But you dont know if that's actually true - it might be for some people, but not for others. It's a skill for someone to have, to find out whether their current toolkit is not good, and that a better one exists.
- orwin 4y agoLearning this will make you more efficient, always. Using gdb (pdb for me rn, but it's the same) or vim especially, but you have to consider your working week, the habits you forged, and like a sibling said, the opportunity cost. Let's be honest, CS students do have time to try thing, do vim tutor or regex golf (yeah, add regex to the list too) and other stuff like that. Once you're working, you loose some agency. And gain some. Recently at a daily, i proposed to help write my coworker's regex. He is a fine dev/ops guy, but self taught (ex electronics guy) and miss some basics that aren't useful 99% of the time. He could've written his regex without help, but this is typically the case where getting more efficient isn't really worth the cost once you're working.
- nmz 4y agoNobody should learn vim when vis exists.
- orwin 4y agoReplace vim by a modal editor.
- ComputerGuru 4y agoExcept you have to recreate the awesome plugins and community that exist around vim for vis. I wish they existed.
- nmz 4y agoAnd this is why we CS never advances and is still stuck in the 70's. well, just linux.
- randmeerkat 4y ago> A CS student should absolutely take time to learn to use these vim, awk, gdb etc. You forgot emacs.
- BenjiWiebe 4y agoHe mentioned vim.
- randmeerkat 4y ago> He mentioned vim. He did, but just as less is more vi and emacs should always be mentioned together.
- orwin 4y agoI shouldn't have mentionned vim. What people should learn is to use any modal editor, the specifics aren't important.
- bongoman37 4y ago
- ketanmaheshwari 4y agoToo much text to explain what could have been explained clearly in 3-4 sentences. Making simple things complicated and then writing a long essay with dramatic phrases is a disservice to awk.
- anony23 4y agoIt's not about explaining the one liner, it was more of an awk tutorial.
- pgporada 4y agoWonderful. My old license plate was awk sed.
- macintux 4y agoCompletely off topic now, but there’s a local politician named “Cat Ping.” I figured she has the UNIX enthusiast vote wrapped up.
- fsckboy 4y agoif you do an image search for Cat Ping, she faces some challenges gaining recognition https://duckduckgo.com/?t=ffcm&q=Cat+Ping&iax=images&ia=images https://duckduckgo.com/?t=ffcm&q=Cat+Ping&iax=images&ia=imag...
- dietrichepp 4y agoAwk 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.)
- deepsun 4y agoJust like Perl. Most people prefer dumb verbose code, not smart terse.
- yakubin 4y agoThe 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 4y agoMost people prefer legible code, not hieroglyphics
- scubbo 4y ago(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.
- bspammer 4y agoElegant, 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 ^)
- ISL 4y agoThat's way more comprehensible and maintainable.
- layer8 4y agoIt’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.
- nine_k 4y agoGnu ask does support them. MacOS cli userland is less capable by default.
- layer8 4y agoYou can't reference the results of capture groups in line patterns in gawk, unless you use the match() function. You can write: match($0, /...(...).../, arr) { ...refer to arr[i]... } But you can't write: /...(...).../ { ...refer to a capture group somehow... }
- asicsp 4y agoGrep is often better suited for extracting matched portion, especially if you also have PCRE option: grep -oP -m1 'name:\s*"\K[^"]+'
- 4y ago
- scubbo 4y agoOff-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.
- LanternLight83 4y agoLove the sound of it, but don't see this on mobile, even in desktop mode, must be tied to a media query; I'll be checking it out later too c:
- sorcercode 4y agofor the kind words and noticing . I enable the sidenotes based on how “wide” your current viewport is. I’ve just personally found they don’t work as well on smaller screens. I wrote it with simple jQuery. Happy to share if anyone is interested and don’t want to have to write it from scratch.
- scubbo 4y agoThanks a ton!
- hivacruz 4y agoThe author made a post about it: https://kau.sh/blog/jekyll-footnote-tufte-sidenote/ https://kau.sh/blog/jekyll-footnote-tufte-sidenote/
- jrm4 4y agoYeah, this just proves to me that the intuitive shell tools are way better. I'll keep all the heads, tails, cuts, and trs, etc.
- LanternLight83 4y agoI think awk and sed are great, and it's great to eschew pipes when a powerful component that's ready in the pipelibe, like sed, awk, or grep, can pull double duty, but do think that one should favor using `grep -e` with a positive look behind in a case like this. I can just picture an innocuous string being added anywhere above the version string and throwing this one-liner off, when a regex could have pulled the version string out from an arbitrary position within the file and would hold up much more robustly; You don't want to impose opaque constraints on future edits even if you're the only one who will be editing the file, it's just too easy to forget and trip over later.
- jrm4 4y agoI'm whatever the opposite of a code golf extremist is. E.g. I hate the so called "useless use of cat." I uselessly use cat all the dang time; visualizing the pipeline is infinity more useful then, what, "elegance?" Who cares?
- piperswe 4y agoSame here. I use `cat` as a tool to read data from the filesystem into a pipeline, regardless if that's the initial intention of the tool. I don't want the input filename being to the right of the first manipulation command in a pipeline that's supposed to read left to right!
- venil 4y agoIts actually part of the POSIX standard that redirections can be put anywhere in the command line, so one can do: <file.txt sed 's/some/filter' | other_cmd on any standard compliantish shell :) (I use zsh, and I know it also works for bash and dash)
- asicsp 4y agoHere are some more learning resources: * https://backreference.org/2010/02/10/idiomatic-awk/ https://backreference.org/2010/02/10/idiomatic-awk/ — how to write more idiomatic (and usually shorter and more efficient) awk programs * https://learnbyexample.github.io/learn_gnuawk/preface.html https://learnbyexample.github.io/learn_gnuawk/preface.html — my ebook on GNU awk one-liners, plenty of examples and exercises * https://www.grymoire.com/Unix/Awk.html https://www.grymoire.com/Unix/Awk.html — covers information about different `awk` versions as well * https://earthly.dev/blog/awk-examples/ https://earthly.dev/blog/awk-examples/ — start with short Awk one-liners and build towards a simple program to process book reviews
- vram22 4y agoAlso, the original awk book is available on the Internet Archive, IIRC. https://www.google.com/search?q=site%3Aarchive.org+the+awk+programming+language https://www.google.com/search?q=site%3Aarchive.org+the+awk+p...
- DesiLurker 4y agotakes 2 dollars and spends it!
- FPGAhacker 4y ago> This is really such a gorgeous piece of code. Clever and poetic. I suppose that is subjective, but I think this is terrible code. The fact that someone had to write a lengthy blogpost to figure out what it was doing should be an obvious warning against it. Write code for readability / comprehensibility.
- sorcercode 4y agoI understand the sentiment. As a software engineer I refrain from clever code especially if somebody else has to come and maintain it. In this case, once you realize how the defaults stack, whoever came up with the one liner indeed has come up with something clever. Also the exercise in understanding the defaults cements the understanding of awk and as others have pointed out enables you to write much cleaner awk.
- xorcist 4y agoDo not do this in awk when the same in sed would be easier to read. But also do not match this data format in the first quote. Match "name:" instead if that is what you mean. This makes the intention clear. For matching text, just use grep is that is what most people would expect: grep -m 1 "name:" appVersion.gradle | grep -o "[0-9.]" You can shorten it to one regexp if you use an extended format that can handle backrefs if the context would make that clearer.
- dark-star 4y ago$0=$2 sounds a lot like my wallet recently...
- lugao 4y agoAs others here pointed out: this is bad awk. I think good awk scripts lie somewhere between "This is atrociously overfit to this file" and "this is so general you should have done it in python/perl/etc". Of course there are legitimate uses for both super-specific and super-general awk scripts, but finding the right compromise is what makes a good awk script. You want your script to be concise yet robust to changes in the input files. Also, readability and maintainability are really important if you plan to add it to an important script. Short awk != good awk.
- pmarreck 4y agoAwk strikes me as a marvelous text parsing tool that too few understand.