Negative arguments to head
Asked Answered
N

2

2

I was trying using head command, in macOS using zsh, code below,

a.txt:

1
2
3
4
5
6
7
8
9
10

tail -n +5 a.txt   // line 5 to line end

tail -n -5 a.txt   // last line 5 to line end

head -n +5 a.txt // line 1 to line 5

head -n -5 a.txt  // # What did this do?

The last command shows an error.

head: illegal line count -- -5

What did head -n -5 actually do?

Nat answered 21/2, 2020 at 9:33 Comment(5)
man head should answer all your questions.Scrounge
Shows what error? And have you tried to read the head manual page?Uther
@Someprogrammerdude hi , i add the errorNat
look this https://mcmap.net/q/120211/-negative-arguments-to-headSharika
@RobbyCornelissen it doesn't, because man head on a Mac does not show any information at all, and although info head does, it still shows valid negative -n flag, which isn't supported by the macOs. So the question is valid and the answer is not in the manual :)Disharoon
S
3

Some implementations of head like GNU head support negative arguments for -n

But that's not standard! Your case is clearly not supported.

When supported The negative argument should remove the last 5 lines before doing the head

Sharika answered 21/2, 2020 at 10:41 Comment(0)
H
0

It becomes more clear, if using 3 instead of 5. Note the signs!

# print 10 lines:
seq 10

1
2
3
4
5
6
7
8
9
10

#-------------------------    
# get the last 3 lines:
seq 10 | tail -n 3

8
9
10

#--------------------------------------
# start at line 3 (skip first 2 lines)
seq 10 | tail -n +3

3
4
5
6
7
8
9
10

#-------------------------    
# get the first 3 lines:
seq 10 | head -n 3

1
2
3

#-------------------------    
# skip the last 3 lines:
seq 10 | head -n -3

1
2
3
4
5
6
7

btw, man tail and man head explain this behavior.

Hayseed answered 21/2, 2020 at 9:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.