Replacing all patterns except last one

Replacing all patterns except last one

Post by karthik.prabaha.. » Wed, 12 Oct 2005 04:26:45



Hi,

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

any help would be appreciated.

Thanks in advance,

Karthik

 
 
 

Replacing all patterns except last one

Post by Jan Schamper » Wed, 12 Oct 2005 04:32:09


On 10 Oct 2005 12:26:45 -0700


> 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.

sed -e 's/_/-/g;s/-\([^-]*\)$/_\1/g' comes to mind. But I'm sure there
are better solutions.

Regards,
J.

--
"Be liberal in what you accept, and conservative in what you send."
- J. B. Postel, master of the net.

 
 
 

Replacing all patterns except last one

Post by Stephane CHAZELA » Wed, 12 Oct 2005 04:34:19



Quote:> 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

[...]

sed -e :1 -e '/_.*_/{s/_/-/;b1' -e '}' < in > out

Or:

sed -e 's/.*_/&\
/;s/_/-/g;s/-\n/_/' < in > out

--
Stphane

 
 
 

Replacing all patterns except last one

Post by Ed Morto » Wed, 12 Oct 2005 04:40:37



> Hi,

> 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

> any help would be appreciated.

> Thanks in advance,

> Karthik

awk '{c=split($0,t,"_");for(i=2;i<c;i++)sub("_","-")}1' file

Regards,

        Ed.

 
 
 

Replacing all patterns except last one

Post by Chris F.A. Johnso » Wed, 12 Oct 2005 05:45:54



> 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>

 
 
 

Replacing all patterns except last one

Post by William Par » Wed, 12 Oct 2005 07:02:17



> Hi,

> 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

a='123_123_123_3.jt'
right=${a##*_}
left=${a%_${right}}
echo ${left//_/-}_${right}

--

ThinFlash: Linux thin-client on USB key (flash) drive
           http://home.eol.ca/~parkw/thinflash.html
BashDiff: Super Bash shell
          http://freshmeat.net/projects/bashdiff/

 
 
 

Replacing all patterns except last one

Post by John W. Krah » Wed, 12 Oct 2005 08:58:13



> 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

> any help would be appreciated.

$ echo "123_123_123_3.jt
abc_abc_1.jt" | perl -pe'$_ = join q/-/, split /_/, $_, tr/_//'
123-123-123_3.jt
abc-abc_1.jt

John
--
use Perl;
program
fulfillment

 
 
 

Replacing all patterns except last one

Post by John W. Krah » Wed, 12 Oct 2005 09:06:10




>>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

>>any help would be appreciated.

> $ echo "123_123_123_3.jt
> abc_abc_1.jt" | perl -pe'$_ = join q/-/, split /_/, $_, tr/_//'
> 123-123-123_3.jt
> abc-abc_1.jt

Or:

$ echo "123_123_123_3.jt
abc_abc_1.jt" | perl -pe's/_(?=.*?_)/-/g'
123-123-123_3.jt
abc-abc_1.jt

John
--
use Perl;
program
fulfillment