TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

What does $0=$2 in awk do?

131 点作者 sorcercode超过 2 年前

14 条评论

asicsp超过 2 年前
Here are some more learning resources:<p>* <a href="https:&#x2F;&#x2F;backreference.org&#x2F;2010&#x2F;02&#x2F;10&#x2F;idiomatic-awk&#x2F;" rel="nofollow">https:&#x2F;&#x2F;backreference.org&#x2F;2010&#x2F;02&#x2F;10&#x2F;idiomatic-awk&#x2F;</a> — how to write more idiomatic (and usually shorter and more efficient) awk programs<p>* <a href="https:&#x2F;&#x2F;learnbyexample.github.io&#x2F;learn_gnuawk&#x2F;preface.html" rel="nofollow">https:&#x2F;&#x2F;learnbyexample.github.io&#x2F;learn_gnuawk&#x2F;preface.html</a> — my ebook on GNU awk one-liners, plenty of examples and exercises<p>* <a href="https:&#x2F;&#x2F;www.grymoire.com&#x2F;Unix&#x2F;Awk.html" rel="nofollow">https:&#x2F;&#x2F;www.grymoire.com&#x2F;Unix&#x2F;Awk.html</a> — covers information about different `awk` versions as well<p>* <a href="https:&#x2F;&#x2F;earthly.dev&#x2F;blog&#x2F;awk-examples&#x2F;" rel="nofollow">https:&#x2F;&#x2F;earthly.dev&#x2F;blog&#x2F;awk-examples&#x2F;</a> — start with short Awk one-liners and build towards a simple program to process book reviews
评论 #32980650 未加载
bspammer超过 2 年前
Elegant, but not something you should ever use outside of ad-hoc situations.<p>I think this is more comprehensible, and is also more robust because it actually specifies the field you&#x27;re looking for rather than just any line with quotes:<p>gawk -F&#x27;&quot;&#x27; &#x27;&#x2F;^ name:&#x2F; {print $2}&#x27; appVersion.gradle<p>(hacker news is clobbering the spaces after ^)
评论 #32977153 未加载
评论 #32977066 未加载
评论 #32979776 未加载
评论 #32979938 未加载
评论 #32976767 未加载
dietrichepp超过 2 年前
Awk is such a weird tool--it&#x27;s powerful and so few people know how to leverage it.<p>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:<p><pre><code> void func(void) { &#x2F;&#x2F;d break } </code></pre> 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:<p><pre><code> awk -F&#x27;&#x2F;&#x2F;d[[:space:]]+&#x27; \ &#x27;NF &gt; 1 {print FILENAME &quot;:&quot; FNR &quot; &quot; $2}&#x27; \ source&#x2F;*.c </code></pre> This one command find all of those special comments in all of your source files. For example, it might print out something like:<p><pre><code> source&#x2F;main.c:105 break source&#x2F;lib.c:23 break </code></pre> The idea of using &#x2F;&#x2F;d[[:space:]]+ as the field separator was not obvious, like many Awk tricks are to people who don&#x27;t use Awk often (that includes me).<p>(One of the other cases I&#x27;ve heard for using Awk is for deploying scripts in environments where you&#x27;re not permitted to install new programs or do shell scripting, but somehow an Awk script is excepted from the rules.)
评论 #32976520 未加载
评论 #32976898 未加载
评论 #32976468 未加载
评论 #32980209 未加载
zwkrt超过 2 年前
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.
评论 #32976967 未加载
评论 #32980414 未加载
scubbo超过 2 年前
Off-topic, but I _love_ the &quot;sidenote&quot; format for footnotes. I&#x27;ve been meaning to implement that in my own blog for a while, now. I&#x27;ll check out the source for inspiration.
评论 #32979196 未加载
评论 #32977526 未加载
xorcist超过 2 年前
Do not do this in awk when the same in sed would be easier to read.<p>But also do not match this data format in the first quote. Match &quot;name:&quot; instead if that is what you mean. This makes the intention clear.<p>For matching text, just use grep is that is what most people would expect:<p><pre><code> grep -m 1 &quot;name:&quot; appVersion.gradle | grep -o &quot;[0-9.]&quot; </code></pre> You can shorten it to one regexp if you use an extended format that can handle backrefs if the context would make that clearer.
jrm4超过 2 年前
Yeah, this just proves to me that the intuitive shell tools are way better. I&#x27;ll keep all the heads, tails, cuts, and trs, etc.
评论 #32977512 未加载
评论 #32980334 未加载
pgporada超过 2 年前
Wonderful. My old license plate was awk sed.
评论 #32978185 未加载
lugao超过 2 年前
As others here pointed out: this is bad awk.<p>I think good awk scripts lie somewhere between &quot;This is atrociously overfit to this file&quot; and &quot;this is so general you should have done it in python&#x2F;perl&#x2F;etc&quot;.<p>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.<p>Short awk != good awk.
FPGAhacker超过 2 年前
&gt; This is really such a gorgeous piece of code. Clever and poetic.<p>I suppose that is subjective, but I think this is terrible code.<p>The fact that someone had to write a lengthy blogpost to figure out what it was doing should be an obvious warning against it.<p>Write code for readability &#x2F; comprehensibility.
评论 #32982526 未加载
ketanmaheshwari超过 2 年前
Too 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.
评论 #32982577 未加载
pmarreck超过 2 年前
Awk strikes me as a marvelous text parsing tool that too few understand.
dark-star超过 2 年前
$0=$2 sounds a lot like my wallet recently...
DesiLurker超过 2 年前
takes 2 dollars and spends it!