I want to swap hyphen (-) and underscore (_) in a string. In fact only one will be present. So if it is hyphen I want to make it underscore and if it is underscore I want to make it hyphen.
I can write a long code like below. Is there any easy way?
#!/bin/ksh
typeset -i exists
str="ag-sd"
new_str=$str
exists=`print $new_str | grep "\-" | wc -l`
if [[ $exists -eq 1 ]]
then
str=`print $new_str | sed 's/\-/\_/g'`
fi
exists=`print $new_str | grep "\_" | wc -l`
if [[ $exists -eq 1 ]]
then
str=`print $new_str | sed 's/\_/\-/g'`
fi
print "Old string $new_str"
print "New string $str"