31

I have a text file which has more than 200 lines in it, and I just want to add a new line before line 4. I'm using Windows XP.

Example text file before input:

header 1
header 2
header 3
details 1
details 2

After output:

header 1
header 2
header 3
<----- This is new line ---->
details 1
details 2
smci
  • 32,567
  • 20
  • 113
  • 146
newbie18
  • 323
  • 1
  • 3
  • 6

4 Answers4

41

I believe you are using the

echo Text >> Example.txt 

function?

If so the answer would be simply adding a "." (Dot) directly after the echo with nothing else there.

Example:

echo Blah
echo Blah 2
echo. #New line is added
echo Next Blah
avitex
  • 2,478
  • 3
  • 22
  • 22
  • 1
    thanks your reply james Dyson. i need to edit the existing file instead of new file – newbie18 Sep 13 '11 at 06:36
  • 4
    using the `>>` to direct the output to file will append to the end of the file. If you use a single chevron then the output will overwrite the existing file if it does exist, or create a new file if it does not. – Ahmad Sep 13 '11 at 07:36
  • 6
    I'm not really sure how this answers the question. It seems the text file already exists and the OP just wanted to insert a new line in the middle somewhere, not the end. – General Grievance Oct 07 '20 at 12:43
4

DISCLAIMER: The below solution does not preserve trailing tabs.


If you know the exact number of lines in the text file, try the following method:

@ECHO OFF
SET origfile=original file
SET tempfile=temporary file
SET insertbefore=4
SET totallines=200
<%origfile% (FOR /L %%i IN (1,1,%totallines%) DO (
  SETLOCAL EnableDelayedExpansion
  SET /P L=
  IF %%i==%insertbefore% ECHO(
  ECHO(!L!
  ENDLOCAL
)
) >%tempfile%
COPY /Y %tempfile% %origfile% >NUL
DEL %tempfile%

The loop reads lines from the original file one by one and outputs them. The output is redirected to a temporary file. When a certain line is reached, an empty line is output before it.

After finishing, the original file is deleted and the temporary one gets assigned the original name.


UPDATE

If the number of lines is unknown beforehand, you can use the following method to obtain it:

FOR /F %%C IN ('FIND /C /V "" ^<%origfile%') DO SET totallines=%%C

(This line simply replaces the SET totallines=200 line in the above script.)

The method has one tiny flaw: if the file ends with an empty line, the result will be the actual number of lines minus one. If you need a workaround (or just want to play safe), you can use the method described in this answer.

Community
  • 1
  • 1
Andriy M
  • 76,112
  • 17
  • 94
  • 154
  • 1) this script i save as run1.bat. i test to run it, got error msg (the syntax of command is incorrect.) the file rename as after.txt – newbie18 Sep 13 '11 at 06:33
  • 2) the format is out of order
    for example: input header 1<--here got tab delimited format--><--here got tab delimited format--> header 2<--here got tab delimited format--><--here got tab delimited format--> header 3<--here got tab delimited format--><--here got tab delimited format--> details 1 details 2 output: header 1<--tab delimited is missing--><--tab delimited is missing--> header 2<--tab delimited is missing--><--tab delimited is missing--> header 3<--tab delimited is missing--><--tab delimited is missing--> details 1 details 2
    – newbie18 Sep 13 '11 at 06:36
  • I tested the script too and didn't get any errors. As for your issue #2... Well, using a batch script to process a file with such tender contents is like mowing a lawn with a combine harvester: it *might* be possible, but requires **very** thorough adjustments. I re-tested the script and it indeed removes trailing tab characters. So far I have no idea how to work around that. Someone else might. Or maybe you should consider using other tools. You could also try your luck at [superuser](http://superuser.com/). – Andriy M Sep 13 '11 at 06:55
  • item 1 - still have error this is the script. @ECHO OFF
    SET origfile="C:\Documents and Settings\user\Desktop\test1\before.txt"
    SET tempfile="C:\Documents and Settings\user\Desktop\test1\after.txt"
    SET insertbefore=4
    FOR /F %%C IN ('FIND /C /V "" ^<%origfile%') DO SET totallines=%%C
    <%origfile% (FOR /L %%i IN (1,1,%totallines%) DO (
    SETLOCAL EnableDelayedExpansion
    SET /P L=
    IF %%i==%insertbefore% ECHO(
    ECHO(!L!
    ENDLOCAL
    )
    ) >%tempfile%
    DEL %origfile%
    RENAME %tempfile% %origfile%
    pause
    – newbie18 Sep 13 '11 at 07:02
  • I see now, thanks. Please change the last two lines of the script like in my updated answer. The problem was, the second argument of the RENAME command should be just a name, with no path. – Andriy M Sep 13 '11 at 07:16
3

You can use:

type text1.txt >> combine.txt
echo >> combine.txt
type text2.txt >> combine.txt

or something like this:

echo blah >> combine.txt
echo blah2 >> combine.txt
echo >> combine.txt
echo other >> combine.txt
Shawn Chin
  • 84,080
  • 19
  • 162
  • 191
khaled
  • 39
  • 2
  • 4
    This won't work in Windows because the echo command with nothing following it actually prints the echo state, "ECHO is ON" for example. – mtalexan Dec 20 '13 at 19:38
  • 3
    Though it is an old post, if someone wants to use this solution in windows, please use _echo. >> combine.txt_. The additional "dot" after echo should solve the "ECHO is ON" problem. – Saneesh kumar Apr 20 '18 at 01:28
  • @Saneeshkumar you should make that into an answer. – oxygen May 08 '18 at 09:31
-2

Suppose you want to insert a particular line of text (not an empty line):

@echo off
FOR /F %%C IN ('FIND /C /V "" ^<%origfile%') DO SET totallines=%%C
set /a totallines+=1

@echo off
<%origfile% (FOR /L %%i IN (1,1,%totallines%) DO (
  SETLOCAL EnableDelayedExpansion
  SET /p L=
  IF %%i==%insertat% ECHO(!TL!
  ECHO(!L!
  ENDLOCAL
)
) >%tempfile%

COPY /Y %tempfile% %origfile% >NUL

DEL %tempfile%
Jan Doggen
  • 8,799
  • 13
  • 70
  • 144