Add a line in middle of txtfile in cmd

前端 未结 1 539
失恋的感觉
失恋的感觉 2020-12-22 01:45

here\'s a text file txtfile.txt

line1
line2
add after this line
line4
etc
etc

i want to create a batch file which can add a li

相关标签:
1条回答
  • 2020-12-22 02:11
    @ECHO OFF
    SETLOCAL
    SET /p info="enter info : "
    :: read addafter line
    (
    FOR /f "delims=" %%i IN (poison1.txt) DO (
    SET addafter=%%i
    FOR /f "delims=" %%n IN (' findstr /n "^" txtfile.txt') DO (
    SET line=%%n
    SETLOCAL ENABLEDELAYEDEXPANSION
    SET line=!line:*:=!
    ECHO(!line!
    IF "!line!"=="!addafter!" ECHO(%info%
    ENDLOCAL
    )
    )
    )>newfile.txt
    
    FC newfile.txt txtfile.txt
    
    GOTO :eof
    

    Where poison1.txt contains the one line

    "A line !of! ] many < & >var*ied %poison ^ char;ac(ters) | like "," a\nd+so=on"
    

    and txtfile.txt contains this line.

    • Read the line from the poison1.txt file to addafter
    • For each line in the add-to-me file
      • Number the line to catch empty lines
      • Remove the number and first colon (added by FINDSTR)
      • Output the line verbatim
      • if the line matches the target, output the extra line

    done!

    0 讨论(0)
提交回复
热议问题