Igor Moiseev Applied mathematician, Web Developer

Find and substitute the string in all files with `sed` command on GNU/Linux

Small and powerful command to find and substitute the old_phrase with the new_phrase in all files and directories recursively with sed command on GNU/Linux

find . -type f -print0 | xargs -0 sed -i 's/old_phrase/new_phrase/g'

Attention! The previous command finds files also in the hidden folders and if you're working with Subversion or GIT you'd like to skip them. The following keys -not -path '*/\.*' makes the trick

find . -not -path '*/\.*' -type f -print0 | xargs -0 sed -i 's/old_phrase/new_phrase/g'

Oracle fixed length CHAR field in Django

When you're developing the Django app based on the legacy Oracle database you'll find this type of model

from django.db import models

class Product(models.Model):
    type_product = models.CharField(max_length=3)
    code_product = models.CharField(max_length=15)
    price = models.DecimalField(max_digits=8, decimal_places=2)

    class Meta:
        unique_together = (('type_product', 'code_product'),)

where the primary key is made by the tuple ('type_product', 'code_product'). The field code_product usually is filled by some characters and the rest is padded with spaces to respect the Oracle's CHAR(15) type. You may wish to trim those spaces in your REST web-service, but still wish to filter lists and to join on foreign keys.

Here is my hack of this problem, the CharFieldPadding class register the new character field in Django that call handy ljust function to pad the string with spaces and respect the max_length parameter.

from django.db import models

class CharFieldPadding(models.CharField):
    def __init__(self, max_length, *args, **kwargs):
        kwargs['max_length'] = max_length

        super(CharFieldPadding, self).__init__(*args, **kwargs)

    def get_prep_value(self, value):
        return value.ljust(self.max_length, ' ')


class Product(models.Model):
    type_product = models.CharField(max_length=3)
    code_product = CharFieldPadding(max_length=15)
    price = models.DecimalField(max_digits=8, decimal_places=2)

    class Meta:
        unique_together = (('type_product', 'code_product'),)

Everything works smoothly in Django Rest Framework and you have nice looking URLs.

Happy coding!

Show git branch in the bash command prompt

When you install git on your computer, you may find new variables available in the environment, it is $(__git_ps1). This variable contains the branch name of the current repository. The only thing you need to edit ~/.bashrc and add $(__git_ps1) to the PS1 definition in this way

if [ "$color_prompt" = yes ]; then
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]$(__git_ps1)\$ '
else
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w$(__git_ps1)\$ '
fi

N.B. the $(__git_ps1) is available by default for Ubuntu >= 14.04, you may check if it works by just going to any git repository and run echo $(__git_ps1)

~/Work/moiseevigor.github.io $ echo $(__git_ps1);
(master)

If you see an empty string, so just source it from /etc/bash_completion.d/git, and in this case the ~/.bashrc will look like

source /etc/bash_completion.d/git

if [ "$color_prompt" = yes ]; then
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]$(__git_ps1)\$ '
else
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w$(__git_ps1)\$ '
fi

This is it, now you have a gorgeous prompt

:~/moiseevigor.github.io (master)$ 

Have a nice branching!

Remove old kernels in Ubuntu

To remove old linux kernels and leave the current one

dpkg -l 'linux-*' | sed '/^ii/!d;/'"$(uname -r | sed "s/\(.*\)-\([^0-9]\+\)/\1/")"'/d;s/^[^ ]* [^ ]* \([^ ]*\).*/\1/;/[0-9]/!d' | xargs sudo apt-get -y purge

Be aware, the command uses uname -r to get the current kernel.

Clone repository from Subversion to Git

Here we discus how to migrate the Subversion repository into a new Git repository. We'll do it with a handy git svn utility. We will refer Gitlab/Github as the destination Git server.

Create a new project on Gitlab: http://\<gitlab-address\>/projects/new and clone it into your local machine

git svn clone http://<svn-address>/<projectname>
cd <projectname>
git remote add origin git@gitlab.newentity.it:newentity-sas/<projectname>.git
git push -u origin master

Now backup your SVN project folder

cp -raux <projectname> <projectname>.`date +'%Y%m%d'`

And remove .svn folders

cd <projectname>
rm -rf `find . -type d -name .svn`

Init GIT

git init

Config GIT

git config --global user.name "Full Name"
git config --global user.email "Email@example.com"

Check the remote url

git config remote.origin.url

Change the remote origin

git config remote.origin.url git@<gitlab-url>:<username>/<projectname>.git

Create a .gitignore file

echo "<projectname>.sublime*" > .gitignore

Add files

git add .

Commit il progetto

git commit -m "init <projectname>"
[master 820fe97] init <projectname>

Pull and merge from remote

git pull

Push into remote

git push -u origin master