Sunday, August 8, 2010

Python - remove leading zeros

To remove one or more leading zeros from the string eg. convert 0040 into 40:

import re
oldstring = '0040'
newstring = re.sub(r"\b0{2}","",oldstring)

To remove different number of zeros change the number in {} in the code.

EDIT:
Better way:


import re
oldstring = '0040'
newstring = re.sub("^0+","",oldstring)

Friday, August 6, 2010

Checking ntfs partition on linux

Use ntfsfix application from ntfs-3g package eg.:

$ sudo ntfsfix /dev/sdc1

Tuesday, August 3, 2010

Python - get date and time

To get current date and time:

from time import strftime

strftime("%Y/%m/%d %H:%M:%S")

'2010/07/28 21:50:30'

Format drive to ntfs in Ubuntu


If you want to format eg. external usb drive to NTFS in Ubuntu you can use gparted. You will also need ntfsprogs package:


sudo apt-get install gparted ntfsprogs

Python - replace character in a string

To replace a character("a") in a string with the other one ("b"):

s = s.replace('a', 'b')

Python - get index in the list

To get the index of the element in the list:

list.index(element)

Linux - finding last reboot/shutdown time

To find last reboot time:

last reboot | less

To find last shutdown time:

last -x | grep shutdown | less

Python and config file parser

Very useful Python module for parsing config files:
http://docs.python.org/library/configparser.html

ssh access using public and private keys


  1. Generate a key pair on your machine:

    $ ssh-keygen

    Enter the name of the files with the keys and passphrase (not necessary).

  2. Add your public key to the list of authorized keys on the remote machine:


    cat ~/.ssh/id_rsa.pub | ssh user@machine "cat >> ~/.ssh/authorized_keys"

  3. Set permissions on the remote machine:


    chmod 700 ~/.ssh

    chmod 600 ~/.ssh/authorized_keys


Tracking changes between file revisions in CVS


  • To check file history (and revisions):

    $ cvs log filename


  • To compare two revisions (REV1 and REV2) of the file:

    $ cvs diff -u -r REV1 -r REV2 filename


Passing arguments to awk from shell

To pass argument SHELLARGUMENT to awk from a shell do:

$ awk -v x="$SHELLARGUMENT" '{print x}'

eg.:

#!/bin/sh

SHELLARGUMENT=test

awk -v x="$SHELLARGUMENT" '{print x}'


Echo without newline

To print a text without a newline in shell script:


$ echo -n "Text"

diff print only unmatched lines

To print only unmatched lines from file2:


$ diff file1 file2 | grep ">" | awk -F '>' '{print $2}'

Loop over a files in directory in shell



#!/bin/sh

for f in `ls`

do

echo “Processing $f file …”

#Here you can put your commands

done


You can loop over files with specific names changing ls to ie. ls *.txt etc.

Awk - powerful tool

Awk is a scripting language that can be used in shell. Please find here some examples:


  1. We've got a file that contains lines and columns (separated with spaces). We want to print only the contents of 3rd column.

    cat file.txt | awk '{print $3}'

  2. Print last column:

    cat file.txt | awk '{print $NF}'

    $NF means for awk the last argument (here: column)

  3. Print the first expression before '/' sign:

    cat file.txt | awk -F '/' '{print $1}'

    Using -F option you can set different separators for awk arguments.

  4. Another example. Printing last column + some text before it + some text after:

    cat file.txt | awk '{print "cp /home/"$1" "$1}'

    If you have filenames from /home directory in the file.txt this will prepare copying those file to current directory.

Grep: get patterns from a file

Having a list of patterns in patternsfile and needed to search for those patterns in datafile use:


$ grep -f patternsfile datafile

Double pipe

Nice feature of double pipe "||":


$ command1 || command2


This means that command2 will be run if the command1 fails.

Clickable links in Latex table of contents


  1. Use hyperref package:

    \usepackage{hyperref}

  2. Set link colors:

    \hypersetup{

    colorlinks,

    citecolor=black,

    filecolor=black,

    linkcolor=black,

    urlcolor=black

    }

  3. Link only the page numbers and not the entire table of contents:

    \hypersetup{linktocpage}

Change color in OpenOffice formula

color <colour>{expression}

File extension in shell

FILENAME="file.txt"


  1. To print file's extension:

    $ echo ${FILENAME#*.}

    txt

  2. To print file name without extension:

    $ echo ${FILENAME%.*}

    file


Tracking memory leaks

Very powerful tool for checking memory leaks and speeding up the applications:


  1. Valgrind (http://valgrind.org)

  2. Valkyrie - front-end for Valgrind

  3. Callgrind (valgrind -tool=callgrind): CPU-time profiling tool

  4. kcachegrind - front-end for callgrind


User management in terminal


  1. Adding new user:

    $useradd user

  2. Setting password:

    $passwd user

  3. Assigning user to a group:

    $usermod -a -G group user

Update-alternatives

If you have installed two versions of the application and you need to switch between them - there's a nice tool: update-alternatives. How to use it? Here's an example: switching between gcc-4.3 (and simultaneously g++-4.3) and gcc-4.4 (g++-4.4):


  1. $ sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.3 60 --slave /usr/bin/g++ g++ /usr/bin/g++-4.3

  2. $ sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.4 40 --slave /usr/bin/g++ g++ /usr/bin/g++-4.4

  3. $ sudo update-alternatives --config gcc