Page 1 of 1

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

Posted: 29 Jun 2012 00:09
by kuuuuu
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.

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

Posted: 29 Jun 2012 01:27
by Ed Dyreen
'
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

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

Posted: 29 Jun 2012 01:37
by kuuuuu
Hello dyreen,
The above code is working fine but the result is not geting copied to out file.

Thanx a ton.. :) :)

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

Posted: 29 Jun 2012 02:33
by foxidrive
Another option: GnuSED for Windows produces the result you need



Code: Select all

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