Delete lines in a text file fast

If you want to delete lines fast then all you need to do is vi/vim a text file, type in the amount of lines you want to delete (in my example I wanted to delete 10056 lines) followed by dd (no spaces). There will be no output so becareful with what number you type.

By: techie
2013-04-10 10:34:39

What Others Think

This is very-very basic vi/vim knowledge - without you will propably fail to use the editor. Therefore IMHO highly redundant to mention it here. Sorry ..
Paule · 365 weeks and 1 day ago
I'm inclined to agree with Paule, also I'm having difficultly framing a situation in which I want to delete 10056 lines without some idea of what that range might be, why 10056 lines, other than the fact that it a big(ish) number and therefore provides some context for "fast"? Anyhow, for such a task there is no need to open the file: sed -i '1,10056d' file.txt The question of range aside, should I remove such a number of lines I'd probably want to keep the original and make a revision: sed '100,700d' revision1.txt > revision2.txt
khayyam · 365 weeks and 1 day ago
I had a very large file with many different configurations in but #'d out.. I used vim and got to the line that I needed the file to start at which was 1057 so I went back to the top of the file and used 1057dd.. When I posted I used an extra 0 in the middle. It would have been a mess if I did that in the command line lol
techie · 360 weeks and 1 day ago
dG in vim is better, I think...
Zulu · 360 weeks and 1 day ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this? is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.


Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: