Batch to replace text in text file

Lenovo / C200
May 25, 2009 at 14:34:25
Specs: Windows XP
If I have next text in text file:

aaaaaaa ccccccc ggggggg MARKO llllll
nnnnn MARKO jjjjjjj kkkkkkkkk MARKO
bbbbbbbb nnnnnnnn MARKO dddddd

I'd like a batch file which will be able to replace MARKO with MARKO-1, MARKO-2,...

So my new.txt has to look like:

aaaaaaa ccccccc ggggggg MARKO-1 llllll
nnnnn MARKO-2 jjjjjjj kkkkkkkkk MARKO-3
bbbbbbbb nnnnnnnn MARKO-4 dddddd

Can someone help me?


See More: Batch to replace text in text file

Report •


#1
May 25, 2009 at 18:25:42
if you have Python on Windows
import fileinput
i=0
for line in fileinput.FileInput("file",inplace=1):
    line=line.strip()
    if "MARKO" in line:
        line=line.split()
        for n,item in enumerate(line):
            if item=="MARKO":
                i=i+1
                line[n]="MARKO-"+str(i)
        print ' '.join(line)



save as myscript.py and on command prompt
c:\test> python myscript.py
c:\test> more file
aaaaaaa ccccccc ggggggg MARKO-1 llllll
nnnnn MARKO-2 jjjjjjj kkkkkkkkk MARKO-3
bbbbbbbb nnnnnnnn MARKO-4 dddddd



Report •

#2
May 25, 2009 at 23:57:42
Thank you so much for your help, but I don't have Python.
Obviusly, I'll have to resolve this issue on another way.
Thank you again.

Report •

#3
May 26, 2009 at 00:19:20
Change based on what?


=====================================
If at first you don't succeed, you're about average.

M2


Report •

Related Solutions

#4
May 26, 2009 at 00:21:04
if doesn't matter if you have it or not. The algorithm is already shown very clearly to you. Whatever language you use, you can implement it using your language's syntax.
1) open the file for reading
2) split the line on spaces. store in array
3) go through the array, each time MARKO is found, increment counter and create the new word
4) print to standard out the whole modified line
etc.
etc

Report •


Ask Question