What's the opposite of head? I want all but the first N lines of a file

Command LineScriptingTailUnix Head

Command Line Problem Overview


Given a text file of unknown length, how can I read, for example all but the first 2 lines of the file? I know tail will give me the last N lines, but I don't know what N is ahead of time.

So for a file

AAAA
BBBB
CCCC
DDDD
EEEE

I want

CCCC
DDDD
EEEE

And for a file

AAAA
BBBB
CCCC

I'd get just

CCCC

Command Line Solutions


Solution 1 - Command Line

tail --help gives the following:

  -n, --lines=K            output the last K lines, instead of the last 10;
                           or use -n +K to output lines starting with the Kth
      

So to filter out the first 2 lines, -n +3 should give you the output you are looking for (start from 3rd).

Solution 2 - Command Line

Assuming your version of tail supports it, you can specify starting the tail after X lines. In your case, you'd do 2+1.

tail -n +3

[mdemaria@oblivion ~]$ tail -n +3 stack_overflow.txt
CCCC
DDDD
EEEE

Solution 3 - Command Line

A simple solution using awk:

awk 'NR > 2 { print }' file.name

Solution 4 - Command Line

Try sed 1,2d. Replace 2 as needed.

Solution 5 - Command Line

tail -n +linecount filename will start output at line linecount of filename, so tail -n +3 filename should do what you want.

Solution 6 - Command Line

Use this, supposing the first sample is called sample1.dat then tail --lines=3 sample1.dat which would print all lines from the 3rd line to the last line.

For the second sample, again suppose it is called sample2.dat it would be tail --lines=-1 sample2.dat which would print the last line...

Solution 7 - Command Line

I really don't know how to do it from just tail or head but with the help of wc -l (line count) and bash expression, you can achieve that.

tail -$(( $( wc -l $FILE | grep -Eo '[0-9]+' ) - 2 )) $FILE

Hope this helps.

Solution 8 - Command Line

using awk to get all but the last 2 line

awk 'FNR==NR{n=FNR}FNR<=n-3{print}' file file

awk to get all but the first 2 lines

awk 'NR>2' file

OR you can use more

more +2 file

or just bash

#!/bin/bash

i=0
while read -r line
do
  [[ $i > 1 ]] && echo "$line"
  ((i++))
done <"file"

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNicholas M T ElliottView Question on Stackoverflow
Solution 1 - Command LineJoe EnosView Answer on Stackoverflow
Solution 2 - Command LineMike DeMariaView Answer on Stackoverflow
Solution 3 - Command LinejanmView Answer on Stackoverflow
Solution 4 - Command LinelhfView Answer on Stackoverflow
Solution 5 - Command LineJim LewisView Answer on Stackoverflow
Solution 6 - Command Linet0mm13bView Answer on Stackoverflow
Solution 7 - Command LineNawaManView Answer on Stackoverflow
Solution 8 - Command Lineghostdog74View Answer on Stackoverflow