aspose file tools*
The moose likes Ant, Maven and Other Build Tools and the fly likes replace word in file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Engineering » Ant, Maven and Other Build Tools
Bookmark "replace word in file" Watch "replace word in file" New topic
Author

replace word in file

kumar hai
Greenhorn

Joined: Oct 12, 2009
Posts: 26
Hi.Using ANT scripts i need to replace the one word location from particular files

example:
file:test.txt

55 <name>user.name.family</name>
56 </user-attribute>
57 </portlet-app>

123 <name>user.name.given</name>
124 </user-attribute>
125


In above i mention few line details ,there line no:57 (</porlet-app>) this line i need to replace to line no:125

i need to remove from 57 line,paste to line no:125.

Please help for this...

suresh

Peter Johnson
author
Bartender

Joined: May 14, 2008
Posts: 5836
    
    7

I'm sorry, but I did not follow your description. Perhaps if you showed the expected output file contents that would help.

Usually, to replace text in a file you use the copy task with a filterchain. For example, I often use generic config files that look like this:



Then I use this copy statement:



Then by setting the my.value property I get a new file with the desired value; example, entering:

ant -Dmy.value=foobar

yields:



There are many more filters you can use in a filter chain, see the Ant docs at http://ant.apache.org/manual/CoreTypes/filterchain.html


JBoss In Action
kumar hai
Greenhorn

Joined: Oct 12, 2009
Posts: 26
Thanks for your reply...


I want to replace one particular text (this text available in middle of one file.so i want to replace to end(last) . from one line to another line...

please arrange me sample scripts for this........

Peter Johnson
author
Bartender

Joined: May 14, 2008
Posts: 5836
    
    7

The replacetoken filterchain item might work.
Vijay Rajendran
Greenhorn

Joined: Mar 30, 2006
Posts: 4
Hi,

I want to replace a word in a file using ANT script.

For Ex : Let's consider there is a line

"I like Apple" , in this line I'd like to replace the word "Apple" with "Orange" i.e the expected output should be "I like Orange".

This should be done using ANT script, any help is much appreciated.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42273
    
  64
Vijay Rajendran wrote:This should be done using ANT script, any help is much appreciated.

So ... have you tried all that was discussed in the earlier posts?


Ping & DNS - my free Android networking tools app
Vijay Rajendran
Greenhorn

Joined: Mar 30, 2006
Posts: 4
I tried the previous method, but I found the below one much more easier to replace,

<replace file="${file}" token="Apple" value="Orange" />
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: replace word in file