Re: Code for how to append a string for every occurance.

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
kuuuuu
Posts: 2
Joined: 29 Jun 2012 00:02

Re: Code for how to append a string for every occurance.

#1 Post by kuuuuu » 29 Jun 2012 00:09

abc.txt contains
sachin
ramesh
suresh
ramesh
ganguly

i want to append a string lets say "xyz" for every occurance of "ramesh" in the above file.

output shud be :
sachin
xyz
ramesh
suresh
xyz
ramesh
ganguly

thanx in advance ..reply awaited.

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Code for how to append a string for every occurance.

#2 Post by Ed Dyreen » 29 Jun 2012 01:27

'
Assuming case insensitive match.

Code: Select all

@echo off &setlocal enableDelayedExpansion

set "$inFile=abc.txt"
set "$ouFile=out.txt"

type "!$inFile!"

> "!$ouFile!" (

   for /f "usebackq delims=" %%? in (

          "!$inFile!"

   ) do   set "$=%%?" &if /i "!$:ramesh=!" == "%%?" (

          (echo.%%?)
   ) else (echo.xyz) &(echo.%%?)
)

type "!$ouFile!"
pause

endlocal &exit /b 0

kuuuuu
Posts: 2
Joined: 29 Jun 2012 00:02

Re: Code for how to append a string for every occurance.

#3 Post by kuuuuu » 29 Jun 2012 01:37

Hello dyreen,
The above code is working fine but the result is not geting copied to out file.

Thanx a ton.. :) :)

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Code for how to append a string for every occurance.

#4 Post by foxidrive » 29 Jun 2012 02:33

Another option: GnuSED for Windows produces the result you need



Code: Select all

@echo off
sed "s/^ramesh$/xyz\n&/" abc.txt >out.txt

Post Reply