Hide

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again.

Delete that bloated snippets file you've been using and share your personal repository with the world. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.


If you have a new feature suggestion or find a bug, please get in touch via http://commandlinefu.uservoice.com/

Get involved!

You can sign-in using OpenID credentials, or register a traditional username and password.

First-time OpenID users will be automatically assigned a username which can be changed after signing in.

Hide

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for:

Hide

News

2011-03-12 - Confoo 2011 presentation
Slides are available from the commandlinefu presentation at Confoo 2011: http://presentations.codeinthehole.com/confoo2011/
2011-01-04 - Moderation now required for new commands
To try and put and end to the spamming, new commands require moderation before they will appear on the site.
2010-12-27 - Apologies for not banning the trolls sooner
Have been away from the interwebs over Christmas. Will be more vigilant henceforth.
2010-09-24 - OAuth and pagination problems fixed
Apologies for the delay in getting Twitter's OAuth supported. Annoying pagination gremlin also fixed.
Hide

Tags

Hide

Functions

Commands tagged CSV from sorted by
Terminal - Commands tagged CSV - 10 results
# su -c "psql -d maillog -c \"copy (select date,sender,destination,subject from maillog where destination like '%domain.com%') to '/tmp/mails.csv' with csv;\" " postgres
2013-02-13 13:03:17
User: Risthel
Functions: su
0

This command will "su" the execution of the command to the postgres user(implies that you are already logger as root), and export the result of the query to a file on the csv format.

You'll need to adequate the fields and database information to one of your choice/need.

for i in `ls *.anki`; do sqlite3 $i "select (cards.question || '||' || cards.answer) from cards, facts where cards.factid=facts.id and facts.tags like '%mytag%';" >> mytag.csv; done
2012-03-13 19:03:20
Tags: CSV anki
0

Produces a CSV file containing fronts/backs of cards with specified tag ("mytag" above). This command pulls these cards from different card databases, and allows them to be merged into one (by importing the resulting CSV file).

The CSV file is not directly produced; instead of commas, "||" are inserted. In your editor of choice, modify the resulting file to put quotes around the text before || and after ||, then change || to a comma (for every line).

sed 's/$/uniqueString/' file.old | sed 's/,/\n/g' | sed ':loop;/^\"[^\"]*$/N;s/\n/,/;/[^\"]$/t loop' | sed ':loop;N;s/\n/@/g;/uniqueString$/!b loop;s/uniqueString$//' > file.new
2012-01-06 10:06:40
User: moogmusic
Functions: sed
Tags: sed CSV delimiter
0

Useful for CSV files. In the command, the file in question is comma delimited but contains double quoted fields containing commas and contains no @ symbols (as confirmed with http://www.commandlinefu.com/commands/view/9998/delimiter-hunting). This command converts the delimiting commas to @s while preserving the commas in the fields using the "uniqueString" to mark the ends of lines.

csvde -f test.csv
sqlite3 -line database.db
2010-10-09 16:10:19
User: pykler
Tags: CSV html sql sqlite
-2

Similar output to using MySQL with the \G at the end of a Query. Displays one column per line. Other modes include:

-column

Query results will be displayed in a table like form, using whitespace characters to separate the columns and align the output.

-html Query results will be output as simple HTML tables.

-line Query results will be displayed with one value per line, rows separated by a blank line. Designed to be easily parsed by scripts or other programs

-list Query results will be displayed with the separator (|, by default) character between each field value. The default.

From inside the command line this can be also changed using the mode command:

.mode MODE ?TABLE? Set output mode where MODE is one of:

csv Comma-separated values

column Left-aligned columns. (See .width)

html HTML code

insert SQL insert statements for TABLE

line One value per line

list Values delimited by .separator string

tabs Tab-separated values

tcl TCL list elements

python -c 'import sys,csv; c = csv.reader(sys.stdin); [sys.stdout.write("^M".join(map(repr,r))+"\n") for r in c];' <tmp/test.csv | column -s '^M' -t
2010-02-01 14:57:25
User: pykler
Functions: c++ column python
Tags: CSV
1

Will handle pretty much all types of CSV Files.

The ^M character is typed on the command line using Ctrl-V Ctrl-M and can be replaced with any character that does not appear inside the CSV.

Tips for simpler CSV files:

* If newlines are not placed within a csv cell then you can replace `map(repr, r)` with r

perl -F',' -ane '$a += $F[3]; END { print $a }' test.csv
2009-08-11 15:08:58
Functions: perl
Tags: awk column CSV sum
1

More of the same but with more elaborate perl-fu :-)

awk -F ',' '{ x = x + $4 } END { print x }' test.csv
perl -ne 'split /,/ ; $a+= $_[3]; END {print $a."\n";}' -f ./file.csv
function headers { head -1 $* | tr ',' '\12' | pr -t -n ; }
2009-03-25 20:07:47
User: totoro
Functions: head pr tr
Tags: CSV headers
0

This little command (function) shows the CSV header fields (which are field names separated by commas) as an ordered list, clearly showing the fields and their order.