Delete first line of text file

怎甘沉沦 提交于 2019-12-13 11:33:18

问题


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.


回答1:


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...




回答2:


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

sed -i~ 1d target-file



回答3:


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".




回答4:


In windows without extra tools:

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

No extra tools needed




回答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



回答6:


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.



来源:https://stackoverflow.com/questions/600653/delete-first-line-of-text-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!