Click here to Skip to main content
15,897,891 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi Guys,

my nant build file,

XML
<target name="replace" descripton="replaces a line">
<property name="txt.file" value="C:\defence.txt" />
<loadfile file="${txt.file}" property="txt.file.content" />
<regex input="${txt.file.content}" pattern="please write regex pattern" />
<echo file="${txt.file}" message="Message to display" append="false" />
</target>


consider this is text file,

VB
element * CHECKEDOUT
element * UsAf_34567 -Strong # Defence1
element * RusAf_24567 -Strong # Defence2
element * ChinaAf_14567 -Strong # Defence3
element * India_12567 -Strong # Defence4
element * NK_10567 -Strong # Defence5
element * main/LATEST -Strong



please let me know how to change fourth(anyline) line, with Germany(this is for example)?
Posted
Comments
Sergey Alexandrovich Kryukov 29-Apr-14 11:14am    
Why showing some artifact, like a Nant project, manually, not using Paste? What you show in not a valid XML file — look at the first line.
—SA

1 solution

The only valid way to change anything in a text file is be reading it at least once. Why? Because lines are defined by the positions of end-of-line characters, so the lines have different length. Before you read all lines, you don't know the position of N-the line in the file.

For simple files, you can read it all in memory, and then write it all back, after your modifications. You can use File.ReadAllLines:
http://msdn.microsoft.com/en-us/library/system.io.file.readalllines.aspx[^].

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900