19

I need a cmd script that deletes the first line in my text file. The scenario is the following: I take a txt file from FTP everyday, the problem is that it comes with blank line at the top then the headers of the file. Since I'm importing that file automatically into an access table, that blank line is causing me problems.

So, I need a script that deletes the blank line and saves the file.

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199

6 Answers6

30

Windows/command prompt:

more +1 filename.ext > otherfilename.ext

That seems to work fine, however it appears that this also converts tab characters into multiple spaces.. I needed to remove the first line of a tab-delimited file before importing into postgres. That failed due to the automatic conversion of tabs to spaces by more...

Community
  • 1
  • 1
Guido Domenici
  • 5,146
  • 2
  • 28
  • 38
9

You didn't specify a platform. Here's how to do it in any *NIX environment (and Windows+Cygwin):

sed -i~ 1d target-file
Adam Rosenfield
  • 390,455
  • 97
  • 512
  • 589
7

To remove the first line, I would use

tail -n +2 source-file > target-file

If you want this on Windows, download the gnu utils to obtain a tail command. +2 means "Start at the second line".

Renze de Waal
  • 533
  • 2
  • 5
6

In windows without extra tools:

findstr /V /R "^$" filename.whatever

No extra tools needed

Preet Sangha
  • 64,563
  • 18
  • 145
  • 216
  • @Preet_Sangha i couldn't get this to work. is there something i'm missing? – FistOfFury Jun 10 '13 at 20:10
  • What happens? What is the input and what is output? – Preet Sangha Jun 10 '13 at 21:34
  • Should add that a simple output redirect gets the result into a file: `findstr /V /R "^$" filename.whatever>clean_filename.whatever` Also don't use same file for input and output! It'll put the file on 0b. – JDuarteDJ Jul 20 '16 at 14:34
5

I noticed some comments asking how to use Preet Sangha's solution. As I do not have enough rep to add a comment, I wanted to post a more complete solution here.

You can use Preet Sangha's solution as follows. This script will create a new text file without the first line of the input file.

findstr /V /R "^$" InputFile.txt > OutputFileNameWithFirstLineRemoved.txt
Nick Painter
  • 720
  • 10
  • 13
  • 1
    that would remove _any_ empty line (what seems to be wanted here; just to mention it for other searchers) – Stephan Jul 08 '15 at 16:28
  • The solution provided by Preet Sangha was the best imho, I was able to process file with over 3GB in seconds! BTW I used this code inside a BAT file to process multiple files (from smallest to largest) `FOR /F %%a in ('dir /B /O:S *.sql') do (findstr /V /R "$" %%a > clean_%%a)` – JDuarteDJ Jul 20 '16 at 14:31
  • @sangorys This has worked in every case that I have tested. Can you provide any details on the encoding of file that you are using? – Nick Painter Dec 18 '19 at 18:29
  • 1. It didn't remove the fist line for me. 2. it removed a lot of void lines in the middle of the file. – klm123 May 22 '22 at 11:15
3

I needed to do something similar today and this was what I came up with:

FOR /F "tokens=* skip=1" %A IN ('type "input_file.ext"') DO @echo %A>>"output_file.ext"

This has the advantage over the more +1 solution in that tab characters will be preserved. However, on large files this method is a lot slower than others. Also - leading spaces will be left-trimmed, which may or may not be a problem.

Jimadine
  • 998
  • 13
  • 26
  • You can make it a lot faster for big files with `(for ...... do @echo/%A)>"output.ext"` (just one redirection). (Although it should be mentioned that this removes *every* empty line) – Stephan Oct 18 '21 at 21:45