Monday, March 17, 2014

Shell script - function to split a string based on a delimiter

#!/bin/ksh

#function to split a string based on a delimiter
function split {

        string=$1
        delimiter=$2

echo "split string '"$string"' with delimiter '"$delimiter"'"
        for word in $(echo $string | tr "$delimiter" "\n")
        do
                echo $word
        done
}

#call the split function with arguments string and delimiter
split "transient;private;protected;public;synchronized;native" ";"

split "float|short|char|double" "|"

No comments:

Post a Comment