Skip to content

Instantly share code, notes, and snippets.

@managai
managai / gist:5021277
Last active December 14, 2015 03:29
Python: read file line by line into list
import os
with open(fname) as f:
content = f.readlines()
f.close()
# http://stackoverflow.com/questions/3277503/python-read-file-line-by-line-into-array
@managai
managai / gist:5015313
Last active December 14, 2015 02:39
list of all files (and directories) in a given directory
import os
for dirname, dirnames, filenames in os.walk('.'):
# print path to all subdirectories first.
for subdirname in dirnames:
print os.path.join(dirname, subdirname)
# print path to all filenames.
for filename in filenames:
print os.path.join(dirname, filename)
@managai
managai / gist:1367517
Created November 15, 2011 16:34
(python) extract substring between two symbols
myString="Hello there !bob@"
mySubString=myString[myString.find("!")+1:myString.find("@")]
print mySubString
## extract text between ! and @
@managai
managai / gist:1367478
Created November 15, 2011 16:19
(python) read file line by line
f = open('myfile.txt')
for line in iter(f):
print line,
f.close()
@managai
managai / gist:1347822
Created November 8, 2011 14:12
(bash) list directories names
#!/bin/bash
ls -d */ | xargs -l basename
@managai
managai / gist:1344835
Created November 7, 2011 12:39
(SQL) copy data between tables
insert
into table2
select *
from table1
#########
insert
into table2
( col1
@managai
managai / gist:1292409
Created October 17, 2011 11:12
(bash) Measure Elapsed Time
#!/bin/bash
#
# Elapsed time. Usage:
#
# t=$(timer)
# ... # do something
# printf 'Elapsed time: %s\n' $(timer $t)
# ===> Elapsed time: 0:01:12
#
#
@managai
managai / gist:1292405
Created October 17, 2011 11:09
(bash) calculate fibonacci number
#!/bin/bash
fib () {
if [ $1 -le 1 ]; then
echo $1
else
echo $[$[`fib $[$1 - 1]` + `fib $[$1 - 2]`]]
fi
}
@managai
managai / gist:1255802
Created October 1, 2011 09:31
(perl) trim function to strip whitespace from a string
# Perl trim function to remove whitespace from the start and end of the string
sub trim($)
{
my $string = shift;
$string =~ s/^\s+//;
$string =~ s/\s+$//;
return $string;
}
# Left trim function to remove leading whitespace
sub ltrim($)