通过批处理文件将几行从一个文本复制到另一个文本

时间:2016-04-30 22:40:02

标签: batch-file batch-processing

嘿伙计们,我需要你的帮助。我是业余爱好者。我已经尝试了很多来解决这个问题,而不是没有。

@ECHO off

C:

CD "C:\Program Files (x86)\GnuWin32\bin"

SED = "G:\mini thesis\New folder\input.txt" > "G:\mini thesis\New folder\output.txt"

sed 1p,3p G:\mini thesis\New folder\input.txt>G:\mini thesis\New folder\output.txt

此代码将所有行从输入复制到输出,我不知道我的错误。

@echo off
setlocal

( set /P "line1=" & set /P "line2=" ) < input.txt
(
   echo %line1%
   echo %line2%

) > output.txt

我想从第22行复制到第84行,但我不知道使用哪种语法 请帮帮我们

2 个答案:

答案 0 :(得分:0)

您可以尝试使用此混合代码Batch和vbscript来实现您的目标:

@echo off
Rem Just Change those params to yours ie InputFile and OutPutFile
Set "InputFile=C:\temp\Input.txt"
Set "OutPutFile=C:\temp\Output.txt"
Set FromLine=22
Set ToLine=84
::********************************************************************************************************
Call:ExtractLinesFromTextFile "%InputFile%" %FromLine% %ToLine% > "%OutPutFile%"
If Exist "%OutPutFile%" Start "" "%OutPutFile%"
Exit /b
::********************************************************************************************************
:ExtractLinesFromTextFile <InputFile> <FromLine> <ToLine>
(
    echo Wscript.echo(ExtractLinesFromTextFile("%~1",%2,%3^)^)
    echo Function ExtractLinesFromTextFile(TextFile,FromLine,ToLine^)
    echo     If FromLine ^<= ToLine Then
    echo         With CreateObject("Scripting.FileSystemObject"^).OpenTextFile(TextFile^)
    echo             Do Until .Line = FromLine Or .AtEndOfStream
    echo                 .SkipLine
    echo             Loop
    echo             Do Until .Line ^> ToLine Or .AtEndOfStream
    echo                 ExtractLinesFromTextFile = ExtractLinesFromTextFile ^& (.ReadLine ^& vbNewLine^)
    echo             Loop
    echo         End With
    echo     Else
    echo         Wscript.echo "Lines overflow error", vbCritical, "Lines overflow error"
    echo     End If
    echo End Function
)>"%~n0.vbs"
Cscript /Nologo "%~n0.vbs" "%~1" %~2 %~3
If Exist "%~n0.vbs" Del "%~n0.vbs"
Exit /b
::********************************************************************************************************

答案 1 :(得分:0)

要从文本文件中读取某一行,请使用带有for /F选项的skipgoto来打破循环。每行前面都有findstr行号,以便不会丢失空行:

rem This reads the 22nd line of a text file:
for /F "skip=21 delims=" %%L in ('findstr /N /R "^" "input.txt"') do (
    set "LINE=%%L"
    setlocal EnableDelayedExpansion
    echo(!LINE:*:=!
    endlocal
    goto :CONT
)
:CONT

要在文本文件中写入(替换)某一行,您需要读取文本,替换该行并将结果写入临时(临时)文件;最后,将临时文件移动到原始文件上(并覆盖它)。子例程:SUB基本上包含上面的代码片段:

@echo off
setlocal EnableExtensions DisableDelayedExpansion

rem Define constants here:
set "INFILE=input.txt"
set "OUTFILE=output.txt"
set "TEMPFILE=%TEMP%\interim.txt"
set "INLINE=22"
set "OUTLINE=84"

rem This reads a text file and replaces a certain line:
> "%TEMPFILE%" (
    for /F "delims=" %%F in ('findstr /N /R "^" "%OUTFILE%"') do (
        set "LINE=%%F"
        for /F "delims=:" %%N in ("%%F") do set "LNUM=%%N"
        setlocal EnableDelayedExpansion
        if !LNUM! EQU %OUTLINE% (
            call :SUB
        ) else (
            echo(!LINE:*:=!
        )
        endlocal
    )
)
> nul 2>&1 move /Y "%TEMPFILE%" "%OUTFILE%"

endlocal
exit /B

:SUB
rem This reads a certain line from a text file:
set /A SKIP=INLINE-1
if %SKIP% GTR 0 (set "SKIP=skip=%SKIP% ") else (set "SKIP=")
for /F "%SKIP%delims=" %%L in ('findstr /N /R "^" "%INFILE%"') do (
    set "LINE=%%L"
    setlocal EnableDelayedExpansion
    echo(!LINE:*:=!
    endlocal
    goto :EOF
)