29

for example i have this file :

cat myfile
1
2
3
4
5

i want to print all lines except first 2 line . output should be like this :

tail -n $(( $(wc -l myfile | awk '{print $1}') - 2 )) myfile
3
4
5

Yes , out put is correct. but there is a problem , we have 5 line in this sample file right ? if i use more that 5 in this command output should be empty but it is not !!!

tail -n $(( $(wc -l myfile | awk '{print $1}') - NUMBER )) myfile

this outout should be empty but it is not

tail -n $(( $(wc -l myfile | awk '{print $1}') - 8 )) myfile

1
2
3
4
5

myfile can contain X lines... Thanks for help

network
  • 401
  • 1
  • 4
  • 4

2 Answers2

50

tail -n+3 outputs all lines starting from the third one.

head -n-3 outputs all lines except for the last three.

ctrueden
  • 123
choroba
  • 20,299
2

I know this is old, but for posterity.

given the input (as posted by OP),

cat << EOF > myfile
1
2
3
4
5
EOF

you can solve the problem using awk

awk 'FNR > 2 {print $1}' myfile

will yield desired result

3
4
5

tested with awk version (GNU Awk 4.1.4, API: 1.1 (GNU MPFR 4.0.1, GNU MP 6.1.2))

bgs
  • 21