How can I replace a part of a certain line in a text file

July 11, 2013 at 11:33:45
Specs: Windows 7
For example:

I want to change on the 3rd line the last CCC to EEE.

AAAAAAAAA
BBBBBBBBB
CCCCCCCC
DDDDDDDD

should become

AAAAAAAAA
BBBBBBBBB
CCCCCEEE
DDDDDDDD


See More: How can I replace a part of a certain line in a text file

Report •


#1
July 11, 2013 at 12:16:46
::====== script starts here ===============
::
:: chg.bat 2013-07-12 2:09:25.51
@echo off > NEWFILE & setLocal enableDELAYedeXpansioN

set N=
:main
for /f "tokens=* delims= " %%a in (myfile) do (
set/a N+=1
set S=%%a
If !N! equ 3 set S=!S:~0,-3!EEE
echo.!S!
)>> NEWFILE
goto :eof
::====== script ends here =================

=====================
M2 Golden-Triangle


Report •

#2
July 11, 2013 at 13:59:27
How about a scripting language like perl (editing myfile.txt in place):

perl -wnli -e ' $. == 3 and s/CCC$/EEE/; print; ' myfile.txt


Report •

Related Solutions


Ask Question