문제

나는 이것에 또 다른 것을 게시했지만, 나는 그것을 정말로 편집해야했다 ...

베일 치는 배치 (vbscript 포함)가 txtfile에서 29 행을 찾을 수 있다는 것입니다 ...이 줄을 편집해야합니다.

29 행이 다음과 같다면 : '옵션 = 21'은 '옵션 = 22'로 변경해야합니다.

문제는이 줄이 파일에 더 많이 위치한다는 것입니다. 그래서 그것은 단지 라인 29를 편집해야합니다 ...

어떻게???

제발 맞춤형 프로그램은 없습니다 ;;; 괜찮은 것을 설치하지 않고 모든 사용자가 수행해야합니다.

도움이 되었습니까?

해결책

이것은 일반적으로 배치로하고있는 일이 아니지만 상당히 간단합니다.

@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

당신이 그것을 조정하려면 일반적인 방향을 지적해야합니다.

다른 팁

나는 당신이 a를 요구하는 것을 본다 vb 스크립트,
사용 AWK, 그것은 이런 식으로 갈 것입니다.
어쩌면 그것은 당신이 코드에 도움이 될 것입니다 vb.

awk '{if (fnr == 29) {gsub (/옵션 = 21/, "옵션 = 22"); print} else {print $ 0;}} 'input.txt> output.txt

시도하지 않았으므로 약간의 결함이있을 수 있습니다 ...

  1. 그만큼 FNR=29 처리를 확인합니다 gsub 29 행에서만
    • gsub 대체 할 것입니다 option=21 라인에 발생합니다 option=22
    • 다른 줄은 통과됩니다

다음은 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    

사용하는 방법:

c:\test> cscript /nologo myscript.vbs > newfile
c:\test> ren newfile file.txt
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top