obda.net

Export Postgres Table to CSV

Add a comment

cheat-sheet articles are about code snippets that I need every once in a while, and which I constantly forget about.

PostgreSQL’s psql command line client features a \copy command that allows dumping a table to a CSV file:

\copy table_name to 'filename.csv' delimiter ',' csv header

The header argument at the end will add a header line with the column names to the CSV file. Use ; as delimiter if the CSV file shall be compatible with Microsoft’s Excel.

Rotate a Video with ffmpeg

Add a comment

cheat-sheet articles are about code snippets that I need every once in a while, and which I constantly forget about.

The transpose video filter of ffmpeg allows rotating a video by 90 degrees:

ffmpeg -i in.mp4 -vf 'transpose=1' out.mp4

Specify a value from 0–3 as transpose’s argument:

Value Description
0 Rotate by 90 degrees counterclockwise and flip vertically (default)
1 Rotate by 90 degrees clockwise
2 Rotate by 90 degrees counterclockwise
3 Rotate by 90 degrees clockwise and flip vertically

Automatically Run Commands with inotifywait

Add a comment

cheat-sheet articles are about code snippets that I need every once in a while, and which I constantly forget about.

You can use inotifywait from inotify-tools to automatically run commands, for example whenever a file is written to.

The following shell loop runs xelatex whenever ~/input.tex changes:

inotifywait -m -e close_write ~/input.tex | while read line
do
    xelatex -interaction nonstopmode ~/input.tex
done

It is also possible to watch more than one file. Additionally, if using the -r switch, any specified directory will be watched recursively:

inotifywait -m -e close_write -r locale/ | while read line
do
    python manage.py compilemessages
done

Update: December 19, 2015

The previous version of this article did not make use of inotifywait’s -m option:

while inotifywait -e close_write -r locale/
do
    python manage.py compilemessages
done

This way, inotify’s watches are re-setup after each single run of the specified command. If watching large directory trees, this is a pretty expensive operation.

In contrast, the -m option activates inotifywait’s “monitor” mode—the necessary watches are set up only once, and inotifywait runs indefinitely.