E
E
Eugene2015-08-06 14:38:01
cmd/bat
Eugene, 2015-08-06 14:38:01

How to replace the nth line in a text file using cmd?

Hello. Subject. It is necessary to replace the 13th line in the file with a previously known one. I write
for /f "skip=12" in (settings.ini) do (echo dparm=BIND=0,lck='ru',APP='GPS',HST='%COMPUTERNAME%')
As planned, I should skip 12 lines from the beginning of the file and replace with whatever is in parentheses. In fact, zero response. It may be necessary to interrupt the cycle, as well. the replacement is made once, but I'm not sure, this is an assumption, I had no business with smd before.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
E
Eugene, 2015-08-06
@tw1ggyz

In general, I climbed more on the forums and did it. Initially, the task was to replace a piece in the file with the name of the computer on which the file is running. But I was smart, I decided to replace the whole line, so I fiddled around for a long time. Everything turned out to be more prosaic. In the example below, pc-name is the chunk to be replaced by the computer name retrieved via %computername%.
@echo off
setlocal enabledelayedexpansion
Set infile=settings.ini
Set find=pc-name
Set replace=%COMPUTERNAME%
@echo off
setlocal enabledelayedexpansion
set COUNT=0
for /F "tokens=* delims=, eol=#" %%n in (!infile!) do (
set LINE=%%n
set TMPR=!LINE:%find%=%replace%!
Echo !TMPR!>>TMP.TXT
)
move TMP.TXT %infile%

T
TyzhSysAdmin, 2015-08-06
@POS_troi

Is the content in the 12th line known?
as an option
You can PowerShell

$FilePath = C:\path_to_settings.ini
(Get-Content $FilePath) -replace 'parametr =.*','parametr = "YOUR TEXT"' | Out-File $FilePath

E
Eugene, 2015-08-06
@yellowmew

On PowerShell, just
Run PoSH from a script on the command line: (contents of a bat file)
And no one forces you to write monstrous designs, right? :)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question