0

This is my text file and I want to remove first line (with blank):

abc!
def  #
ghi./;
jklm
nopqrs

How can I remove first line from my text file?

I want my second line be first line:

def  #
ghi./;
jklm
nopqrs

I tried findstr command, but it doesn't work.

  • 1
    note sure why people down-vote it, but I think this is a good question, and the answers are good too. – jyao Jan 12 '19 at 6:14
5

The easiest approach (assuming your text file is called data.txt):

more +1 "data.txt" > "data_NEW.txt"

This limits lines to a length of about 65535 bytes/characters and the file to about 65535 lines. Furthermore, TABs become expanded to SPACEs (8 by default).


You could use a for /F loop instead (the odd-looking unquoted option string syntax is needed here in order to disable the default eol character ; to not ignore lines beginning with such):

@echo off
(
    for /F usebackq^ skip^=1^ delims^=^ eol^= %%L in ("data.txt") do echo(%%L
) > "data_NEW.txt"

This limits lines to a length of about 8190 characters/bytes and loses empty lines.


You could use a for /F loop together with findstr to keep empty lines (findstr adds a line number plus : to each line, so for /F does not see empty lines; everything up to the (first) colon is then removed in the loop body; toggling delayed expansion ensures not to lose !):

@echo off
(
    for /F "skip=1 delims=" %%L in ('findstr /N "^" "data.txt"') do (
        set "LINE=%%L"
        setlocal EnableDelayedExpansion
        echo(!LINE:*:=!
        endlocal
    )
) > "data_NEW.txt"

This still limits lines to a length of about 8190 characters/bytes.


Or you could use input redirection < together with set /P (for this the total number of lines needs to be determined in advance):

@echo off
for /F %%C in ('find /C /V "" ^< "data.txt"') do set "COUNT=%%C"
setlocal EnableDelayedExpansion
(
    for /L %%I in (1,1,%COUNT%) do (
        set "LINE=" & set /P LINE=""
        if %%I gtr 1 echo(!LINE!
    )
) < "data.txt" > "data_NEW.txt"
endlocal

This limits lines to a length of about 1022 characters/bytes.


To replace your original file with the modified one, simply do this:

move /Y "data_NEW.txt" "data.txt" > nul
  • 1
    Reminds me of the days I used more on a regular base on windows, I never really use it these days... thumbs up for that one :) – Gerhard Barnard May 25 '18 at 17:12
  • 1
    Thank you, @GerhardBarnard! Your comment also reminded me that I forgot to mention another limitation that comes up using more -- see my edit... – aschipfl May 25 '18 at 17:58
1

From the prompt,

for /f "skip=1delims=" %a in (yourtextfilename) do >>anoutputfilename echo %a

where yourtextfilename & anoutputfilename must be different filenames and need to be "quoted" if they contain spaces or other separators.

  • This works for the sample data in the question, however, if the sample data has spaces in each line, this answer does not work. Strictly speak, this answer does not work either, there is a typo here. The correct answer should be do @echo %a >> anooutputfilename instead of putting echo %a at the very end. – jyao Jan 12 '19 at 6:18
  • @jyao : please provide sample data for where this fails. I have tried it using spaces in each line and it worked perfectly. If you mean empty lines or lines that contain only spaces, then yes, empty lines will be eliminated (characteristic of for /f) and lines containing only spaces will be replaced by ECHO is on. (characteristic of echo). Lines starting ; will also be eliminated (characteristic of findstr) – Magoo Jan 12 '19 at 23:26
  • @jyao Your assertion about the syntax of the redirected-echo is completely wrong however. It does not matter whether the redirection appears before or after the echo and the space before the >> will be reproduced so each output line will be terminated by an extra space. Actually, this is the reason for using the unconventional redirection-before-echo structure. – Magoo Jan 12 '19 at 23:29
  • I guess my assertion about the syntax is indeed not right because when I did the test, I used @echo (trying to prevent displaying echo), without using @, your command indeed works. My apology. – jyao Jan 13 '19 at 4:32
1

I suggest tail to output lines starting with line number 2:

tail -n +2 in.txt > out.txt

From the man page:

       -n, --lines=[+]NUM
          output the last NUM lines, instead of the last  10;  or  use  -n
          +NUM to output starting with line NUM
0

This is pretty easy with PowerShell. If the file is not large, Get-Content works well.

Get-Content -Path .\t.txt | Select-Object -Skip 1

If you must run from a cmd.exe shell, then use:

powershell -NoProfile -Command "Get-Content -Path .\t.txt | Select-Object -Skip 1"
0

If you like using Vim with CMD you can use: gvim -c "execute \"normal! #dd\"" file.txt, where "#" is the number of lines you want to delete off of the top of the text file ("#" could "1", "82", or another number). For this question you want gvim -c "execute \"normal! 1dd\"" file.txt.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.