Using awk (or an alternative)

Hi,

I'm trying to echo a variable that has values separated by colons, putting each value on a new line. So as an example, a variable I might have would contain:

My name is Earl:My name is Dorothy:My name is Bernard:

And I want the output:

My name is Earl
My name is Dorothy
My name is Bernard

Also, I will be unaware of how many values the colon separated variable will contain (as it is generated by another function), so whatever is used will need to be flexible enough to accommodate any number of values. I'm not very competent in awk, so is there a way this can be done either with awk or with something else?

Also, is it possible to specify a field separator (such as a colon or comma) to the for command?

echo "name is Earl:My name is Dorothy:My name is Bernard:" | tr ':' '\n'

or

echo "name is Earl:My name is Dorothy:My name is Bernard:" | sed "s/:/\\
/g"

or

echo "name is Earl:My name is Dorothy:My name is Bernard:" | awk ' gsub( ":" , "\n" ,$0 ) '

or alternatively with 'nawk':

echo 'name is Earl:My name is Dorothy:My name is Bernard:' | nawk -v RS=: '$1=$1'

if you have Python, here's an alternative:

#!/usr/bin/python
s = "name is Earl:My name is Dorothy:My name is Bernard:"
for item in s.split(":"):
    print item
echo 'name is Earl:My name is Dorothy:My name is Bernard:' | awk -F":" -v OFS="\n" '$1=$1'

To answer your last question first: yes, set IFS; see my code below.

You don't need awk:

var="My name is Earl:My name is Dorothy:My name is Bernard:"
set -f
IFS=:
printf "%s\n" $var

A slower method would be to use tr:

printf "%s\n" "${var%:}" | tr : '\n'