Encontre a linha e editá-lo
-
12-09-2019 - |
Pergunta
Eu postei uma outra sobre este ,, mas eu tinha para editá-lo realmente muito ...
O que a coisa bassicly é que um lote (Talvez incluindo VBScript) pode encontrar linha 29 em um txtfile ... deve editar esta linha.
Se a linha 29 é assim: 'option = 21' deve alterá-lo para 'option = 22'
O problema é que esta linha está localizado mais no arquivo. por isso deve apenas editar linha 29 ...
Como fazer ???
[por favor, não programas personalizados ;;; isso deve ser feito por todos os usuários sem instalar algo OK.]
Solução
Isto não é algo que você está fazendo geralmente no lote, mas é bastante simples:
@echo off
setlocal enableextensions enabledelayedexpansion
rem the input file
set inputfile=file.txt
rem temporary file for output, we can't just write into the
rem same file we're reading
set tempfile=%random%-%random%.tmp
rem delete the temporary file if it's already there
rem shouldn't really happen, but just in case ...
copy /y nul %tempfile%
rem line counter
set line=0
rem loop through the file
for /f "delims=" %%l in (%inputfile%) do (
set /a line+=1
if !line!==29 (
rem hardcoded, at the moment, you might want to look
rem here whether the line really starts with "options"
rem and just put another number at the end.
echo option=22>>%tempfile%
) else (
echo %%l>>%tempfile%
)
)
del %inputfile%
ren %tempfile% %inputfile%
endlocal
Deve apontar para a direção geral, se você quiser adaptá-lo.
Outras dicas
Eu vejo que você está pedindo um script vb
,
Usando AWK
, seria algo como isto,
talvez ele vai ajudar você vb
código.
awk '{if (FNR == 29) {gsub (/ option = 21 /, "option = 22"); print} else {print $ 0;}}' input.txt> output.txt
não tentei isso, então pode ter algumas falhas leves ...
- o
FNR=29
irá verificar o processamentogsub
apenas na linha 29-
gsub
irá substituir quaisquer ocorrênciasoption=21
na linha comoption=22
- outras linhas serão passados ??através
-
aqui está um vbscript
Set objFS = CreateObject("Scripting.FileSystemObject")
strFile = "c:\test\file.txt"
Set objFile = objFS.OpenTextFile(strFile)
Do Until objFile.AtEndOfStream
linenum = objFile.Line
strLine = objFile.ReadLine
If linenum = 29 Then
strLine = Replace(strLine,"option=21","option=22")
End If
WScript.Echo strLine
Loop
Como usar:
c:\test> cscript /nologo myscript.vbs > newfile
c:\test> ren newfile file.txt