Negative arguments to head / tail
Clash Royale CLAN TAG#URR8PPP
up vote
7
down vote
favorite
Variants of this question have certainly been asked several times in different places, but I am trying to remove the last M
lines from a file without luck.
The second most voted answer in this question recommends doing the following to get rid of the last line in a file:
head -n -1 foo.txt > temp.txt
However, when I try that in OSX & Zsh, I get:
head: illegal line count -- -1
Why is that? How can I remove the M
last lines and the first N
lines of a given file?
shell-script osx tail options head
add a comment |
up vote
7
down vote
favorite
Variants of this question have certainly been asked several times in different places, but I am trying to remove the last M
lines from a file without luck.
The second most voted answer in this question recommends doing the following to get rid of the last line in a file:
head -n -1 foo.txt > temp.txt
However, when I try that in OSX & Zsh, I get:
head: illegal line count -- -1
Why is that? How can I remove the M
last lines and the first N
lines of a given file?
shell-script osx tail options head
1
See POSIX head and tail not feature equivalent
– Stéphane Chazelas
Nov 20 '14 at 17:19
1
What is the output ofhead --version
? What system are you using?
– jofel
Nov 20 '14 at 17:21
@jofelhead --version
returns an error actually.
– Amelio Vazquez-Reina
Nov 20 '14 at 17:33
add a comment |
up vote
7
down vote
favorite
up vote
7
down vote
favorite
Variants of this question have certainly been asked several times in different places, but I am trying to remove the last M
lines from a file without luck.
The second most voted answer in this question recommends doing the following to get rid of the last line in a file:
head -n -1 foo.txt > temp.txt
However, when I try that in OSX & Zsh, I get:
head: illegal line count -- -1
Why is that? How can I remove the M
last lines and the first N
lines of a given file?
shell-script osx tail options head
Variants of this question have certainly been asked several times in different places, but I am trying to remove the last M
lines from a file without luck.
The second most voted answer in this question recommends doing the following to get rid of the last line in a file:
head -n -1 foo.txt > temp.txt
However, when I try that in OSX & Zsh, I get:
head: illegal line count -- -1
Why is that? How can I remove the M
last lines and the first N
lines of a given file?
shell-script osx tail options head
shell-script osx tail options head
edited May 23 '17 at 12:40
Community♦
1
1
asked Nov 20 '14 at 17:17
Amelio Vazquez-Reina
12.2k52128228
12.2k52128228
1
See POSIX head and tail not feature equivalent
– Stéphane Chazelas
Nov 20 '14 at 17:19
1
What is the output ofhead --version
? What system are you using?
– jofel
Nov 20 '14 at 17:21
@jofelhead --version
returns an error actually.
– Amelio Vazquez-Reina
Nov 20 '14 at 17:33
add a comment |
1
See POSIX head and tail not feature equivalent
– Stéphane Chazelas
Nov 20 '14 at 17:19
1
What is the output ofhead --version
? What system are you using?
– jofel
Nov 20 '14 at 17:21
@jofelhead --version
returns an error actually.
– Amelio Vazquez-Reina
Nov 20 '14 at 17:33
1
1
See POSIX head and tail not feature equivalent
– Stéphane Chazelas
Nov 20 '14 at 17:19
See POSIX head and tail not feature equivalent
– Stéphane Chazelas
Nov 20 '14 at 17:19
1
1
What is the output of
head --version
? What system are you using?– jofel
Nov 20 '14 at 17:21
What is the output of
head --version
? What system are you using?– jofel
Nov 20 '14 at 17:21
@jofel
head --version
returns an error actually.– Amelio Vazquez-Reina
Nov 20 '14 at 17:33
@jofel
head --version
returns an error actually.– Amelio Vazquez-Reina
Nov 20 '14 at 17:33
add a comment |
3 Answers
3
active
oldest
votes
up vote
14
down vote
accepted
You can remove the first 12 lines with:
tail -n +13
(That means print from the 13th line.)
Some implementations of head
like GNU head
support:
head -n -12
but that's not standard.
tail -r file | tail -n +12 | tail -r
would work on those systems that have tail -r
(see also GNU tac
) but is sub-optimal.
Where n
is 1:
sed '$d' file
You can also do:
sed '$d' file | sed '$d'
to remove 2 lines, but that's not optimal.
You can do:
sed -ne :1 -e 'N;1,12b1' -e 'P;D'
But beware that won't work with large values of n with some sed
implementations.
With awk
:
awk -v n=12 'NR>nprint line[NR%n];line[NR%n]=$0'
To remove m
lines from the beginning and n
from the end:
awk -v m=6 -v n=12 'NR<=mnext;NR>n+mprint line[NR%n];line[NR%n]=$0'
sed 'x;1d;$d'
might be used to get the last two lines as well. And w/ a couple ofN
's after1d
might be used to get a little more than that, though at that point it offers no advantage over theN;P;D
loop you already recommend.
– mikeserv
Nov 21 '14 at 0:46
Thank you -- The last command is extremely helpful. A summer small caveat: it doesn't work whenm
orn
are0
. Otherwise it would be perfect.
– Amelio Vazquez-Reina
Nov 21 '14 at 21:13
add a comment |
up vote
2
down vote
You can use the following way to remove first N lines and last M lines.
With N=5
, M=7
and file test.txt
:
sed -n -e "6,$(($(wc -l < test.txt) - 7))p" test.txt
The command prints all lines from N+1 to LastLine-M.
Another option is to use python:
python -c 'import sys;print "".join(sys.stdin.readlines()[5:-7]),' < test.txt
add a comment |
up vote
1
down vote
You can remove the last M
(here M=100) lines from a file with:
head -$(($(wc -l < foo.txt) - 100)) foo.txt > temp.txt
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
14
down vote
accepted
You can remove the first 12 lines with:
tail -n +13
(That means print from the 13th line.)
Some implementations of head
like GNU head
support:
head -n -12
but that's not standard.
tail -r file | tail -n +12 | tail -r
would work on those systems that have tail -r
(see also GNU tac
) but is sub-optimal.
Where n
is 1:
sed '$d' file
You can also do:
sed '$d' file | sed '$d'
to remove 2 lines, but that's not optimal.
You can do:
sed -ne :1 -e 'N;1,12b1' -e 'P;D'
But beware that won't work with large values of n with some sed
implementations.
With awk
:
awk -v n=12 'NR>nprint line[NR%n];line[NR%n]=$0'
To remove m
lines from the beginning and n
from the end:
awk -v m=6 -v n=12 'NR<=mnext;NR>n+mprint line[NR%n];line[NR%n]=$0'
sed 'x;1d;$d'
might be used to get the last two lines as well. And w/ a couple ofN
's after1d
might be used to get a little more than that, though at that point it offers no advantage over theN;P;D
loop you already recommend.
– mikeserv
Nov 21 '14 at 0:46
Thank you -- The last command is extremely helpful. A summer small caveat: it doesn't work whenm
orn
are0
. Otherwise it would be perfect.
– Amelio Vazquez-Reina
Nov 21 '14 at 21:13
add a comment |
up vote
14
down vote
accepted
You can remove the first 12 lines with:
tail -n +13
(That means print from the 13th line.)
Some implementations of head
like GNU head
support:
head -n -12
but that's not standard.
tail -r file | tail -n +12 | tail -r
would work on those systems that have tail -r
(see also GNU tac
) but is sub-optimal.
Where n
is 1:
sed '$d' file
You can also do:
sed '$d' file | sed '$d'
to remove 2 lines, but that's not optimal.
You can do:
sed -ne :1 -e 'N;1,12b1' -e 'P;D'
But beware that won't work with large values of n with some sed
implementations.
With awk
:
awk -v n=12 'NR>nprint line[NR%n];line[NR%n]=$0'
To remove m
lines from the beginning and n
from the end:
awk -v m=6 -v n=12 'NR<=mnext;NR>n+mprint line[NR%n];line[NR%n]=$0'
sed 'x;1d;$d'
might be used to get the last two lines as well. And w/ a couple ofN
's after1d
might be used to get a little more than that, though at that point it offers no advantage over theN;P;D
loop you already recommend.
– mikeserv
Nov 21 '14 at 0:46
Thank you -- The last command is extremely helpful. A summer small caveat: it doesn't work whenm
orn
are0
. Otherwise it would be perfect.
– Amelio Vazquez-Reina
Nov 21 '14 at 21:13
add a comment |
up vote
14
down vote
accepted
up vote
14
down vote
accepted
You can remove the first 12 lines with:
tail -n +13
(That means print from the 13th line.)
Some implementations of head
like GNU head
support:
head -n -12
but that's not standard.
tail -r file | tail -n +12 | tail -r
would work on those systems that have tail -r
(see also GNU tac
) but is sub-optimal.
Where n
is 1:
sed '$d' file
You can also do:
sed '$d' file | sed '$d'
to remove 2 lines, but that's not optimal.
You can do:
sed -ne :1 -e 'N;1,12b1' -e 'P;D'
But beware that won't work with large values of n with some sed
implementations.
With awk
:
awk -v n=12 'NR>nprint line[NR%n];line[NR%n]=$0'
To remove m
lines from the beginning and n
from the end:
awk -v m=6 -v n=12 'NR<=mnext;NR>n+mprint line[NR%n];line[NR%n]=$0'
You can remove the first 12 lines with:
tail -n +13
(That means print from the 13th line.)
Some implementations of head
like GNU head
support:
head -n -12
but that's not standard.
tail -r file | tail -n +12 | tail -r
would work on those systems that have tail -r
(see also GNU tac
) but is sub-optimal.
Where n
is 1:
sed '$d' file
You can also do:
sed '$d' file | sed '$d'
to remove 2 lines, but that's not optimal.
You can do:
sed -ne :1 -e 'N;1,12b1' -e 'P;D'
But beware that won't work with large values of n with some sed
implementations.
With awk
:
awk -v n=12 'NR>nprint line[NR%n];line[NR%n]=$0'
To remove m
lines from the beginning and n
from the end:
awk -v m=6 -v n=12 'NR<=mnext;NR>n+mprint line[NR%n];line[NR%n]=$0'
edited Jul 4 '15 at 15:10
MichalH
1,2081620
1,2081620
answered Nov 20 '14 at 17:27
Stéphane Chazelas
296k54559904
296k54559904
sed 'x;1d;$d'
might be used to get the last two lines as well. And w/ a couple ofN
's after1d
might be used to get a little more than that, though at that point it offers no advantage over theN;P;D
loop you already recommend.
– mikeserv
Nov 21 '14 at 0:46
Thank you -- The last command is extremely helpful. A summer small caveat: it doesn't work whenm
orn
are0
. Otherwise it would be perfect.
– Amelio Vazquez-Reina
Nov 21 '14 at 21:13
add a comment |
sed 'x;1d;$d'
might be used to get the last two lines as well. And w/ a couple ofN
's after1d
might be used to get a little more than that, though at that point it offers no advantage over theN;P;D
loop you already recommend.
– mikeserv
Nov 21 '14 at 0:46
Thank you -- The last command is extremely helpful. A summer small caveat: it doesn't work whenm
orn
are0
. Otherwise it would be perfect.
– Amelio Vazquez-Reina
Nov 21 '14 at 21:13
sed 'x;1d;$d'
might be used to get the last two lines as well. And w/ a couple of N
's after 1d
might be used to get a little more than that, though at that point it offers no advantage over the N;P;D
loop you already recommend.– mikeserv
Nov 21 '14 at 0:46
sed 'x;1d;$d'
might be used to get the last two lines as well. And w/ a couple of N
's after 1d
might be used to get a little more than that, though at that point it offers no advantage over the N;P;D
loop you already recommend.– mikeserv
Nov 21 '14 at 0:46
Thank you -- The last command is extremely helpful. A summer small caveat: it doesn't work when
m
or n
are 0
. Otherwise it would be perfect.– Amelio Vazquez-Reina
Nov 21 '14 at 21:13
Thank you -- The last command is extremely helpful. A summer small caveat: it doesn't work when
m
or n
are 0
. Otherwise it would be perfect.– Amelio Vazquez-Reina
Nov 21 '14 at 21:13
add a comment |
up vote
2
down vote
You can use the following way to remove first N lines and last M lines.
With N=5
, M=7
and file test.txt
:
sed -n -e "6,$(($(wc -l < test.txt) - 7))p" test.txt
The command prints all lines from N+1 to LastLine-M.
Another option is to use python:
python -c 'import sys;print "".join(sys.stdin.readlines()[5:-7]),' < test.txt
add a comment |
up vote
2
down vote
You can use the following way to remove first N lines and last M lines.
With N=5
, M=7
and file test.txt
:
sed -n -e "6,$(($(wc -l < test.txt) - 7))p" test.txt
The command prints all lines from N+1 to LastLine-M.
Another option is to use python:
python -c 'import sys;print "".join(sys.stdin.readlines()[5:-7]),' < test.txt
add a comment |
up vote
2
down vote
up vote
2
down vote
You can use the following way to remove first N lines and last M lines.
With N=5
, M=7
and file test.txt
:
sed -n -e "6,$(($(wc -l < test.txt) - 7))p" test.txt
The command prints all lines from N+1 to LastLine-M.
Another option is to use python:
python -c 'import sys;print "".join(sys.stdin.readlines()[5:-7]),' < test.txt
You can use the following way to remove first N lines and last M lines.
With N=5
, M=7
and file test.txt
:
sed -n -e "6,$(($(wc -l < test.txt) - 7))p" test.txt
The command prints all lines from N+1 to LastLine-M.
Another option is to use python:
python -c 'import sys;print "".join(sys.stdin.readlines()[5:-7]),' < test.txt
edited Nov 20 '14 at 17:52
answered Nov 20 '14 at 17:45
jofel
20k34780
20k34780
add a comment |
add a comment |
up vote
1
down vote
You can remove the last M
(here M=100) lines from a file with:
head -$(($(wc -l < foo.txt) - 100)) foo.txt > temp.txt
add a comment |
up vote
1
down vote
You can remove the last M
(here M=100) lines from a file with:
head -$(($(wc -l < foo.txt) - 100)) foo.txt > temp.txt
add a comment |
up vote
1
down vote
up vote
1
down vote
You can remove the last M
(here M=100) lines from a file with:
head -$(($(wc -l < foo.txt) - 100)) foo.txt > temp.txt
You can remove the last M
(here M=100) lines from a file with:
head -$(($(wc -l < foo.txt) - 100)) foo.txt > temp.txt
answered Nov 29 at 8:15
make
111
111
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f169079%2fnegative-arguments-to-head-tail%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
See POSIX head and tail not feature equivalent
– Stéphane Chazelas
Nov 20 '14 at 17:19
1
What is the output of
head --version
? What system are you using?– jofel
Nov 20 '14 at 17:21
@jofel
head --version
returns an error actually.– Amelio Vazquez-Reina
Nov 20 '14 at 17:33