Replacing the last occurrence of char on every line of file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
yigitozen
Posts: 6
Joined: 07 Jun 2012 06:41

Replacing the last occurrence of char on every line of file

#1 Post by yigitozen » 07 Jun 2012 06:46

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

Squashman
Expert
Posts: 3741
Joined: 23 Dec 2011 13:59

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

#2 Post by Squashman » 07 Jun 2012 07:37

Then why not just use sed to do this as well.

yigitozen
Posts: 6
Joined: 07 Jun 2012 06:41

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

#3 Post by yigitozen » 07 Jun 2012 08:35

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.

foxidrive
Expert
Posts: 6033
Joined: 10 Feb 2012 02:20

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

#4 Post by foxidrive » 07 Jun 2012 09:13

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.

yigitozen
Posts: 6
Joined: 07 Jun 2012 06:41

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

#5 Post by yigitozen » 07 Jun 2012 11:18

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.

foxidrive
Expert
Posts: 6033
Joined: 10 Feb 2012 02:20

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

#6 Post by foxidrive » 07 Jun 2012 11:42

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
)

yigitozen
Posts: 6
Joined: 07 Jun 2012 06:41

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

#7 Post by yigitozen » 07 Jun 2012 14:05

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
)

Post Reply