Solved replacing the last occurrence of char on every line of file

June 7, 2012 at 05:38:20
Specs: Windows 7, 64-bit
Hi, i am looking for a windows-batch solution to the problem of, replacing the last occurrence of the given character on each lines of file. the input and the output should be as below:

replace the last "-" on each line with " AAA "

input file:

output file:
-some-text AAA here
some AAA morehere
blah-blah AAA
AAA end

Actually i have a unix script to perform the task with sed, wit whom i replace every last / with :/" archives="

more gecici_1 | sed 's/\(.*\)\//\1\" archives="/' > gecici_2

Any help will be appreciated, thanks in advance..

See More: replacing the last occurrence of char on every line of file

Report •

June 12, 2012 at 19:24:52
✔ Best Answer
here's a quick runoff that might work. Some additional tinkering will allow more
than one-byte target, but it's late, I need a beer, and you've probably already gotten
a better solution anyhows.

@echo off & setlocal enabledelayedexpansion
set x=%1
REM length of target
set z=1
REM target
set aa=-
REM substitute-last-target string
(set sub= AAA )

for /L %%a in (0,1,1000) do (
set c=%%a
call :s
if "!b!" equ "" goto :show
if "!b!" equ "%aa%" (
set h=!h!!a!!j!
set a=%aa%
set j=
) else (
set j=!j!!b!
set h=%h%%sub%%j%
echo final:%h%
goto :eof

set b=!x:~%c%,%z%!
goto :eof

Report •
Related Solutions

Ask Question