Thursday, June 29, 2006

Perl : Formatting code

Following one liner attempts to format your codes. Works on programs of perl, c, php etc where curly braces - '{ and }', are used for blocks, loops, conditions etc.

perl -pe '$tabs = substr($tabs,1) if (/^\s*}\s*$/);s/^\s*/$tabs/;$tabs.="\t" if(/{[^}]*$/);' -i.bk
filename.c

Files are backed up with .bk extension.

Wildcards are allowed so *.c can be used in place of filename.c.

Monday, June 26, 2006

Perl : Editing files on the command line

With -i switch of perl, files can be edited on command line. You can optionally take a back-up of the old files by providing an extension.

perl -pe 's/\r\n/\n/;' -i file1

Replaces \r\n (dos newlines) to \n from file1.

perl -pe 's/\<\?php/<?/;' -i.bk *.php

Replaces <?php with <? in php files and creates back-up files with .bk extension.

Thursday, June 22, 2006

vi/vim : Changing the tabsize

Default tabstop is 8. To set the tabsize to 4 spaces, just issue this command

:set ts=4

ts stands for tabstop.

To make this a default setting, write this line without the colon ':' in .exrc (.vimrc if you use vim) file.

Wednesday, June 21, 2006

Vim : Dictionary completion facility

Dictionary completion facility is useful when you don't remember the spelling of a word. To make use of this, we must first set the dictionary option so that vim knows where the dictionary is. Following command does that.

:set dictionary-=/usr/share/dict/words dictionary+=/usr/share/dict/words

Now, start typing a word in insert mode.

astro<ctrl+x><ctrl+k>

This shows the first match in the dictionary. You can loop through the matches by using <ctrl+n> (for next) and <ctrl+p> (for previous).

Notes

To understand why the -= and +=, use this command
:help :set+=

Tuesday, June 20, 2006

Regular Expressions : Dealing with special characters like asterisk

This post is for beginners of perl/regular expressions who find it hard to deal with special characters like asterisks if they appear in patterns. For instance, here is a pattern that contains an asterisk
4*3
If you want to replace or remove the asterisk, you will have to escape it with a back-slash ('\') in a regular expression. Following is a sample code.

$x = "4*3";
print "$x\n";

## following line removes the special character asterisk from the scalar $x
## note that the asterisk is 'escaped' by using a back-slash
$x =~ s/\*//;

print "$x\n";


Output

4*3

43

Use of a back-slash ('\') for escaping special characters is common where regular expressions are used. It's also used for escaping spaces in directory-names on shell prompt.

Some examples

character : escaping it
\ : \\
* : \*
+ : \+
/ : \/

Perl : Removing blank lines from a file

You can do intricate things even on command line. The following command prints filename on the command standard output after removing blank lines

cat filename | perl -ne 's/^\s*\n$//; print;'

Notes
  • Replace filename with the path of the file from which you want to remove blank lines.
  • You can optionally redirect the output by using '>' operator to create a new file.

Monday, June 12, 2006

Linux : Installing Packages .... common procedure

This post is for novices who are scared of building/installing new packages on linux :-) Here are the steps.

  1. Download the package. It's normally a .tar.gz file. It could be in some other format like bz2 as well.
  2. Extract it with tar -zxvf package.tar.gz (for .tar.gz files)
  3. Change to the newly created directory with this extraction
  4. Read README files (good practice)
  5. Run on prompt
    ./confgure
  6. then
    make
  7. Become root if you have not already. Default location of installation of almost all new packages is /usr/local so you need write permission to create any new files there.
  8. issue
    make install
You can install packages in other locations by using --prefix=/path/where/you/want/to/install option to ./configure. Learn more about options by using --help (normally) switch of ./configure scripts. All configure scripts may not present you with nice help.

Friday, June 02, 2006

Linux : Synchronizing directories with rdist

rdist is a linux command for keeping identical copies of directories/files over two hosts. File permissions and modification time are preserved if possible. It can use various protocols like ssh, rsh (default). You can have all the synchronization details stored in a file and pass the filename as an argument with option -f to the command as illustrated below.

rdist -f rdist_file

To specify the protocol, use -P option

rdist -P /usr/bin/ssh -f rdist_file

rdist_file is of the following format.

## comments start with '#' sign
/path/on/master/server    ->   remote_server_name  install  /path/on/slave/server ;

-o option is used for specifying an action to be taken on those files on the slave (remote server) which are missing from the master. For truly synchromising two directlries, you can use -oremove to remove the files. The directive then takes the following form.

/path/on/master/server    ->   remote_server_name  install -oremove /path/on/slave/server ;

For more about actions and about the command, see man pages.