Sunday, November 18, 2012

Grep tips

To get all lines not beginning with a tab, do
grep -vP "^\t" abc.txt
-P turns on Perl-style regex (otherwise tab cannot be specified as \t). "^\t" matches lines beginning with a tab. -v inverts the selection.

For most cases though, using -E (extended regex) instead of -P works, and is better supported.

Friday, November 16, 2012

Using `less'

View text files using less, including very large files. It also opens gzip-ed files. Use less -S to truncate long lines (instead of wrapping around).

Wednesday, August 22, 2012

Information about your Linux system

Here are some commands that give information about your linux system.

To get  the number of CPU's/cores in the system, use
less /proc/cpuinfo

To see if your processor supports 64-bit OS, you can Google the processor name (got using /proc/cpuinfo). To see if your OS is 64-bit, use
uname -a
If it says x_86_64 somewhere in the output, then it is a 64-bit OS, else it is probably a 32-bit OS.

To see where (in which directories) a package is installed in Ubuntu, use
dpkg -L <packagename>

Friday, August 3, 2012

Displaying Unicode text in the terminal / console

If gnome-terminal has problems displaying Unicode (UTF-8) characters properly, try using konsole (from KDE). It worked for me.

Thursday, July 5, 2012

LaTeX Beamer Tips

To include videos, do  
\usepackage{multimedia}
\movie[externalviewer]{\includegraphics{poster.jpg}}{film.mp4}
Without externalviewer, the PDF viewer attempts to play the video within itself (this usually does not work).

To cover/uncover parts of slide, do
\uncover<1-4>{Show this throughout.}
\uncover<2-4>{Show this after the first transition.}
\only<3-4>{Show this after the second transition.}
\only<4>{Show this after the third transition.}
Roughly, \uncover puts the text on the slide before the transition but hides it. \only doesn't put the text on the slide until the specified transition. You can replace the text with image (\includegraphics), environment, etc.

To have text on the left and an explanatory image on the right, (within the frame) do
\begin{columns}
    \begin{column}{0.5\textwidth}
        \begin{itemize}
            \item Blah
            \item Blah Blah
        \end{itemize}
    \end{column}
    \begin{column}{0.5\textwidth}
        \centering \includegraphics{Blah.jpg}
    \end{column}
\end{columns}

To display slide numbers in the footer, (in the preamble) do
\useoutertheme{infolines}
\setbeamertemplate{headline}{}
\setbeamertemplate{footline}{\insertframenumber/\inserttotalframenumber}
Move \insert... to headline to display in the header.

Friday, March 23, 2012

Debugging and Profiling in Python

To debug hello.py, do
$ python -m pdb hello.py

You will immediately get the debug prompt, with the execution stopped before the first line of the program. You can now use:

l - to display source code around the line to be executed next
n - to execute the next line of code
s - to step into the next line of code
b <line no.> - to set breakpoints
b - to list breakpoints
clear <breakpt no.> - to clear breakpoints
c - to continue execution
tbreak <line no.> followed by c - to execute till a particular line
p <var> - to print contents of variables,
vars(obj) - to print contents of objects (same as obj.__dict__)
dir(obj) - to list attributes/methods of objects
help - to get more help.


 To profile hello.py, do
 $ python -m cProfile hello.py

The program will be run and you will get a profiler report on the console.

Monday, March 12, 2012

Lagrangian duality

Given an minimization problem

min f(x)
s.t. g_i(x)  = 0,     i=1..m
      h_j(x) <=  0,  j=1..p,

the Lagrangian function is

L = f(x) + \sum_i \alpha_i g_i(x) + \sum_j \beta_j h_j(x)

where \alpha_i is unconstrained, and \beta_j >= 0.

See more on Wikipedia.

Monday, February 20, 2012

Literal strings in Python

Some of the many ways one can construct strings in Python:

s = "qwe\nasd\nzxc"  #gives 'qwe\nasd\nzxc'
s = 'qwe\nasd\nzxc'  #gives 'qwe\nasd\nzxc'
s = 'qwe\
\nasd\
\nzxc'  #gives 'qwe\nasd\nzxc'
s = '''qwe
asd
zxc'''  #gives 'qwe\nasd\nzxc'
s = '''qwe\\n
asd
zxc'''  #gives 'qwe\\n\nasd\nzxc'
s = r'qwe\n\
asd\
zxc'  #gives 'qwe\\n\\\nasd\\\nzxc'; => \n in the string is always accompanied by a '\'.
s = u'qwe\u0020asd'  #gives a Unicode string 'qwe asd' (the Unicode code for space is 20)
s = ur'qwe\u0020asd'  #gives a Unicode string 'qwe asd'
s = ur'qwe\\u0020asd'  #gives a Unicode string 'qwe\\\\u0020asd' (the desired format for use in regular expressions.
s = u'qwe\u0020asd'.encode('utf-8')  #gives an encoding of the Unicode string in the specified encoding ('utf-8')
s1 = unicode(s, 'utf-8')  #gives a Unicode string after decoding the content of s according to specified encoding

Wednesday, January 25, 2012

Scheduling a regular job in Linux

To execute the shell script myjob.sh every Friday and Saturday of July at 0,20 and 40 minutes past 10pm, do
crontab -e 
In the editor that opens up, enter
0,20,40 22-23 * 7 fri-sat /absolute/path/to/myjob.sh 
and save. You are done!
The format is for specifying jobs is
Min Hour DoM MoY DoW Cmd.
See Ian's article for more.

Monday, January 23, 2012

Reading ISO files in Linux

Given the file
/path/to/iso/abc.iso
do
mkdir /path/to/mount/dir/abcdir
mount -o loop /path/to/iso/abc.iso /path/to/mount/dir/abcdir
cd /path/to/mount/dir/abcdir
You can now start using the contents of the ISO file.