I have a variable with data in this format
field1;field2;field3
I wanted to split the variable like this
field1
field2
field3
this statement was working fine echo $key_val | awk '{gsub(";" , "\n"))'
but sometimes we get the data in the variable in this format key_val="field1"
if this value is sent through the above statement it does'nt return any value
I want to write a short shell script which will return the data like
field1
field2
field3
when the input was like field1;field2;field3
and just field1 when the input was just like that
I think this way will work
count=0
count=`echo $key_val | sed -cd ";" |wc -c`
if [[ $ count -eq 0 ]]
then
echo $key_val
else
echo $key_val | awk '{gsub(";" , "\n"))'
fi
However it seems too long and cumbersome way of implementing it,do you have any suggestions how i can make the code more compact or more efficient
I am working in the korn shell
Thanks
Mervin