jueves, 29 de octubre de 2009
martes, 20 de octubre de 2009
Get list tables from postgres database
$ psql -qAtF, -c "\dt da_schema.*"
da_schema,segments,table,jacen
da_schema,speeds,table,jacen
$
You can also use "*" in the schema name, for example:
$ psql -qAtF, -c "\dt da_schema*.*"
da_schema,segments,table,jacen
da_schema,speeds,table,jacen
da_schema_2,labels,table,jacen
da_schema_test,positions,table,jacen
$
martes, 22 de septiembre de 2009
Changing execution attributes on svn
This is what can be done to change the attributes of the file:
$ svn propset svn:executable on da_scripto.sh
$
lunes, 21 de septiembre de 2009
Rebuilding TeX's filenames database
! LaTeX Error: File `beamerthemeAnnArbor.sty' not found.
The solution was simple, rebuild the TeX's filenames database
sudo texhash
viernes, 11 de septiembre de 2009
Attach a file to a mail on shell
mail
command. Now, we can do the same thing using mutt
:
$ echo "Mail body" | mutt -s "This is a mail with attachments" -a ~/mi_file.txt -a ~/mi_picture.png anne@octop.us
$
We can also use some pre-written file to use as the email body:
$ mutt -s "Mail with body from a file" anne@octop.us < ~/mail_body.txt
$
martes, 11 de agosto de 2009
Work with Data/Time in Postgres
* How to transform 'time' from epoch to any human readable format?
SELECT to_char(to_timestamp(1239148800) at TIME ZONE 'UTC', 'YYYY/MM/DD HH24:MI:SS')
For any other format, you can see the table "Template Patterns for Date/Time Formatting" on the documentation of your version of Postgres.
* How to transform from timestamp to epoch ?
SELECT EXTRACT (EPOCH FROM timestamp '20090101T041000' at time zone 'UTC') ;
Other way to do the last transformation is set the time zone on the environment
SET TIMEZONE TO 'UTC'
SELECT EXTRACT(EPOCH FROM timestamp '20090506T041000');
miércoles, 5 de agosto de 2009
Generating lines from one line
You can do in perl with:
$ echo "1,a/b/c/d" | \
perl -F, -l0ane 'print "$F[0],$_\n" foreach(split("/",$F[1]))'
$
Or if you want to write less do:
$ echo "1,a/b/c/d" | awk -F, '{split($2,z,"/");for(i in z)print$1","z[i]}'
lunes, 27 de julio de 2009
Get IP address of a host
$ hostname -i
192.168.1.160
$
viernes, 17 de julio de 2009
UnicodeEncodeError: 'ascii' codec can't encode character XXX
Yet another text-format error in python (personal error, of course)... while I was trying to store in file a joined list (through list comprehension), one or more of the characters of one the fields that are being joined generate the following 'friendly message':
Traceback (most recent call last):
File "stations.py", line 62, in
exit(main())
File "stations.py", line 16, in main
record.append(str(element.text))
UnicodeEncodeError: 'ascii' codec can't encode character u'\xf1' in position 7: ordinal not in range(128)
repr(object)
Return a string containing a printable representation of an object.
(http://docs.python.org/library/functions.html)
Example:
>>> caca = u'\u2603'
>>> print caca
☃
>>> str(caca)
Traceback (most recent call last):
File "
UnicodeEncodeError: 'ascii' codec can't encode character u'\u2603' in position 0: ordinal not in range(128)
>>> repr(caca)
"u'\\u2603'"
solution from:
http://blog.codekills.net/archives/45-str...-yer-probably-doin-it-wrong..html
martes, 14 de julio de 2009
shp2pgsql UTF-8 enconding workaround
shp2pgsql -W "UTF-8" ... :(
Then, a nice workaround is to use | with iconv:
shp2pgsql file.shp schema.table | iconv -f LATIN1 -t UTF-8 | psql
viernes, 10 de julio de 2009
Python broken pipe
python foo.py| head
and you get a Broken Pipe Error then the next code snippet could be useful.
import sys
for line in sys.stdin:
print line.strip()
Then run your program and you will get
$ yes | python pipe.py |head
y
y
y
y
y
Traceback (most recent call last):
File "pipe.py", line 5, in ?
print line
IOError: [Errno 32] Broken pipe
To avoid this error just use signal package:
import signal
import sys
signal.signal(signal.SIGPIPE, signal.SIG_DFL)
for line in sys.stdin:
print line.strip()
Enjoy
jueves, 25 de junio de 2009
Error handling in Bash
rm ~/data.tar.bz2
if [ "$?" != 0 ]; then
echo "ERROR: Could not delete file"
exit 1
fi
$
miércoles, 24 de junio de 2009
The kill that revives
$ kill -SIGCONT pid
$
:)
martes, 23 de junio de 2009
disown ...
It happens to all of us ...
You launch a command that is taking too long, you can't logout 'cause you'll lose your session and all the running hours...
what to do??
$ ./myprocess.bash
^Z
[1]+ Stopped ./myprocess.bash
$ bg
$ disown %1
$ logout
$
Now INIT will take care of your son, and you are free to leave ...
(see "man disown", a bash built-in).
Adding en_US.utf8 locale to an Ubuntu
locale -a
C
en_US.utf8
POSIX
For the next time you log into the server the following two lines have to vi added to /etc/environment
LANGUAGE=”en_US.utf8″
LANG=”en_US.utf8″
viernes, 19 de junio de 2009
double/single quotes in bash
$ string="Hello World"
$ echo $string
$ echo '$string "string"'
$
Double Quotes (" "): Preserve the literal value of all characters within the quotes. Rules Exception are: ‘$’, ‘`’ and‘\’ (shell expansion). For example,
string="Hello World"
echo $string
echo "My first script: $string, \"string\", "string""
$
Back Quotes or Back Sticks(``):Command Substitution (idem $() )
For example,
echo `date`, $(date)
$
jueves, 18 de junio de 2009
Adding Google projection to your PostGIS
INSERT into spatial_ref_sys (srid, auth_name, auth_srid, proj4text, srtext) values ( 900913, 'spatialreference.org', 900913, '+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +wktext +no_defs', 'PROJCS["unnamed",GEOGCS["unnamed ellipse",DATUM["unknown",SPHEROID["unnamed",6378137,0]],PRIMEM["Greenwich",0],UNIT["degree",0.0174532925199433]],PROJECTION["Mercator_2SP"],PARAMETER["standard_parallel_1",0],PARAMETER["central_meridian",0],PARAMETER["false_easting",0],PARAMETER["false_northing",0],UNIT["Meter",1],EXTENSION["PROJ4","+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +wktext +no_defs"]]');
Files being accessed by a process
$ ls -l /proc/pid_number/fd
Try with the pid of your firefox process and you'll see all the sqlite files where firefox stores places, cookies, downloads, permissions, etc.. All the jar files, certificates, extensions and more.
Try:
$ echo ".dump" | sqlite3 /path_to_firefox_profile/formhistory.sqlite
:P
Request Layer with another SLD from OpenLayers
layer=Map.getLayersByName('Reports')[0];
layer.params['STYLES']='SLD';
later.redraw();
Kill'em all >:|
$ kill -9 -1
$
Kill all processes owned by the user except the shell from where it's being launched, it's a nice cleanup shortcut.
Code Highlighting
Example:
from datetime import datetime
print datetime.now()
To use it, just write your code inside of <pre> or <textarea>
with tags name="code" and class="language" where language could be:
- Javascript (js)
- Bash (bash)
- Python (python)
- Sql (sql)
- C++ (cpp)
- Java (java)
- Php (php)
If you are looking for a console style, use class="console", and <blink>_</blink>
for your cursor :)
miércoles, 17 de junio de 2009
Expanding strings in shell
$ a="my string"
$
Simple quote doesn't expand the variable:
$ echo 'This is $a'
This is $a
$
Double quote does expand the variable:
$ echo "This is $a"
This is my string
$
list processes with open sockets
lsof -i -r30 -n +c0
-r30: list every 30 secs
-n: do not resolve names
-i: sockets
+c0: do NOT truncate chars
Days between 2 unix epoch
#!/bin/bash
# Calculate "days diff" (unix epoch)
function daysdiff {
begin=${1:-0}
end=${2:-0}
let Sec=$end-$begin
if ((Sec < 0)); then
echo $((Sec/-86400))
else
echo $((Sec/86400));
fi
return 0
}
Mount a directory through ssh
# remote host (username@host:/dirnamehost) to
# local host (dirnamelocalhost)
sshfs -o ro username@host:/dirnamehost dirnamelocalhost
# remember that the user must belong to the fuse group
# before doing the sshfs
usermod -a -G fuse username
Working with date in shell
Or in UTC
$ date -d @1245257034 -u
Wed Jun 17 16:43:54 UTC 2009
To know what is the current unix time in your machine
$ date +%s
1245257175
Getting last part of a string in shell script
string="my string.hello.world"
# to get the last part considering the field separator = "." execute:
echo ${string##*.}