Page 1 of 1

Replacing the last occurrence of char on every line of file

Posted: 07 Jun 2012 06:46
by yigitozen
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:

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

input file:
-some-text-here
some-morehere
blah-blah-
-end

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

Code: Select all

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


Any help will be appreciated, thanks in advance..

Re: Replacing the last occurrence of char on every line of f

Posted: 07 Jun 2012 07:37
by Squashman
Then why not just use sed to do this as well.

Re: Replacing the last occurrence of char on every line of f

Posted: 07 Jun 2012 08:35
by yigitozen
hi squashman, because i need a portable and native solution which can run on every windows system without an installation. i need to solve it programmatically, not by installing sed for windows, if possible..
any advice?

thanks.

Re: Replacing the last occurrence of char on every line of f

Posted: 07 Jun 2012 09:13
by foxidrive
Will your files have any blank lines, or % ^ ! characters in them?
That is the first hurdle.

Is performance going to be an issue - are the files large?

Those things can make plain batch an issue.

Re: Replacing the last occurrence of char on every line of f

Posted: 07 Jun 2012 11:18
by yigitozen
hi foxidrive,

performance is not of a big concern.
my aim is perform such a conversion:

<input.txt>
/data/kibris/AS/lib/Administration.jar
/data/kibris/AS/lib/Extensions.jar
/data/kibris/AS/lib/Batch.jar

<output.txt>
/data/kibris/AS/lib/" archives="Administration.jar
/data/kibris/AS/lib/" archives="Extensions.jar
/data/kibris/AS/lib/" archives="Batch.jar

in other words, i like to parse a fullpath into path and file name and place a string in between. (replacing the last occorrence of / with /" archives=" does the same here.)

about your question; i have no blank lines but have "/" to to be replaced.

kind regards.

Re: Replacing the last occurrence of char on every line of f

Posted: 07 Jun 2012 11:42
by foxidrive
You have no idea how different the two requirements you stated are in terms of the required batch code!

Thank you for giving us the real data - this should do what you need.

EDIT:

Code: Select all

@echo off
setlocal enabledelayedexpansion
for /f "delims=" %%a in (filein.txt) do (
set "p=%%~pa"
set "p=!p:\=/!"
>>"fileout.txt" echo !p!" archives="%%~nxa
)



If you can handle backslashes in the path then this will work:

Code: Select all

@echo off
for /f "delims=" %%a in (filein.txt) do (
>>"fileout.txt" echo %%~pa" archives="%%~nxa
)

Re: Replacing the last occurrence of char on every line of f

Posted: 07 Jun 2012 14:05
by yigitozen
thank you very much foxidrive, your first code suggested does just what i need.

Code: Select all

@echo off
setlocal enabledelayedexpansion
for /f "delims=" %%a in (filein.txt) do (
set "p=%%~pa"
set "p=!p:\=/!"
>>"fileout.txt" echo !p!" archives="%%~nxa
)