> I have an input file as:
> 123_123_123_3.jt
> abc_abc_1.jt
> I need to replace all the underscores ("_") with hypen ("-") except the
> last occurence.
> The expected outfile file is:
> 123-123-123_3.jt
> abc-abc_1.jt
If the file is big, the sed or awk solutions already posted will be
faster. If it's small, or if you need to perform the operation on a
single string, this POSIX shell function will be faster:
_ab1()
{
_AB1=$1
while :
do
case $_AB1 in
*_*_*)
_AB1L=${_AB1%%_*}
_AB1R=${_AB1#*_}
_AB1=$_AB1L-$_AB1R
;;
*) break ;;
esac
done
Quote:}
To fix the entire file:
while IFS= read -r line
do
_ab1 "$line"
printf "%s\n" "$_AB1"
done < "$1"
--
Chris F.A. Johnson <http://cfaj.freeshell.org>
==================================================================
Shell Scripting Recipes: A Problem-Solution Approach, 2005, Apress
<http://www.torfree.net/~chris/books/cfaj/ssr.html>