DosTips.com

A Forum all about DOS Batch
It is currently 29 Apr 2016 23:10

All times are UTC-06:00




Post new topic  Reply to topic  [ 7 posts ] 
Author Message
PostPosted: 07 Jun 2012 06:46 
Offline

Joined: 07 Jun 2012 06:41
Posts: 6
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..


Top
   
PostPosted: 07 Jun 2012 07:37 
Offline

Joined: 23 Dec 2011 13:59
Posts: 3033
Then why not just use sed to do this as well.


Top
   
PostPosted: 07 Jun 2012 08:35 
Offline

Joined: 07 Jun 2012 06:41
Posts: 6
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.


Top
   
PostPosted: 07 Jun 2012 09:13 
Offline

Joined: 10 Feb 2012 02:20
Posts: 5567
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.


Top
   
PostPosted: 07 Jun 2012 11:18 
Offline

Joined: 07 Jun 2012 06:41
Posts: 6
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.


Top
   
PostPosted: 07 Jun 2012 11:42 
Offline

Joined: 10 Feb 2012 02:20
Posts: 5567
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
)


Top
   
PostPosted: 07 Jun 2012 14:05 
Offline

Joined: 07 Jun 2012 06:41
Posts: 6
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
)


Top
   
Display posts from previous:  Sort by  
Post new topic  Reply to topic  [ 7 posts ] 

All times are UTC-06:00


Who is online

Users browsing this forum: Bing [Bot], Yahoo [Bot] and 11 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® Forum Software © phpBB Limited