I need to repeatedly remove the first line from a huge text file using a bash script.
Right now I am using sed -i -e '1d' $FILE – but it takes around a minute to do the deletion.
Is there a more efficient way to accomplish this?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Try tail:
-n x: Just print the lastxlines.tail -n 5would give you the last 5 lines of the input. The+sign kind of inverts the argument and maketailprint anything but the firstx-1lines.tail -n +1would print the whole file,tail -n +2everything but the first line, etc.GNU
tailis much faster thansed.tailis also available on BSD and the-n +2flag is consistent across both tools. Check the FreeBSD or OS X man pages for more.The BSD version can be much slower than
sed, though. I wonder how they managed that;tailshould just read a file line by line whileseddoes pretty complex operations involving interpreting a script, applying regular expressions and the like.Note: You may be tempted to use
but this will give you an empty file. The reason is that the redirection (
>) happens beforetailis invoked by the shell:$FILEtailtailprocess to$FILEtailreads from the now empty$FILEIf you want to remove the first line inside the file, you should use:
The
&&will make sure that the file doesn’t get overwritten when there is a problem.