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
Try GNU tail:
tail -n +2 "$FILE"
-n x: Just print the last
tail -n 5 would give you the last 5 lines of the input. The
+ sign kind of inverts the argument and make
tail print anything but the first
tail -n +1 would print the whole file,
tail -n +2 everything but the first line, etc.
Note: You may be tempted to use
# THIS WILL GIVE YOU AN EMPTY FILE! tail -n +2 "$FILE" > "$FILE"
but this will give you an empty file. The reason is that the redirection (
>) happens before
tail is invoked by the shell:
tailreads from the now empty
If you want to remove the first line inside the file, you should use:
tail -n +2 "$FILE" > "$FILE.tmp" && mv "$FILE.tmp" "$FILE"
&& will make sure that the file doesn't get overwritten when there is a problem.