UNIX command to delete a line from a file?

I’ve been trying to find the answer to this through Google, but I can’t come up with the magic words.

How do I delete the first (or indeed any) line from a text file in UNIX?

I can’t load the file into vi (which is the only editor on our system) because it is too large - over 300MB.

Any help would be great…

The tail command should do it:

Here’s the man page

I think you need to think of it more as sucking out all but the one line, and pushing that into a new file…you will use grep to locate the culprit line…

rather than give my way of doing it, this article I just drummed up should give you some ideas…
http://www.computing.net/unix/wwwboard/forum/3053.html

OK, I like the idea of “sucking out all but the one line”, and making a new file.

So, using that idea and the man page, the command would be:

tail +1 oldfile.txt > newfile.txt

to delete the first line. Is that right?

look good to me, plus, since you are piping the output to a new file, there is not much risk.

Thanks man. You guys are great. :slight_smile:

(And I’ll bookmark the sites too.)

You could also do this with sed or awk, if available on your system, but I don’t remember the exact commands. If you’re going to be doing a lot with UNIX, those will be very useful tools to know.

sed -e 1d oldfile > newfile

would be one way. sed is often my tool of choice for ad-hoc
file manipulation. I thought I’d let this pass since the OP solved
his problem, but since you brought it up …

To delete an arbitrary line from a file, you could grep it out:



grep -v foo file.txt > newfile.txt


That would send all lines not matching the pattern “foo” (-v option) to newfile.txt.

Yes, but that (grep) would only work if that line alone matches some specific pattern. In a file that size, it seems quite possible that you’d have duplicate entries, so at the least you’d want to do a straight grep to stdout first, to check if the line is unique.

That’s true, Chronos, though I am assuming that this is a data file of some sort with a fairly predictable format, or that a specific enough regex could be composed to isolate a single line.