Using pipes to list the first two and last two users on the system?
up vote
2
down vote
favorite
I have to use the who
command to display who's online, then use pipes to display the first and last 2 users online. the only thing I know how to do is something like:
who | head -5 | tail -2
.
That doesn't work, though.
pipe tail head who
add a comment |
up vote
2
down vote
favorite
I have to use the who
command to display who's online, then use pipes to display the first and last 2 users online. the only thing I know how to do is something like:
who | head -5 | tail -2
.
That doesn't work, though.
pipe tail head who
you can combine multiple commands inside()
or{}
separated by;
... see Command-Grouping
– Sundeep
Feb 23 '17 at 3:02
First and last users in terms of what? Just from the output?
– DarkHeart
Feb 23 '17 at 4:48
Similar: Command to display first few and last few lines of a file
– Stéphane Chazelas
Feb 23 '17 at 16:19
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I have to use the who
command to display who's online, then use pipes to display the first and last 2 users online. the only thing I know how to do is something like:
who | head -5 | tail -2
.
That doesn't work, though.
pipe tail head who
I have to use the who
command to display who's online, then use pipes to display the first and last 2 users online. the only thing I know how to do is something like:
who | head -5 | tail -2
.
That doesn't work, though.
pipe tail head who
pipe tail head who
edited Nov 24 at 20:19
Rui F Ribeiro
38.3k1475126
38.3k1475126
asked Feb 23 '17 at 2:41
user217538
111
111
you can combine multiple commands inside()
or{}
separated by;
... see Command-Grouping
– Sundeep
Feb 23 '17 at 3:02
First and last users in terms of what? Just from the output?
– DarkHeart
Feb 23 '17 at 4:48
Similar: Command to display first few and last few lines of a file
– Stéphane Chazelas
Feb 23 '17 at 16:19
add a comment |
you can combine multiple commands inside()
or{}
separated by;
... see Command-Grouping
– Sundeep
Feb 23 '17 at 3:02
First and last users in terms of what? Just from the output?
– DarkHeart
Feb 23 '17 at 4:48
Similar: Command to display first few and last few lines of a file
– Stéphane Chazelas
Feb 23 '17 at 16:19
you can combine multiple commands inside
()
or {}
separated by ;
... see Command-Grouping– Sundeep
Feb 23 '17 at 3:02
you can combine multiple commands inside
()
or {}
separated by ;
... see Command-Grouping– Sundeep
Feb 23 '17 at 3:02
First and last users in terms of what? Just from the output?
– DarkHeart
Feb 23 '17 at 4:48
First and last users in terms of what? Just from the output?
– DarkHeart
Feb 23 '17 at 4:48
Similar: Command to display first few and last few lines of a file
– Stéphane Chazelas
Feb 23 '17 at 16:19
Similar: Command to display first few and last few lines of a file
– Stéphane Chazelas
Feb 23 '17 at 16:19
add a comment |
3 Answers
3
active
oldest
votes
up vote
4
down vote
Directly:
who | head -2
who | tail -2
add a comment |
up vote
0
down vote
You can use tee
and a redirection to stderr
who | tee >(head -n2 1>&2 ) | tail -n2
Where tee
allows you to copy stdin
to both a file and stdout
. Here we actually replace the file by the head
command and redirect its output from stdout
to stderr
(via 1>&2
) as all of stout
is piped to the tail
command and processed, so we need to circumvent tail
ing the head
result, too. stderr
itself is however still printed on the terminal.
Updates following discussion in comments
As pointed out by Stéphane, head
might send a SIGPIPE signal kill the pipe prematurely so that tail
might not see the actual end of the input stream.
To prevent this, one could ignore the SIGPIPE signal as described here, by using a trap
with an empty command. Tested in bash
only .....
who | { trap "" PIPE ; tee >( head -n 2 3>&1 >&2 ) ; } | tail -n 2
Additionally mentioned in the comments: non-bash
shells might tumble the ordering of the results - see the proposed solution by Stéphane for this.
1
who -l | ( tee >(head -n2 1>&3 ) | tail -n2 ) 3>&1
will ensure bothhead
andtail
write to stdout.
– roaima
Feb 23 '17 at 14:45
Depending on timing and on the number of logged-in users,tee
may get a SIGPIPE whenhead
terminates so thattee
would not be able to write the full output totail
. Also, ashead
andtail
run in parallel, the order of their output is not guaranteed.
– Stéphane Chazelas
Feb 23 '17 at 16:28
@StéphaneChazelas I can confirm the SIGPIPE problem simulating with a large enoughseq
asstdin
- order is never tumbled, buttail
does not show the last two number of theseq
uence. However I do not understand properly what is happening. Could you elaborate?
– Fiximan
Feb 23 '17 at 16:43
1
Ifhead
finishes beforetee
has written all the data to the pipe tohead
, then the next timetee
write to that pipe it will get a SIGPIPE and die.tail
will output the last two lines it has received before that.
– Stéphane Chazelas
Feb 23 '17 at 17:37
1
Withbash
, the order will be preserved becausebash
(at least current versions) process substitution leaves abash
process around with its stdout still connected to the pipe totail
waiting for thehead
command, sotail
won't see eof until that process has terminated so not until head has terminated. Other shells likeksh93
orzsh
don't have that limitation, so you may see the output tumbled there. One way to guarantee the order with any shell would be withtee >(head -n 2 3>&1 >&2) | tail -n 2
. Here that fd 3 holds the pipe to tail open.
– Stéphane Chazelas
Feb 23 '17 at 18:00
|
show 2 more comments
up vote
-1
down vote
It goes wrong for you when you are trying to use tail
command. You are not actually using it to extract what you want from who
command's output but from head
command.
If you need to use head
and tail
combined with piping, you could do it like this for example:
(who |head -n5 && who |tail -n2)
Modify numbers to meet your needs
1
This won't work becausehead
will eat up all of stdin, leaving nothing fortail
.
– roaima
Feb 23 '17 at 14:42
@roaima I see where it went wrong now. But there's one weird thing I don't uderstand at all. I tried to run following commandls -l |(head -n5 && tail -n2)
. I tested it 10 times now, 6 times I got the result I wanted and 4 times I didn't. Why is that?
– jiipeezz
Feb 23 '17 at 15:56
1
@jiipeezz, that very much depends on timing and the number of files in the directory. It depends whether the firstread()
done byhead
reads the whole output ofls -l
or not.
– Stéphane Chazelas
Feb 23 '17 at 18:04
@StéphaneChazelas thanks for elaborating. Now it makes more sense. Corrected the answer to something that works.
– jiipeezz
Feb 23 '17 at 20:38
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
Directly:
who | head -2
who | tail -2
add a comment |
up vote
4
down vote
Directly:
who | head -2
who | tail -2
add a comment |
up vote
4
down vote
up vote
4
down vote
Directly:
who | head -2
who | tail -2
Directly:
who | head -2
who | tail -2
answered Feb 23 '17 at 4:21
Jeff Schaller
36.6k1052121
36.6k1052121
add a comment |
add a comment |
up vote
0
down vote
You can use tee
and a redirection to stderr
who | tee >(head -n2 1>&2 ) | tail -n2
Where tee
allows you to copy stdin
to both a file and stdout
. Here we actually replace the file by the head
command and redirect its output from stdout
to stderr
(via 1>&2
) as all of stout
is piped to the tail
command and processed, so we need to circumvent tail
ing the head
result, too. stderr
itself is however still printed on the terminal.
Updates following discussion in comments
As pointed out by Stéphane, head
might send a SIGPIPE signal kill the pipe prematurely so that tail
might not see the actual end of the input stream.
To prevent this, one could ignore the SIGPIPE signal as described here, by using a trap
with an empty command. Tested in bash
only .....
who | { trap "" PIPE ; tee >( head -n 2 3>&1 >&2 ) ; } | tail -n 2
Additionally mentioned in the comments: non-bash
shells might tumble the ordering of the results - see the proposed solution by Stéphane for this.
1
who -l | ( tee >(head -n2 1>&3 ) | tail -n2 ) 3>&1
will ensure bothhead
andtail
write to stdout.
– roaima
Feb 23 '17 at 14:45
Depending on timing and on the number of logged-in users,tee
may get a SIGPIPE whenhead
terminates so thattee
would not be able to write the full output totail
. Also, ashead
andtail
run in parallel, the order of their output is not guaranteed.
– Stéphane Chazelas
Feb 23 '17 at 16:28
@StéphaneChazelas I can confirm the SIGPIPE problem simulating with a large enoughseq
asstdin
- order is never tumbled, buttail
does not show the last two number of theseq
uence. However I do not understand properly what is happening. Could you elaborate?
– Fiximan
Feb 23 '17 at 16:43
1
Ifhead
finishes beforetee
has written all the data to the pipe tohead
, then the next timetee
write to that pipe it will get a SIGPIPE and die.tail
will output the last two lines it has received before that.
– Stéphane Chazelas
Feb 23 '17 at 17:37
1
Withbash
, the order will be preserved becausebash
(at least current versions) process substitution leaves abash
process around with its stdout still connected to the pipe totail
waiting for thehead
command, sotail
won't see eof until that process has terminated so not until head has terminated. Other shells likeksh93
orzsh
don't have that limitation, so you may see the output tumbled there. One way to guarantee the order with any shell would be withtee >(head -n 2 3>&1 >&2) | tail -n 2
. Here that fd 3 holds the pipe to tail open.
– Stéphane Chazelas
Feb 23 '17 at 18:00
|
show 2 more comments
up vote
0
down vote
You can use tee
and a redirection to stderr
who | tee >(head -n2 1>&2 ) | tail -n2
Where tee
allows you to copy stdin
to both a file and stdout
. Here we actually replace the file by the head
command and redirect its output from stdout
to stderr
(via 1>&2
) as all of stout
is piped to the tail
command and processed, so we need to circumvent tail
ing the head
result, too. stderr
itself is however still printed on the terminal.
Updates following discussion in comments
As pointed out by Stéphane, head
might send a SIGPIPE signal kill the pipe prematurely so that tail
might not see the actual end of the input stream.
To prevent this, one could ignore the SIGPIPE signal as described here, by using a trap
with an empty command. Tested in bash
only .....
who | { trap "" PIPE ; tee >( head -n 2 3>&1 >&2 ) ; } | tail -n 2
Additionally mentioned in the comments: non-bash
shells might tumble the ordering of the results - see the proposed solution by Stéphane for this.
1
who -l | ( tee >(head -n2 1>&3 ) | tail -n2 ) 3>&1
will ensure bothhead
andtail
write to stdout.
– roaima
Feb 23 '17 at 14:45
Depending on timing and on the number of logged-in users,tee
may get a SIGPIPE whenhead
terminates so thattee
would not be able to write the full output totail
. Also, ashead
andtail
run in parallel, the order of their output is not guaranteed.
– Stéphane Chazelas
Feb 23 '17 at 16:28
@StéphaneChazelas I can confirm the SIGPIPE problem simulating with a large enoughseq
asstdin
- order is never tumbled, buttail
does not show the last two number of theseq
uence. However I do not understand properly what is happening. Could you elaborate?
– Fiximan
Feb 23 '17 at 16:43
1
Ifhead
finishes beforetee
has written all the data to the pipe tohead
, then the next timetee
write to that pipe it will get a SIGPIPE and die.tail
will output the last two lines it has received before that.
– Stéphane Chazelas
Feb 23 '17 at 17:37
1
Withbash
, the order will be preserved becausebash
(at least current versions) process substitution leaves abash
process around with its stdout still connected to the pipe totail
waiting for thehead
command, sotail
won't see eof until that process has terminated so not until head has terminated. Other shells likeksh93
orzsh
don't have that limitation, so you may see the output tumbled there. One way to guarantee the order with any shell would be withtee >(head -n 2 3>&1 >&2) | tail -n 2
. Here that fd 3 holds the pipe to tail open.
– Stéphane Chazelas
Feb 23 '17 at 18:00
|
show 2 more comments
up vote
0
down vote
up vote
0
down vote
You can use tee
and a redirection to stderr
who | tee >(head -n2 1>&2 ) | tail -n2
Where tee
allows you to copy stdin
to both a file and stdout
. Here we actually replace the file by the head
command and redirect its output from stdout
to stderr
(via 1>&2
) as all of stout
is piped to the tail
command and processed, so we need to circumvent tail
ing the head
result, too. stderr
itself is however still printed on the terminal.
Updates following discussion in comments
As pointed out by Stéphane, head
might send a SIGPIPE signal kill the pipe prematurely so that tail
might not see the actual end of the input stream.
To prevent this, one could ignore the SIGPIPE signal as described here, by using a trap
with an empty command. Tested in bash
only .....
who | { trap "" PIPE ; tee >( head -n 2 3>&1 >&2 ) ; } | tail -n 2
Additionally mentioned in the comments: non-bash
shells might tumble the ordering of the results - see the proposed solution by Stéphane for this.
You can use tee
and a redirection to stderr
who | tee >(head -n2 1>&2 ) | tail -n2
Where tee
allows you to copy stdin
to both a file and stdout
. Here we actually replace the file by the head
command and redirect its output from stdout
to stderr
(via 1>&2
) as all of stout
is piped to the tail
command and processed, so we need to circumvent tail
ing the head
result, too. stderr
itself is however still printed on the terminal.
Updates following discussion in comments
As pointed out by Stéphane, head
might send a SIGPIPE signal kill the pipe prematurely so that tail
might not see the actual end of the input stream.
To prevent this, one could ignore the SIGPIPE signal as described here, by using a trap
with an empty command. Tested in bash
only .....
who | { trap "" PIPE ; tee >( head -n 2 3>&1 >&2 ) ; } | tail -n 2
Additionally mentioned in the comments: non-bash
shells might tumble the ordering of the results - see the proposed solution by Stéphane for this.
edited Apr 13 '17 at 12:36
Community♦
1
1
answered Feb 23 '17 at 14:19
Fiximan
3,143524
3,143524
1
who -l | ( tee >(head -n2 1>&3 ) | tail -n2 ) 3>&1
will ensure bothhead
andtail
write to stdout.
– roaima
Feb 23 '17 at 14:45
Depending on timing and on the number of logged-in users,tee
may get a SIGPIPE whenhead
terminates so thattee
would not be able to write the full output totail
. Also, ashead
andtail
run in parallel, the order of their output is not guaranteed.
– Stéphane Chazelas
Feb 23 '17 at 16:28
@StéphaneChazelas I can confirm the SIGPIPE problem simulating with a large enoughseq
asstdin
- order is never tumbled, buttail
does not show the last two number of theseq
uence. However I do not understand properly what is happening. Could you elaborate?
– Fiximan
Feb 23 '17 at 16:43
1
Ifhead
finishes beforetee
has written all the data to the pipe tohead
, then the next timetee
write to that pipe it will get a SIGPIPE and die.tail
will output the last two lines it has received before that.
– Stéphane Chazelas
Feb 23 '17 at 17:37
1
Withbash
, the order will be preserved becausebash
(at least current versions) process substitution leaves abash
process around with its stdout still connected to the pipe totail
waiting for thehead
command, sotail
won't see eof until that process has terminated so not until head has terminated. Other shells likeksh93
orzsh
don't have that limitation, so you may see the output tumbled there. One way to guarantee the order with any shell would be withtee >(head -n 2 3>&1 >&2) | tail -n 2
. Here that fd 3 holds the pipe to tail open.
– Stéphane Chazelas
Feb 23 '17 at 18:00
|
show 2 more comments
1
who -l | ( tee >(head -n2 1>&3 ) | tail -n2 ) 3>&1
will ensure bothhead
andtail
write to stdout.
– roaima
Feb 23 '17 at 14:45
Depending on timing and on the number of logged-in users,tee
may get a SIGPIPE whenhead
terminates so thattee
would not be able to write the full output totail
. Also, ashead
andtail
run in parallel, the order of their output is not guaranteed.
– Stéphane Chazelas
Feb 23 '17 at 16:28
@StéphaneChazelas I can confirm the SIGPIPE problem simulating with a large enoughseq
asstdin
- order is never tumbled, buttail
does not show the last two number of theseq
uence. However I do not understand properly what is happening. Could you elaborate?
– Fiximan
Feb 23 '17 at 16:43
1
Ifhead
finishes beforetee
has written all the data to the pipe tohead
, then the next timetee
write to that pipe it will get a SIGPIPE and die.tail
will output the last two lines it has received before that.
– Stéphane Chazelas
Feb 23 '17 at 17:37
1
Withbash
, the order will be preserved becausebash
(at least current versions) process substitution leaves abash
process around with its stdout still connected to the pipe totail
waiting for thehead
command, sotail
won't see eof until that process has terminated so not until head has terminated. Other shells likeksh93
orzsh
don't have that limitation, so you may see the output tumbled there. One way to guarantee the order with any shell would be withtee >(head -n 2 3>&1 >&2) | tail -n 2
. Here that fd 3 holds the pipe to tail open.
– Stéphane Chazelas
Feb 23 '17 at 18:00
1
1
who -l | ( tee >(head -n2 1>&3 ) | tail -n2 ) 3>&1
will ensure both head
and tail
write to stdout.– roaima
Feb 23 '17 at 14:45
who -l | ( tee >(head -n2 1>&3 ) | tail -n2 ) 3>&1
will ensure both head
and tail
write to stdout.– roaima
Feb 23 '17 at 14:45
Depending on timing and on the number of logged-in users,
tee
may get a SIGPIPE when head
terminates so that tee
would not be able to write the full output to tail
. Also, as head
and tail
run in parallel, the order of their output is not guaranteed.– Stéphane Chazelas
Feb 23 '17 at 16:28
Depending on timing and on the number of logged-in users,
tee
may get a SIGPIPE when head
terminates so that tee
would not be able to write the full output to tail
. Also, as head
and tail
run in parallel, the order of their output is not guaranteed.– Stéphane Chazelas
Feb 23 '17 at 16:28
@StéphaneChazelas I can confirm the SIGPIPE problem simulating with a large enough
seq
as stdin
- order is never tumbled, but tail
does not show the last two number of the seq
uence. However I do not understand properly what is happening. Could you elaborate?– Fiximan
Feb 23 '17 at 16:43
@StéphaneChazelas I can confirm the SIGPIPE problem simulating with a large enough
seq
as stdin
- order is never tumbled, but tail
does not show the last two number of the seq
uence. However I do not understand properly what is happening. Could you elaborate?– Fiximan
Feb 23 '17 at 16:43
1
1
If
head
finishes before tee
has written all the data to the pipe to head
, then the next time tee
write to that pipe it will get a SIGPIPE and die. tail
will output the last two lines it has received before that.– Stéphane Chazelas
Feb 23 '17 at 17:37
If
head
finishes before tee
has written all the data to the pipe to head
, then the next time tee
write to that pipe it will get a SIGPIPE and die. tail
will output the last two lines it has received before that.– Stéphane Chazelas
Feb 23 '17 at 17:37
1
1
With
bash
, the order will be preserved because bash
(at least current versions) process substitution leaves a bash
process around with its stdout still connected to the pipe to tail
waiting for the head
command, so tail
won't see eof until that process has terminated so not until head has terminated. Other shells like ksh93
or zsh
don't have that limitation, so you may see the output tumbled there. One way to guarantee the order with any shell would be with tee >(head -n 2 3>&1 >&2) | tail -n 2
. Here that fd 3 holds the pipe to tail open.– Stéphane Chazelas
Feb 23 '17 at 18:00
With
bash
, the order will be preserved because bash
(at least current versions) process substitution leaves a bash
process around with its stdout still connected to the pipe to tail
waiting for the head
command, so tail
won't see eof until that process has terminated so not until head has terminated. Other shells like ksh93
or zsh
don't have that limitation, so you may see the output tumbled there. One way to guarantee the order with any shell would be with tee >(head -n 2 3>&1 >&2) | tail -n 2
. Here that fd 3 holds the pipe to tail open.– Stéphane Chazelas
Feb 23 '17 at 18:00
|
show 2 more comments
up vote
-1
down vote
It goes wrong for you when you are trying to use tail
command. You are not actually using it to extract what you want from who
command's output but from head
command.
If you need to use head
and tail
combined with piping, you could do it like this for example:
(who |head -n5 && who |tail -n2)
Modify numbers to meet your needs
1
This won't work becausehead
will eat up all of stdin, leaving nothing fortail
.
– roaima
Feb 23 '17 at 14:42
@roaima I see where it went wrong now. But there's one weird thing I don't uderstand at all. I tried to run following commandls -l |(head -n5 && tail -n2)
. I tested it 10 times now, 6 times I got the result I wanted and 4 times I didn't. Why is that?
– jiipeezz
Feb 23 '17 at 15:56
1
@jiipeezz, that very much depends on timing and the number of files in the directory. It depends whether the firstread()
done byhead
reads the whole output ofls -l
or not.
– Stéphane Chazelas
Feb 23 '17 at 18:04
@StéphaneChazelas thanks for elaborating. Now it makes more sense. Corrected the answer to something that works.
– jiipeezz
Feb 23 '17 at 20:38
add a comment |
up vote
-1
down vote
It goes wrong for you when you are trying to use tail
command. You are not actually using it to extract what you want from who
command's output but from head
command.
If you need to use head
and tail
combined with piping, you could do it like this for example:
(who |head -n5 && who |tail -n2)
Modify numbers to meet your needs
1
This won't work becausehead
will eat up all of stdin, leaving nothing fortail
.
– roaima
Feb 23 '17 at 14:42
@roaima I see where it went wrong now. But there's one weird thing I don't uderstand at all. I tried to run following commandls -l |(head -n5 && tail -n2)
. I tested it 10 times now, 6 times I got the result I wanted and 4 times I didn't. Why is that?
– jiipeezz
Feb 23 '17 at 15:56
1
@jiipeezz, that very much depends on timing and the number of files in the directory. It depends whether the firstread()
done byhead
reads the whole output ofls -l
or not.
– Stéphane Chazelas
Feb 23 '17 at 18:04
@StéphaneChazelas thanks for elaborating. Now it makes more sense. Corrected the answer to something that works.
– jiipeezz
Feb 23 '17 at 20:38
add a comment |
up vote
-1
down vote
up vote
-1
down vote
It goes wrong for you when you are trying to use tail
command. You are not actually using it to extract what you want from who
command's output but from head
command.
If you need to use head
and tail
combined with piping, you could do it like this for example:
(who |head -n5 && who |tail -n2)
Modify numbers to meet your needs
It goes wrong for you when you are trying to use tail
command. You are not actually using it to extract what you want from who
command's output but from head
command.
If you need to use head
and tail
combined with piping, you could do it like this for example:
(who |head -n5 && who |tail -n2)
Modify numbers to meet your needs
edited Feb 23 '17 at 16:02
answered Feb 23 '17 at 3:05
jiipeezz
21125
21125
1
This won't work becausehead
will eat up all of stdin, leaving nothing fortail
.
– roaima
Feb 23 '17 at 14:42
@roaima I see where it went wrong now. But there's one weird thing I don't uderstand at all. I tried to run following commandls -l |(head -n5 && tail -n2)
. I tested it 10 times now, 6 times I got the result I wanted and 4 times I didn't. Why is that?
– jiipeezz
Feb 23 '17 at 15:56
1
@jiipeezz, that very much depends on timing and the number of files in the directory. It depends whether the firstread()
done byhead
reads the whole output ofls -l
or not.
– Stéphane Chazelas
Feb 23 '17 at 18:04
@StéphaneChazelas thanks for elaborating. Now it makes more sense. Corrected the answer to something that works.
– jiipeezz
Feb 23 '17 at 20:38
add a comment |
1
This won't work becausehead
will eat up all of stdin, leaving nothing fortail
.
– roaima
Feb 23 '17 at 14:42
@roaima I see where it went wrong now. But there's one weird thing I don't uderstand at all. I tried to run following commandls -l |(head -n5 && tail -n2)
. I tested it 10 times now, 6 times I got the result I wanted and 4 times I didn't. Why is that?
– jiipeezz
Feb 23 '17 at 15:56
1
@jiipeezz, that very much depends on timing and the number of files in the directory. It depends whether the firstread()
done byhead
reads the whole output ofls -l
or not.
– Stéphane Chazelas
Feb 23 '17 at 18:04
@StéphaneChazelas thanks for elaborating. Now it makes more sense. Corrected the answer to something that works.
– jiipeezz
Feb 23 '17 at 20:38
1
1
This won't work because
head
will eat up all of stdin, leaving nothing for tail
.– roaima
Feb 23 '17 at 14:42
This won't work because
head
will eat up all of stdin, leaving nothing for tail
.– roaima
Feb 23 '17 at 14:42
@roaima I see where it went wrong now. But there's one weird thing I don't uderstand at all. I tried to run following command
ls -l |(head -n5 && tail -n2)
. I tested it 10 times now, 6 times I got the result I wanted and 4 times I didn't. Why is that?– jiipeezz
Feb 23 '17 at 15:56
@roaima I see where it went wrong now. But there's one weird thing I don't uderstand at all. I tried to run following command
ls -l |(head -n5 && tail -n2)
. I tested it 10 times now, 6 times I got the result I wanted and 4 times I didn't. Why is that?– jiipeezz
Feb 23 '17 at 15:56
1
1
@jiipeezz, that very much depends on timing and the number of files in the directory. It depends whether the first
read()
done by head
reads the whole output of ls -l
or not.– Stéphane Chazelas
Feb 23 '17 at 18:04
@jiipeezz, that very much depends on timing and the number of files in the directory. It depends whether the first
read()
done by head
reads the whole output of ls -l
or not.– Stéphane Chazelas
Feb 23 '17 at 18:04
@StéphaneChazelas thanks for elaborating. Now it makes more sense. Corrected the answer to something that works.
– jiipeezz
Feb 23 '17 at 20:38
@StéphaneChazelas thanks for elaborating. Now it makes more sense. Corrected the answer to something that works.
– jiipeezz
Feb 23 '17 at 20:38
add a comment |
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%2f346977%2fusing-pipes-to-list-the-first-two-and-last-two-users-on-the-system%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
you can combine multiple commands inside
()
or{}
separated by;
... see Command-Grouping– Sundeep
Feb 23 '17 at 3:02
First and last users in terms of what? Just from the output?
– DarkHeart
Feb 23 '17 at 4:48
Similar: Command to display first few and last few lines of a file
– Stéphane Chazelas
Feb 23 '17 at 16:19