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.

No comments: