View unanswered posts | View active topics It is currently 19 Dec 2014 10:27



Post new topic Reply to topic  [ 7 posts ] 
Replacing the last occurrence of char on every line of file 
Author Message

Joined: 07 Jun 2012 06:41
Posts: 6
Post Replacing the last occurrence of char on every line of file
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:
more file1 | sed 's/\(.*\)\//\1\" archives="/' > file2


Any help will be appreciated, thanks in advance..


07 Jun 2012 06:46
Profile

Joined: 23 Dec 2011 13:59
Posts: 2124
Post Re: Replacing the last occurrence of char on every line of f
Then why not just use sed to do this as well.


07 Jun 2012 07:37
Profile

Joined: 07 Jun 2012 06:41
Posts: 6
Post Re: Replacing the last occurrence of char on every line of f
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.


07 Jun 2012 08:35
Profile

Joined: 10 Feb 2012 02:20
Posts: 4501
Post Re: Replacing the last occurrence of char on every line of f
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.


07 Jun 2012 09:13
Profile

Joined: 07 Jun 2012 06:41
Posts: 6
Post Re: Replacing the last occurrence of char on every line of f
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.


07 Jun 2012 11:18
Profile

Joined: 10 Feb 2012 02:20
Posts: 4501
Post Re: Replacing the last occurrence of char on every line of f
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:
@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:
@echo off
for /f "delims=" %%a in (filein.txt) do (
>>"fileout.txt" echo %%~pa" archives="%%~nxa
)


07 Jun 2012 11:42
Profile

Joined: 07 Jun 2012 06:41
Posts: 6
Post Re: Replacing the last occurrence of char on every line of f
thank you very much foxidrive, your first code suggested does just what i need.

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


07 Jun 2012 14:05
Profile
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 


Who is online

Users browsing this forum: Aacini, Bing [Bot], Google [Bot] and 10 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Forum style by Vjacheslav Trushkin for Free Forums/DivisionCore.