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

科技回声

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

GitHubTwitter

首页

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

资源链接

HackerNews API原版 HackerNewsNext.js

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

Using command line to process CSV files (2022)

161 点作者 mr_o47将近 2 年前

36 条评论

benhoyt将近 2 年前
Unfortunately &quot;awk -F,&quot; (field separator of comma) doesn&#x27;t work with most real CSV files, because of quoted fields, commas in fields, and (less frequently) multiline fields. My GoAWK implementation has a CSV mode activated with &quot;goawk -i csv&quot; (input mode CSV) and some other CSV features that properly handle quoted and multiline fields: <a href="https:&#x2F;&#x2F;benhoyt.com&#x2F;writings&#x2F;goawk-csv&#x2F;" rel="nofollow noreferrer">https:&#x2F;&#x2F;benhoyt.com&#x2F;writings&#x2F;goawk-csv&#x2F;</a><p>The frawk tool (written in Rust) also supports this.<p>Interestingly, Brian Kernighan is currently updating the book &quot;The AWK Programming Language&quot; for a second edition (I&#x27;m one of the technical reviewers), and Gawk and awk are adding a &quot;--csv&quot; option for this purpose. So real CSV mode is coming to an AWK near you soon!
评论 #36504183 未加载
评论 #36504436 未加载
评论 #36506845 未加载
评论 #36504981 未加载
评论 #36503623 未加载
评论 #36506658 未加载
评论 #36507550 未加载
wenc将近 2 年前
For any kind of tabular data, I use DuckDB (parallel CSV reads, works on small to humongous CSVs). It supports full Postgres-like SQL, so you can do arbitrarily complex manipulations on columns and rows. The CSV essentially becomes a dataframe object that can be manipulated in performant ways (DuckDB vectorizes and paralllelizes, so it&#x27;s way faster than awk on large CSV files). You can even manipulate multiple CSVs in a single statement (JOINs, UNIONs etc).<p><pre><code> % duckdb -c &quot;from &#x27;test.csv&#x27;&quot; ┌───────┬───────┬───────┐ │ A │ B │ C │ │ int64 │ int64 │ int64 │ ├───────┼───────┼───────┤ │ 1 │ 2 │ 3 │ │ 4 │ 5 │ 6 │ │ 7 │ 8 │ 9 │ └───────┴───────┴───────┘ % duckdb -c &quot;select sum(C) from &#x27;test.csv&#x27;&quot; ┌────────┐ │ sum(C) │ │ int128 │ ├────────┤ │ 18 │ └────────┘ % duckdb -c &quot;select sum(A + 2*B + C^2) from &#x27;test.csv&#x27;&quot; ┌────────────────────────────────┐ │ sum(((A + (2 * B)) + (C ^ 2))) │ │ double │ ├────────────────────────────────┤ │ 168.0 │ └────────────────────────────────┘</code></pre>
评论 #36502737 未加载
评论 #36502394 未加载
评论 #36502690 未加载
ldmosquera将近 2 年前
Have a look at visidata, a TUI table viewer&#x2F;editor with vim keybindings. It&#x27;s incredibly powerful and fully automatable - everything you do gets recorded as a series of commands which you can save and replay.<p>Not the right tool for everything, but it shines for quickly glancing at the shape of tabular data and making sense of it. Sorting, filtering, joins across files, column histograms and even column splitting&#x2F;rejoining are all keystrokes away.<p>It groks anything even remotely table shaped like CSV, JSONL, JSON, even Excel files, it can even directly connect to databases and parse tables out of HTML.<p><a href="https:&#x2F;&#x2F;www.visidata.org&#x2F;" rel="nofollow noreferrer">https:&#x2F;&#x2F;www.visidata.org&#x2F;</a>
评论 #36502699 未加载
评论 #36503088 未加载
sklarsa将近 2 年前
Personally, I use xsv and it’s been tremendously helpful, especially when working with larger files. <a href="https:&#x2F;&#x2F;github.com&#x2F;BurntSushi&#x2F;xsv">https:&#x2F;&#x2F;github.com&#x2F;BurntSushi&#x2F;xsv</a>
评论 #36503601 未加载
评论 #36504128 未加载
kippinitreal将近 2 年前
Cool stuff! But it’s criminal to not call attention to JQ’s elder sibling CSVKit. It’s invaluable for playing with csvs. Much easier to parse out columns, allows you to generate new csvs and even merge them. More importantly, it allows SQL on csvs (via SQLite iirc) which empowers all sorts of csv shenanigans. The bash scripting this enables us incredible (good and bad).<p><a href="https:&#x2F;&#x2F;csvkit.readthedocs.io&#x2F;en&#x2F;latest&#x2F;" rel="nofollow noreferrer">https:&#x2F;&#x2F;csvkit.readthedocs.io&#x2F;en&#x2F;latest&#x2F;</a>
评论 #36503310 未加载
pradeepchhetri将近 2 年前
As an SRE, I used to install one tool for every data format until I came across clickhouse-local. Now I use it for everything.<p>- It works with every data format which I come across in my daily job. It support data formats like Protobuf, Avro, Cap&#x27;n Proto which regular tools don&#x27;t support. Funny thing, it can even read mysql dumps.<p>- I can read the data stored in local or remote location like http, s3, gcs, azure and whatever location I can think of.<p>- I can SQL queries on the raw data and improve my SQL skills on daily basis.
评论 #36503095 未加载
评论 #36503630 未加载
globular-toast将近 2 年前
None of this stuff works.<p>Whenever CSV comes up I always feel a bit sad. ASCII includes a set of four out-of-band delimiters[0] that can be used instead of silly formats like CSV that use in-band delimiters which necessitate complicated quoting rules.<p>You can&#x27;t just treat CSV as text. It&#x27;s not and woe betide you if you ever use this stuff in a script instead of using a proper CSV parsing tool. If we used the ASCII delimiters instead it would be possibly to treat it as text and stuff like this would work.<p>[0] <a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Delimiter#ASCII_delimited_text" rel="nofollow noreferrer">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Delimiter#ASCII_delimited_text</a>
评论 #36505562 未加载
评论 #36504893 未加载
评论 #36511936 未加载
woodruffw将近 2 年前
It&#x27;s worth noting that a lot of these tricks will break on non-trivial CSV inputs, e.g. ones that contain escaped commas.<p>I like using the shell (and Awk in particular!) as much as anyone, but for CSV I tend to reach for Python&#x27;s standard csv module[1].<p>[1]: <a href="https:&#x2F;&#x2F;docs.python.org&#x2F;3&#x2F;library&#x2F;csv.html" rel="nofollow noreferrer">https:&#x2F;&#x2F;docs.python.org&#x2F;3&#x2F;library&#x2F;csv.html</a>
评论 #36503624 未加载
vajdagabor将近 2 年前
Nushell is also quite powerful for this. For example:<p><pre><code> &gt; open people.csv | where status == &#x27;customer&#x27; | unique-by email | select surname forename email | sort-by email | save customers.json </code></pre> <a href="https:&#x2F;&#x2F;www.nushell.sh&#x2F;" rel="nofollow noreferrer">https:&#x2F;&#x2F;www.nushell.sh&#x2F;</a>
评论 #36507783 未加载
zX41ZdbW将近 2 年前
My favorite tool is clickhouse-local: <a href="https:&#x2F;&#x2F;clickhouse.com&#x2F;blog&#x2F;extracting-converting-querying-local-files-with-sql-clickhouse-local" rel="nofollow noreferrer">https:&#x2F;&#x2F;clickhouse.com&#x2F;blog&#x2F;extracting-converting-querying-l...</a><p>It is the most powerful (works with any formats, with remote datasets, and supports all the ClickHouse SQL) and the most performant.
评论 #36502640 未加载
tanin将近 2 年前
In my day job, a customer would ask me to reconcile between 2 giant CSVs very often. Think GBs.<p>When I tell them to do it in Excel by themselves, they would say Excel couldn&#x27;t open a CSV larger than 1M rows...<p>At first, I was using sqlite through shell. I hated it so much that I built a desktop app on top of it. It wasn&#x27;t slick enough with all the typing.<p>It is quite a joy to use, and I&#x27;d love for people to try it out: <a href="https:&#x2F;&#x2F;superintendent.app" rel="nofollow noreferrer">https:&#x2F;&#x2F;superintendent.app</a> (disclaimer: I&#x27;m the creator).
评论 #36504272 未加载
评论 #36504878 未加载
评论 #36503263 未加载
dash2将近 2 年前
There&#x27;s a nice tool called dplyr-cli which lets you use R&#x27;s dplyr data manipulation language on the command line.<p><pre><code> cat mtcars.csv | group_by cyl | summarise &quot;mpg = mean(mpg)&quot; | kable #&gt; | cyl| mpg| #&gt; |---:|--------:| #&gt; | 4| 26.66364| #&gt; | 6| 19.74286| #&gt; | 8| 15.10000| </code></pre> <a href="https:&#x2F;&#x2F;github.com&#x2F;coolbutuseless&#x2F;dplyr-cli">https:&#x2F;&#x2F;github.com&#x2F;coolbutuseless&#x2F;dplyr-cli</a>
hfkwer将近 2 年前
These days I just use powershell. It has built-in csv import and then I&#x27;m dealing with familiar pwsh&#x2F;.net objects. I don&#x27;t miss the days of learning bespoke tools to handle slightly different cases.
snthpy将近 2 年前
Shameless plug: I created prql-query (<a href="https:&#x2F;&#x2F;github.com&#x2F;PRQL&#x2F;prql-query">https:&#x2F;&#x2F;github.com&#x2F;PRQL&#x2F;prql-query</a>) in order to scratch my own itch and use PRQL (prql-lang.org) with DataFusion and DuckDB for things like this.<p>pq is overdue some maintenance but I will update it with the imminent PRQL 0.9 release.<p>I&#x27;ve used pq in anger at my $dayjob and found it incredibly productive to have the full power of SQL combined with the terse and logical syntax of PRQL.
chrisshroba将近 2 年前
I have this shell function defined:<p>csv_to_json () { python -c &#x27;import csv, json, sys; print(json.dumps([dict(r) for r in csv.DictReader(sys.stdin)]))&#x27; | jq . }<p>It converts a csv to a json list of objects, mapping column names to values. I find it way easier to then operate on json by filtering with jq or gron, or just pasting it into other tools for post-processing. The jq at the end isn&#x27;t necessary but makes for nice formatting!
评论 #36669655 未加载
评论 #36509134 未加载
dima55将近 2 年前
Lots of tools do this sort of thing. An incomplete list is in the vnlog docs: <a href="https:&#x2F;&#x2F;github.com&#x2F;dkogan&#x2F;vnlog&#x2F;#description">https:&#x2F;&#x2F;github.com&#x2F;dkogan&#x2F;vnlog&#x2F;#description</a>
评论 #36502587 未加载
LispSporks22将近 2 年前
Ruby&#x27;s CSV module can be handy:<p><pre><code> ruby -rcsv -ne &#x27;CSV($&lt;).each { |r| puts r[0] }&#x27; </code></pre> I like the seen example the dude has:<p><pre><code> &#x27;!seen[$1]++&#x27;</code></pre>
评论 #36509308 未加载
评论 #36508605 未加载
thibran将近 2 年前
Nushell can open and write CSV. Those AWK commands look horrible compared to the nu syntax.<p><a href="https:&#x2F;&#x2F;www.nushell.sh&#x2F;commands&#x2F;docs&#x2F;from_csv.html" rel="nofollow noreferrer">https:&#x2F;&#x2F;www.nushell.sh&#x2F;commands&#x2F;docs&#x2F;from_csv.html</a>
评论 #36507810 未加载
jiehong将近 2 年前
If you use powershell, you can directly run Import-Csv and don’t really have to think about it.
oofnik将近 2 年前
Many command-line CSV parsing tools mentioned here, adding my choice to the list:<p><a href="http:&#x2F;&#x2F;harelba.github.io&#x2F;q&#x2F;" rel="nofollow noreferrer">http:&#x2F;&#x2F;harelba.github.io&#x2F;q&#x2F;</a>
asicsp将近 2 年前
For field extraction (like the first example: `awk -F, &#x27;{print $1}&#x27;`), you can also use `cut -d, -f1`<p>GNU datamash (<a href="https:&#x2F;&#x2F;www.gnu.org&#x2F;software&#x2F;datamash&#x2F;" rel="nofollow noreferrer">https:&#x2F;&#x2F;www.gnu.org&#x2F;software&#x2F;datamash&#x2F;</a>) provides features like groupby, statistical operations, etc.<p>See also this free ebook: Data Science at the Command Line (<a href="https:&#x2F;&#x2F;jeroenjanssens.com&#x2F;dsatcl&#x2F;" rel="nofollow noreferrer">https:&#x2F;&#x2F;jeroenjanssens.com&#x2F;dsatcl&#x2F;</a>)
two_handfuls将近 2 年前
I think the lesson here is don’t use awk for CSV. Instead, use one of the many tools discussed in comments that knows how to handle CSV.<p>Some are very close to awk in spirit, like my own attempt: `pawk` (1). It will parse your csv just fine. Or tsv. Or JSON or YAML or TOML. Or Parquet, even.<p>1: <a href="https:&#x2F;&#x2F;github.com&#x2F;jean-philippe-martin&#x2F;pawk">https:&#x2F;&#x2F;github.com&#x2F;jean-philippe-martin&#x2F;pawk</a>
PhilippGille将近 2 年前
Many of the tools shared in this thread simplify working with CSV files, but only some allow running proper SQL queries.<p>SQLite, DuckDB and Clickhouse-local have been mentioned, but another very simple one, a single dependency-free binary, is <a href="https:&#x2F;&#x2F;github.com&#x2F;multiprocessio&#x2F;dsq">https:&#x2F;&#x2F;github.com&#x2F;multiprocessio&#x2F;dsq</a><p>Not affiliated, just a happy user
评论 #36516621 未加载
nbk_2000将近 2 年前
Just thought I&#x27;d plug Octosql[1] which I&#x27;ve enjoyed using for this. It parses CSV and JSON, which are the file types I parse the most.<p>[1] <a href="https:&#x2F;&#x2F;github.com&#x2F;cube2222&#x2F;octosql&#x2F;">https:&#x2F;&#x2F;github.com&#x2F;cube2222&#x2F;octosql&#x2F;</a>
zh3将近 2 年前
&#x27;tr&#x27; and &#x27;cut&#x27; are also very useful; &#x27;tr&#x27; can be used to get rid of extra spaces and to conver commas to spaces and vice versa (and to handle text data with pretty much any character used as a separator).<p><pre><code> &#x27;cat $SPACE_SEPARATED_FILE | tr -s &#x27; &#x27; | tr &#x27; &#x27; &#x27;,&#x27; &gt; out.csv </code></pre> Allied with &#x27;cut&#x27;, it becomes easy to pull particular fields out of a text file:-<p><pre><code> &#x27;cat $FILE_WITH_COMMAS_AND_SPACES | tr &#x27;,&#x27; &#x27; &#x27; | tr -s &#x27; &#x27; | cut -d &#x27; &#x27; -f1,2,17 &gt; out.txt</code></pre>
greazy将近 2 年前
I recommend everyone checkout the very cool csvtk by the amazing bioinformstician Shen Wei<p><a href="https:&#x2F;&#x2F;github.com&#x2F;shenwei356&#x2F;csvtk">https:&#x2F;&#x2F;github.com&#x2F;shenwei356&#x2F;csvtk</a>
hermitcrab将近 2 年前
Presumably this command:<p>awk -F, &#x27;{print $1}&#x27; file.csv<p>Doesn&#x27;t work if the data contains commas (with escapes)? If so, that might be worth spelling out.
bobnamob将近 2 年前
I&#x27;ve defaulted to doing all my data munging in a clojure repl.<p>The datasets that I work with are well small enough to fit in memory on my machine and having the full java ecosystem + clojure ergonomics is worth more to me than the performance full db tooling might offer
petre将近 2 年前
I use miller, as it&#x27;s available on my distro.
pyeri将近 2 年前
Python&#x27;s pandas library has ability to seamlessly work with csv by importing&#x2F;exporting them as data frames.
SPBS将近 2 年前
I don&#x27;t understand why this is so high when it&#x27;s just using awk to naively split on commas. Any programming language could do that! I was expecting an actual command line tip that can handle CSV files in general, looks like it&#x27;s still basically impossible without resorting to a full blown CSV parser (none of which come installed by default).
replwoacause将近 2 年前
I use PowerShell for this and it works very well.
activiation将近 2 年前
Interesting but not very intuitive.
kohlerm将近 2 年前
xsv works pretty well for me
cutler将近 2 年前
Title should have been &quot;Using awk to process CSV files&quot;.
9735194将近 2 年前
Can’t wait for the tutorial on ‘ls’. It’s not like there is a manual.