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

科技回声

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

GitHubTwitter

首页

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

资源链接

HackerNews API原版 HackerNewsNext.js

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

Modernizing AWK, a 45-year old language, by adding CSV support

258 点作者 benhoyt大约 3 年前

28 条评论

jeroenjanssens大约 3 年前
I often use csvquote [1] whenever I need to process CSV with a command-line tool that doesn&#x27;t support it. For example:<p><pre><code> csvquote test.csv | awk &#x27;{print $1, $2}&#x27; | csvquote -u </code></pre> [1] <a href="https:&#x2F;&#x2F;github.com&#x2F;dbro&#x2F;csvquote" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;dbro&#x2F;csvquote</a>
评论 #31359282 未加载
评论 #31357282 未加载
db65edfc7996大约 3 年前
I have grown fond of using miller[0] to handle command line data processing. Handles the standard tabular formats (csv, tsv, json) and has all of the standard data cleanup options. Works on streams so (most operations) are not limited by memory.<p>[0]: <a href="https:&#x2F;&#x2F;github.com&#x2F;johnkerl&#x2F;miller" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;johnkerl&#x2F;miller</a>
评论 #31357541 未加载
评论 #31355117 未加载
mro_name大约 3 年前
I recently learned via <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=31257248" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=31257248</a> that ASCII has the idea of records and fields ever since. It&#x27;s just not used, but workaround CSV.<p>No improvement of CSV handling will ever improved on that.
评论 #31352401 未加载
评论 #31352170 未加载
评论 #31355262 未加载
评论 #31352616 未加载
评论 #31353019 未加载
adamgordonbell大约 3 年前
It&#x27;s somewhat a chore to use but gawkextlib has a CSV extension. so you can do this in gawk if the extension is loaded.<p><pre><code> @include &quot;csv&quot; BEGIN { CSVMODE = 1 } { print $2 } </code></pre> <a href="https:&#x2F;&#x2F;earthly.dev&#x2F;blog&#x2F;awk-csv&#x2F;#gawkextlib" rel="nofollow">https:&#x2F;&#x2F;earthly.dev&#x2F;blog&#x2F;awk-csv&#x2F;#gawkextlib</a>
jph大约 3 年前
Ben this is great, thank you. Would you consider adding Unicode Separated Values (USV)?<p><a href="https:&#x2F;&#x2F;github.com&#x2F;sixarm&#x2F;usv" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;sixarm&#x2F;usv</a><p>USV is like CSV and simpler because of no escaping and no quoting. I can donate $50 to you or your charity of choice as a token of thanks and encouragement.
评论 #31352006 未加载
评论 #31352284 未加载
评论 #31352305 未加载
cb321大约 3 年前
When you have &quot;format wars&quot;, the best idea is usually to have a converter program change to the easiest to work with format - <i>unless</i> this incurs a space explosion as per some image&#x2F;video formats.<p>With CSV-like data, bulk conversion from quoted-escaped RFC4180 CSV to a simpler-to-parse format is the best plan for several reasons. First, it may &quot;catch on&quot;, help Microsoft&#x2F;R&#x2F;whoever embrace the format and in doing so squash many bugs written by &quot;data analyst&#x2F;scientist coders&quot;. Second, in a shell &quot;<i>a|b</i>&quot; runs programs <i>a</i> &amp; <i>b</i> in parallel on multi-core and allow things like <i>csv2x|head -n10000|b</i> or <i>popen(&quot;csv2x foo.csv&quot;)</i>. Third, bulk conversion to a random access file where literal delimiters cannot occur as non-delimiters allows trivial file segmentation to be nCores times faster (under often satisfied assumptions). There are some D tools for this bulk convert in <a href="https:&#x2F;&#x2F;github.com&#x2F;eBay&#x2F;tsv-utils" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;eBay&#x2F;tsv-utils</a> and a much smaller stand-alone Nim tool <a href="https:&#x2F;&#x2F;github.com&#x2F;c-blake&#x2F;nio&#x2F;blob&#x2F;main&#x2F;utils&#x2F;c2tsv.nim" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;c-blake&#x2F;nio&#x2F;blob&#x2F;main&#x2F;utils&#x2F;c2tsv.nim</a> . Optional quoting was always going to be a PITA due to its non-locality. What if there is no quote anywhere? Fourth, by using a program as the unit of modularity in this case, you make things programming language agnostic. Someone could go to town and write a pure SIMD&#x2F;AVX512 converter in assembly even and solve the problem &quot;once and for all&quot; on a given CPU. The problem is actually just simple enough that this smells possible.<p>I am unaware of any &quot;document&quot; that &quot;standardizes&quot; this escaped&#x2F;lossless TSV format. { Maybe call it &quot;DSV&quot; for delimiter separated values where &quot;delimiters actually separate&quot;? Ironically redundant. ;-) } Someone want to write an RFC or point to one? It can be just as &quot;general&#x2F;lossless&quot; (see <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=31352170" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=31352170</a>).<p>Of course, if you are going to do a lot of data processing against some data, it is even better to parse all the way to down to binary so that you never have to parse again (well, unless you call CPUs loading registers &quot;parsing&quot;) which is what database systems have been doing since the 1960s.
评论 #31356342 未加载
rgoulter大约 3 年前
I&#x27;d be curious to see a comparison with the csvkit suite. <a href="https:&#x2F;&#x2F;csvkit.readthedocs.io&#x2F;en&#x2F;latest&#x2F;index.html" rel="nofollow">https:&#x2F;&#x2F;csvkit.readthedocs.io&#x2F;en&#x2F;latest&#x2F;index.html</a>
评论 #31351354 未加载
parasense大约 3 年前
I alway just use awk to process csv files.<p><pre><code> awk -F &#x27;^&quot;|&quot;,&quot;|&quot;$|,&#x27; &#x27;{print $2,$3}&#x27; whatever.csv </code></pre> The above works perfectly well, it handles quoted fields, or even just unquoted fields.... This snippet is taken from a presentation I give on AWK and BASH scripting.<p>That&#x27;s the thing about AWK, it&#x27;s already does everything. No need to extended it much at all.
评论 #31445866 未加载
andi999大约 3 年前
Can you also set the decimal separator? Some countries use &#x27;,&#x27; in numbere like 10,5
评论 #31356232 未加载
评论 #31351752 未加载
malkocoglu大约 3 年前
Modernizing ... by adding CSV support !?!
评论 #31351194 未加载
评论 #31351443 未加载
hawski大约 3 年前
It is a nice addition, but I would like to see this taken further - structural regular expression awk. It is waiting to be implemented for 35 years now.
asicsp大约 3 年前
&gt;A big thank-you to the library of the University of Antwerp, who sponsored this feature. They’re one of two major teams or projects I know of that use GoAWK – the other one is the Benthos stream processor.<p>That&#x27;s great to hear.<p>Are you planning to add support for xml, json, etc next? Something like Python&#x27;s `json` module that gives you a dictionary object.
评论 #31361745 未加载
rufugee大约 3 年前
What’s the best resource for learning modern awk these days? I’ve used it for decades, but only via memorized snippets…
评论 #31355825 未加载
altairprime大约 3 年前
For non-awk tools, csvformat (from csvkit) will unquote and re-delimeter a CSV file (-D\034 -U -B) into something that UNIX pipes can handle (cut -d\034, etc). It’s worth setting up as an alias, and you can store \034 in $D or whatever for convenience.
adolph大约 3 年前
For anything down and dirty, what&#x27;s wrong with -F&#x27;&quot;&#x27;? For anything fancy there are plenty of things like the below.<p><i>eBay&#x27;s TSV Utilities: Command line tools for large, tabular data files. Filtering, statistics, sampling, joins and more.</i><p>includes csv to tsv: <a href="https:&#x2F;&#x2F;github.com&#x2F;eBay&#x2F;tsv-utils" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;eBay&#x2F;tsv-utils</a><p>HT: <a href="https:&#x2F;&#x2F;simonwillison.net&#x2F;" rel="nofollow">https:&#x2F;&#x2F;simonwillison.net&#x2F;</a>
gpvos大约 3 年前
During a recent HN discussion on pipes and text versus structured objects to transfer data between programs, I started wondering if CSV wouldn&#x27;t be a nice middle ground.
评论 #31355087 未加载
评论 #31353699 未加载
ognyankulev大约 3 年前
Instead of yet another limited parser, it would be best if universal tabular data parsing is supported by allowing one to specify all important parsing parameters, as described in <a href="https:&#x2F;&#x2F;www.w3.org&#x2F;TR&#x2F;2015&#x2F;REC-tabular-metadata-20151217&#x2F;#dialect-descriptions" rel="nofollow">https:&#x2F;&#x2F;www.w3.org&#x2F;TR&#x2F;2015&#x2F;REC-tabular-metadata-20151217&#x2F;#di...</a>
评论 #31351611 未加载
tyingq大约 3 年前
Gnu awk also has a csv extension that comes with gawkextlib. I think it may even be installed on many Linux distros by default.
torginus大约 3 年前
I can&#x27;t tell whether the UNIX people have lost their way, or just the demands of modern shell scripts cannot be met by typical shell philosophy - that is, piping together the output of small, orthogonal utilities.<p>The emergence and constantly increasing complexity of these small, bespoke DSLs like this or jq does not inspire confidence in me.
评论 #31355551 未加载
评论 #31354029 未加载
评论 #31353300 未加载
uhtred大约 3 年前
Why not use FPAT: <a href="https:&#x2F;&#x2F;www.gnu.org&#x2F;software&#x2F;gawk&#x2F;manual&#x2F;html_node&#x2F;Splitting-By-Content.html" rel="nofollow">https:&#x2F;&#x2F;www.gnu.org&#x2F;software&#x2F;gawk&#x2F;manual&#x2F;html_node&#x2F;Splitting...</a>
评论 #31354222 未加载
wef大约 3 年前
Here&#x27;s another library I&#x27;ve been using for several years:<p><a href="http:&#x2F;&#x2F;lorance.freeshell.org&#x2F;csv&#x2F;" rel="nofollow">http:&#x2F;&#x2F;lorance.freeshell.org&#x2F;csv&#x2F;</a>
forgotpwd16大约 3 年前
A good and useful addition. There&#x27;s a mention to CSVMODE, a gawk library. I wonder if it could be extended to support the functionality that goawk&#x27;s `-i csv` has.
motohagiography大约 3 年前
Thank you! In non-technical environments, shell scripting with awk is a superpower, and it&#x27;s almost always on csv data.
mattewong大约 3 年前
thanks for this. am looking at the benchmarks. how do I get huge.csv? Don&#x27;t see how to fetch or generate
评论 #31345149 未加载
skanga大约 3 年前
I always use mawk for its performance. This may be worth a try ...
评论 #31351233 未加载
igtztorrero大约 3 年前
one step further towards GoUnix, Gonux or any name Gophers like!
评论 #31354039 未加载
评论 #31360301 未加载
WFHRenaissance大约 3 年前
Always had CSV support, it&#x27;s called `awk --field-separator=&quot;,&quot;`.
评论 #31358487 未加载
评论 #31358376 未加载
w0de0大约 3 年前
-F &#x27;,&#x27;