Wednesday, August 1, 2012

String manipulation in Bash shell

I am really fed up by people who use unreadable sed and awk commands to perform basic string manipulations, like a find and replace on a string.

There are very nice expression you can use:

string length: ${#string}

expression matching: expr match "$string" '$substring'

index: expr index $string $substring

substring: ${string:position:length}

substring delete: ${string#substring}

find and replace: ${string//substring/replacement}

Please use awk and sed only if REALLY needed.

The only good code is readable code.

No comments: