Bash method for viewing beginning and end of file Unicorn Meta Zoo #1: Why another podcast? Announcing the arrival of Valued Associate #679: Cesar Manara 2019 Community Moderator Election Results Why I closed the “Why is Kali so hard” questionCommand to display first few and last few lines of a fileRace Condition for Shell Blocks in Bash?Add character to beginning and end of columnHow to transform two delimited ASCII filesusing awk to create a LaTex table from my data in a text fileAppend columns in a text file to after the final rowRemove spaces and headers from a dumped database tableCount the maximum character length for all the data fields in a simplified csv file and output to txtWriting a program for editing .txt data - Python or Unix?Merging two Unix filesegrep regular expression - same word in the beginning and endhow to evaluate number between a range of numbers using different variables in bash
Multiple fireplaces in an apartment building?
Can I criticise the more senior developers around me for not writing clean code?
Can you stand up from being prone using Skirmisher outside of your turn?
Was Dennis Ritchie being too modest in this quote about C and Pascal?
Why doesn't the standard consider a template constructor as a copy constructor?
Drawing a german abacus as in the books of Adam Ries
How do I proof this combinatorial identity
Is it possible to cast 2x Final Payment while sacrificing just one creature?
What is purpose of DB Browser(dbbrowser.aspx) under admin tool?
Mistake in years of experience in resume?
First instead of 1 when referencing
Does Feeblemind produce an ongoing magical effect that can be dispelled?
What is this word supposed to be?
Is Diceware more secure than a long passphrase?
Older movie/show about humans on derelict alien warship which refuels by passing through a star
How much of a wave function must reside inside event horizon for it to be consumed by the black hole?
How to keep bees out of canned beverages?
Would reducing the reference voltage of an ADC have any effect on accuracy?
Suing a Police Officer Instead of the Police Department
Multiple options vs single option UI
A faster way to compute the largest prime factor
What is the term for a person whose job is to place products on shelves in stores?
Does Mathematica have an implementation of the Poisson binomial distribution?
Retract an already submitted recommendation letter (written for an undergrad student)
Bash method for viewing beginning and end of file
Unicorn Meta Zoo #1: Why another podcast?
Announcing the arrival of Valued Associate #679: Cesar Manara
2019 Community Moderator Election Results
Why I closed the “Why is Kali so hard” questionCommand to display first few and last few lines of a fileRace Condition for Shell Blocks in Bash?Add character to beginning and end of columnHow to transform two delimited ASCII filesusing awk to create a LaTex table from my data in a text fileAppend columns in a text file to after the final rowRemove spaces and headers from a dumped database tableCount the maximum character length for all the data fields in a simplified csv file and output to txtWriting a program for editing .txt data - Python or Unix?Merging two Unix filesegrep regular expression - same word in the beginning and endhow to evaluate number between a range of numbers using different variables in bash
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
On queue-based clusters the Queue of pending jobs is shown from a command, say showqueue
.
The command returns, in columns, a list of reasonable data like names, etc, but the columns/data don't really matter for the question.
I like using the utility watch
like watch showqueue
at times (with an alias of alias watch="watch "
to force alias expansion of my command to watch). There is valuable data (Running jobs), in the first few lines, then pending jobs, etc, and some valuable summaries at the end.
However, at times the output of showqueue goes off the screen (Unbelievable, I know)! Ideally, I'd like some way to be able to see the beginning and end of the file at the same time.
The best I have so far is: showqueue > file; head -n 20 file > file2; echo "..." >> file2 ; tail -n 20 file >> file2; cat file2
, and using watch
on an alias of that.
Does anyone know of anything a little more flexible or single-utility? My solution gets a little nastier with bash loops to make the "..." break multilined, it's not adaptive to resizing the terminal window at all, and I'm sure there's more that I missed.
Any suggestions?
bash text-processing
add a comment |
On queue-based clusters the Queue of pending jobs is shown from a command, say showqueue
.
The command returns, in columns, a list of reasonable data like names, etc, but the columns/data don't really matter for the question.
I like using the utility watch
like watch showqueue
at times (with an alias of alias watch="watch "
to force alias expansion of my command to watch). There is valuable data (Running jobs), in the first few lines, then pending jobs, etc, and some valuable summaries at the end.
However, at times the output of showqueue goes off the screen (Unbelievable, I know)! Ideally, I'd like some way to be able to see the beginning and end of the file at the same time.
The best I have so far is: showqueue > file; head -n 20 file > file2; echo "..." >> file2 ; tail -n 20 file >> file2; cat file2
, and using watch
on an alias of that.
Does anyone know of anything a little more flexible or single-utility? My solution gets a little nastier with bash loops to make the "..." break multilined, it's not adaptive to resizing the terminal window at all, and I'm sure there's more that I missed.
Any suggestions?
bash text-processing
Closed as duplicate, but the question only shares similarities (Mine is more about a command, I learned, and not files), and the answers here didn't show up there, but if you insist
– MadisonCooper
Mar 26 at 19:59
2
Fair enough. I've reopened. Note that the answer that you have accepted will only work for large outputs as noted at my answer at Command to display first few and last few lines of a file (tryseq 30 | (head && tail)
for instance).
– Stéphane Chazelas
Mar 26 at 20:16
2
I meant that answer. That Q&A has two answers of mine because another Q&A was merged with it (see question history)
– Stéphane Chazelas
Mar 26 at 20:24
add a comment |
On queue-based clusters the Queue of pending jobs is shown from a command, say showqueue
.
The command returns, in columns, a list of reasonable data like names, etc, but the columns/data don't really matter for the question.
I like using the utility watch
like watch showqueue
at times (with an alias of alias watch="watch "
to force alias expansion of my command to watch). There is valuable data (Running jobs), in the first few lines, then pending jobs, etc, and some valuable summaries at the end.
However, at times the output of showqueue goes off the screen (Unbelievable, I know)! Ideally, I'd like some way to be able to see the beginning and end of the file at the same time.
The best I have so far is: showqueue > file; head -n 20 file > file2; echo "..." >> file2 ; tail -n 20 file >> file2; cat file2
, and using watch
on an alias of that.
Does anyone know of anything a little more flexible or single-utility? My solution gets a little nastier with bash loops to make the "..." break multilined, it's not adaptive to resizing the terminal window at all, and I'm sure there's more that I missed.
Any suggestions?
bash text-processing
On queue-based clusters the Queue of pending jobs is shown from a command, say showqueue
.
The command returns, in columns, a list of reasonable data like names, etc, but the columns/data don't really matter for the question.
I like using the utility watch
like watch showqueue
at times (with an alias of alias watch="watch "
to force alias expansion of my command to watch). There is valuable data (Running jobs), in the first few lines, then pending jobs, etc, and some valuable summaries at the end.
However, at times the output of showqueue goes off the screen (Unbelievable, I know)! Ideally, I'd like some way to be able to see the beginning and end of the file at the same time.
The best I have so far is: showqueue > file; head -n 20 file > file2; echo "..." >> file2 ; tail -n 20 file >> file2; cat file2
, and using watch
on an alias of that.
Does anyone know of anything a little more flexible or single-utility? My solution gets a little nastier with bash loops to make the "..." break multilined, it's not adaptive to resizing the terminal window at all, and I'm sure there's more that I missed.
Any suggestions?
bash text-processing
bash text-processing
edited Mar 26 at 19:11
Rui F Ribeiro
42.3k1485143
42.3k1485143
asked Mar 26 at 19:05
MadisonCooperMadisonCooper
464
464
Closed as duplicate, but the question only shares similarities (Mine is more about a command, I learned, and not files), and the answers here didn't show up there, but if you insist
– MadisonCooper
Mar 26 at 19:59
2
Fair enough. I've reopened. Note that the answer that you have accepted will only work for large outputs as noted at my answer at Command to display first few and last few lines of a file (tryseq 30 | (head && tail)
for instance).
– Stéphane Chazelas
Mar 26 at 20:16
2
I meant that answer. That Q&A has two answers of mine because another Q&A was merged with it (see question history)
– Stéphane Chazelas
Mar 26 at 20:24
add a comment |
Closed as duplicate, but the question only shares similarities (Mine is more about a command, I learned, and not files), and the answers here didn't show up there, but if you insist
– MadisonCooper
Mar 26 at 19:59
2
Fair enough. I've reopened. Note that the answer that you have accepted will only work for large outputs as noted at my answer at Command to display first few and last few lines of a file (tryseq 30 | (head && tail)
for instance).
– Stéphane Chazelas
Mar 26 at 20:16
2
I meant that answer. That Q&A has two answers of mine because another Q&A was merged with it (see question history)
– Stéphane Chazelas
Mar 26 at 20:24
Closed as duplicate, but the question only shares similarities (Mine is more about a command, I learned, and not files), and the answers here didn't show up there, but if you insist
– MadisonCooper
Mar 26 at 19:59
Closed as duplicate, but the question only shares similarities (Mine is more about a command, I learned, and not files), and the answers here didn't show up there, but if you insist
– MadisonCooper
Mar 26 at 19:59
2
2
Fair enough. I've reopened. Note that the answer that you have accepted will only work for large outputs as noted at my answer at Command to display first few and last few lines of a file (try
seq 30 | (head && tail)
for instance).– Stéphane Chazelas
Mar 26 at 20:16
Fair enough. I've reopened. Note that the answer that you have accepted will only work for large outputs as noted at my answer at Command to display first few and last few lines of a file (try
seq 30 | (head && tail)
for instance).– Stéphane Chazelas
Mar 26 at 20:16
2
2
I meant that answer. That Q&A has two answers of mine because another Q&A was merged with it (see question history)
– Stéphane Chazelas
Mar 26 at 20:24
I meant that answer. That Q&A has two answers of mine because another Q&A was merged with it (see question history)
– Stéphane Chazelas
Mar 26 at 20:24
add a comment |
4 Answers
4
active
oldest
votes
Are you looking to do something like the following? Shows output from both head and tail.
$ showqueue | (head && tail)
6
Some implementations ofhead
may read more than 10 lines and leave nothing fortail
to read. The utility is not supposed to do that and I believe that the GNU tools are well behave in this respect. Usehead; echo '...'; tail
to get the dots in there as well.
– Kusalananda♦
Mar 26 at 19:18
Yeah, combined with @Kusalananda's comment it's much more succinct than my solution. I'll mark as correct for now! Thanks.
– MadisonCooper
Mar 26 at 19:22
4
@Kusalananda, nope, on my system, GNU head (coreutils 8.26) reads blocks of 8192 bytes. It does try to seek back, but obviously can't in case of a pipe. Though anyway, is there really a requirement forhead
to not over-read? (same on Mac, it seems to read by blocks)
– ilkkachu
Mar 26 at 19:22
2
@ilkkachu yeah, even as it stands the tail part is occasionally blank even on sufficiently long output (I know since I'm using head -n 35, and that's full), but fortunately it's just a convenience to watch and I can wait 3 seconds for a refresh if needed.
– MadisonCooper
Mar 26 at 19:27
3
@Kusalananda, POSIX allows implementations to do that when the input is not seekable and most implementations do. Not doing it would mean reading one byte at a time which would be very inefficient (or put the data back onto the pipe where supported which would cause all sorts of different problems). The onlyhead
implementation I know that reads one byte at a time to avoid reading past the 10th newline is ksh93'shead
builtin.
– Stéphane Chazelas
Mar 26 at 20:04
|
show 3 more comments
Using the same approach as you, using a temporary file, but doing it slightly shorter:
showqueue >/tmp/q.out; head -n 20 /tmp/q.out; echo '...'; tail -n 20 /tmp/q.out
This would not suffer from the same issues as discussed under another answer, but would possibly show the same lines twice if the output was shorter than 40 lines.
+1 I was thinking of the same solution with the slight variation of usingtee
to let the output go straight intohead
and only using the saved output fortail
.
– Joe
Apr 9 at 10:31
add a comment |
awk solution for an arbitrary number of lines shown from the head and the tail (change n=3
to set the amount):
$ seq 99999 | awk -v n=3 'NR <= n; NR > n a[NR] = $0; delete a[NR-n];
END print "..."; for (i = NR-n+1; i <= NR; i++) if (i in a) print a[i]; '
1
2
3
...
99997
99998
99999
As it's written, the head and tail parts will not overlap, even if the input is shorter than 2*n
lines.
In some awk implementations, using for (x in a) print a[x];
in the END
part also works. But in general, it's not guaranteed to return the array entries in the correct order, and doesn't in e.g. mawk.
Ifm
is the number of lines in the file, andm < 2n
, that will print2n - m
empty lines. Also have look here ;-).for (x in a)
will iterate in order only in GNU awk; if you think that it works inmawk
too, test with > 10 values.
– mosvy
Mar 26 at 20:31
@mosvy, oh, whoops, I forgot to re-check that after realizingx in a
wasn't right. Fixed now, thanks. There's also another awk answer in the question Stéphane linked to. I like the modulo trick, though!
– ilkkachu
Mar 26 at 20:42
add a comment |
You can use simple awk script. Added circle buffer there n=3 is for last 3 lines
awk 'NR<=n print $0 A[NR%n]=$0 END for (i=1; i<=n; ++i) if (NR+i>2*n)print A[(NR+i)%n] ' n=3 < <( seq 10 )
After update its not simple anymore though,
1
This would only show the first and last line. To show more than that, you would have to changeNR==1
toNR <= 10
(or something), and then collect lines into a (circular?) buffer that you later output in theEND
block.
– Kusalananda♦
Mar 26 at 19:32
yes let me update
– Abdurrahim
Mar 26 at 19:43
This will print some lines twice when then the number of lines is less than 2 * n.
– mosvy
Mar 26 at 20:39
:) ok adding that as well now I am curious what you will find next :D
– Abdurrahim
Mar 27 at 7:22
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
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%2f508818%2fbash-method-for-viewing-beginning-and-end-of-file%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
Are you looking to do something like the following? Shows output from both head and tail.
$ showqueue | (head && tail)
6
Some implementations ofhead
may read more than 10 lines and leave nothing fortail
to read. The utility is not supposed to do that and I believe that the GNU tools are well behave in this respect. Usehead; echo '...'; tail
to get the dots in there as well.
– Kusalananda♦
Mar 26 at 19:18
Yeah, combined with @Kusalananda's comment it's much more succinct than my solution. I'll mark as correct for now! Thanks.
– MadisonCooper
Mar 26 at 19:22
4
@Kusalananda, nope, on my system, GNU head (coreutils 8.26) reads blocks of 8192 bytes. It does try to seek back, but obviously can't in case of a pipe. Though anyway, is there really a requirement forhead
to not over-read? (same on Mac, it seems to read by blocks)
– ilkkachu
Mar 26 at 19:22
2
@ilkkachu yeah, even as it stands the tail part is occasionally blank even on sufficiently long output (I know since I'm using head -n 35, and that's full), but fortunately it's just a convenience to watch and I can wait 3 seconds for a refresh if needed.
– MadisonCooper
Mar 26 at 19:27
3
@Kusalananda, POSIX allows implementations to do that when the input is not seekable and most implementations do. Not doing it would mean reading one byte at a time which would be very inefficient (or put the data back onto the pipe where supported which would cause all sorts of different problems). The onlyhead
implementation I know that reads one byte at a time to avoid reading past the 10th newline is ksh93'shead
builtin.
– Stéphane Chazelas
Mar 26 at 20:04
|
show 3 more comments
Are you looking to do something like the following? Shows output from both head and tail.
$ showqueue | (head && tail)
6
Some implementations ofhead
may read more than 10 lines and leave nothing fortail
to read. The utility is not supposed to do that and I believe that the GNU tools are well behave in this respect. Usehead; echo '...'; tail
to get the dots in there as well.
– Kusalananda♦
Mar 26 at 19:18
Yeah, combined with @Kusalananda's comment it's much more succinct than my solution. I'll mark as correct for now! Thanks.
– MadisonCooper
Mar 26 at 19:22
4
@Kusalananda, nope, on my system, GNU head (coreutils 8.26) reads blocks of 8192 bytes. It does try to seek back, but obviously can't in case of a pipe. Though anyway, is there really a requirement forhead
to not over-read? (same on Mac, it seems to read by blocks)
– ilkkachu
Mar 26 at 19:22
2
@ilkkachu yeah, even as it stands the tail part is occasionally blank even on sufficiently long output (I know since I'm using head -n 35, and that's full), but fortunately it's just a convenience to watch and I can wait 3 seconds for a refresh if needed.
– MadisonCooper
Mar 26 at 19:27
3
@Kusalananda, POSIX allows implementations to do that when the input is not seekable and most implementations do. Not doing it would mean reading one byte at a time which would be very inefficient (or put the data back onto the pipe where supported which would cause all sorts of different problems). The onlyhead
implementation I know that reads one byte at a time to avoid reading past the 10th newline is ksh93'shead
builtin.
– Stéphane Chazelas
Mar 26 at 20:04
|
show 3 more comments
Are you looking to do something like the following? Shows output from both head and tail.
$ showqueue | (head && tail)
Are you looking to do something like the following? Shows output from both head and tail.
$ showqueue | (head && tail)
answered Mar 26 at 19:16
Timothy PulliamTimothy Pulliam
1,4301025
1,4301025
6
Some implementations ofhead
may read more than 10 lines and leave nothing fortail
to read. The utility is not supposed to do that and I believe that the GNU tools are well behave in this respect. Usehead; echo '...'; tail
to get the dots in there as well.
– Kusalananda♦
Mar 26 at 19:18
Yeah, combined with @Kusalananda's comment it's much more succinct than my solution. I'll mark as correct for now! Thanks.
– MadisonCooper
Mar 26 at 19:22
4
@Kusalananda, nope, on my system, GNU head (coreutils 8.26) reads blocks of 8192 bytes. It does try to seek back, but obviously can't in case of a pipe. Though anyway, is there really a requirement forhead
to not over-read? (same on Mac, it seems to read by blocks)
– ilkkachu
Mar 26 at 19:22
2
@ilkkachu yeah, even as it stands the tail part is occasionally blank even on sufficiently long output (I know since I'm using head -n 35, and that's full), but fortunately it's just a convenience to watch and I can wait 3 seconds for a refresh if needed.
– MadisonCooper
Mar 26 at 19:27
3
@Kusalananda, POSIX allows implementations to do that when the input is not seekable and most implementations do. Not doing it would mean reading one byte at a time which would be very inefficient (or put the data back onto the pipe where supported which would cause all sorts of different problems). The onlyhead
implementation I know that reads one byte at a time to avoid reading past the 10th newline is ksh93'shead
builtin.
– Stéphane Chazelas
Mar 26 at 20:04
|
show 3 more comments
6
Some implementations ofhead
may read more than 10 lines and leave nothing fortail
to read. The utility is not supposed to do that and I believe that the GNU tools are well behave in this respect. Usehead; echo '...'; tail
to get the dots in there as well.
– Kusalananda♦
Mar 26 at 19:18
Yeah, combined with @Kusalananda's comment it's much more succinct than my solution. I'll mark as correct for now! Thanks.
– MadisonCooper
Mar 26 at 19:22
4
@Kusalananda, nope, on my system, GNU head (coreutils 8.26) reads blocks of 8192 bytes. It does try to seek back, but obviously can't in case of a pipe. Though anyway, is there really a requirement forhead
to not over-read? (same on Mac, it seems to read by blocks)
– ilkkachu
Mar 26 at 19:22
2
@ilkkachu yeah, even as it stands the tail part is occasionally blank even on sufficiently long output (I know since I'm using head -n 35, and that's full), but fortunately it's just a convenience to watch and I can wait 3 seconds for a refresh if needed.
– MadisonCooper
Mar 26 at 19:27
3
@Kusalananda, POSIX allows implementations to do that when the input is not seekable and most implementations do. Not doing it would mean reading one byte at a time which would be very inefficient (or put the data back onto the pipe where supported which would cause all sorts of different problems). The onlyhead
implementation I know that reads one byte at a time to avoid reading past the 10th newline is ksh93'shead
builtin.
– Stéphane Chazelas
Mar 26 at 20:04
6
6
Some implementations of
head
may read more than 10 lines and leave nothing for tail
to read. The utility is not supposed to do that and I believe that the GNU tools are well behave in this respect. Use head; echo '...'; tail
to get the dots in there as well.– Kusalananda♦
Mar 26 at 19:18
Some implementations of
head
may read more than 10 lines and leave nothing for tail
to read. The utility is not supposed to do that and I believe that the GNU tools are well behave in this respect. Use head; echo '...'; tail
to get the dots in there as well.– Kusalananda♦
Mar 26 at 19:18
Yeah, combined with @Kusalananda's comment it's much more succinct than my solution. I'll mark as correct for now! Thanks.
– MadisonCooper
Mar 26 at 19:22
Yeah, combined with @Kusalananda's comment it's much more succinct than my solution. I'll mark as correct for now! Thanks.
– MadisonCooper
Mar 26 at 19:22
4
4
@Kusalananda, nope, on my system, GNU head (coreutils 8.26) reads blocks of 8192 bytes. It does try to seek back, but obviously can't in case of a pipe. Though anyway, is there really a requirement for
head
to not over-read? (same on Mac, it seems to read by blocks)– ilkkachu
Mar 26 at 19:22
@Kusalananda, nope, on my system, GNU head (coreutils 8.26) reads blocks of 8192 bytes. It does try to seek back, but obviously can't in case of a pipe. Though anyway, is there really a requirement for
head
to not over-read? (same on Mac, it seems to read by blocks)– ilkkachu
Mar 26 at 19:22
2
2
@ilkkachu yeah, even as it stands the tail part is occasionally blank even on sufficiently long output (I know since I'm using head -n 35, and that's full), but fortunately it's just a convenience to watch and I can wait 3 seconds for a refresh if needed.
– MadisonCooper
Mar 26 at 19:27
@ilkkachu yeah, even as it stands the tail part is occasionally blank even on sufficiently long output (I know since I'm using head -n 35, and that's full), but fortunately it's just a convenience to watch and I can wait 3 seconds for a refresh if needed.
– MadisonCooper
Mar 26 at 19:27
3
3
@Kusalananda, POSIX allows implementations to do that when the input is not seekable and most implementations do. Not doing it would mean reading one byte at a time which would be very inefficient (or put the data back onto the pipe where supported which would cause all sorts of different problems). The only
head
implementation I know that reads one byte at a time to avoid reading past the 10th newline is ksh93's head
builtin.– Stéphane Chazelas
Mar 26 at 20:04
@Kusalananda, POSIX allows implementations to do that when the input is not seekable and most implementations do. Not doing it would mean reading one byte at a time which would be very inefficient (or put the data back onto the pipe where supported which would cause all sorts of different problems). The only
head
implementation I know that reads one byte at a time to avoid reading past the 10th newline is ksh93's head
builtin.– Stéphane Chazelas
Mar 26 at 20:04
|
show 3 more comments
Using the same approach as you, using a temporary file, but doing it slightly shorter:
showqueue >/tmp/q.out; head -n 20 /tmp/q.out; echo '...'; tail -n 20 /tmp/q.out
This would not suffer from the same issues as discussed under another answer, but would possibly show the same lines twice if the output was shorter than 40 lines.
+1 I was thinking of the same solution with the slight variation of usingtee
to let the output go straight intohead
and only using the saved output fortail
.
– Joe
Apr 9 at 10:31
add a comment |
Using the same approach as you, using a temporary file, but doing it slightly shorter:
showqueue >/tmp/q.out; head -n 20 /tmp/q.out; echo '...'; tail -n 20 /tmp/q.out
This would not suffer from the same issues as discussed under another answer, but would possibly show the same lines twice if the output was shorter than 40 lines.
+1 I was thinking of the same solution with the slight variation of usingtee
to let the output go straight intohead
and only using the saved output fortail
.
– Joe
Apr 9 at 10:31
add a comment |
Using the same approach as you, using a temporary file, but doing it slightly shorter:
showqueue >/tmp/q.out; head -n 20 /tmp/q.out; echo '...'; tail -n 20 /tmp/q.out
This would not suffer from the same issues as discussed under another answer, but would possibly show the same lines twice if the output was shorter than 40 lines.
Using the same approach as you, using a temporary file, but doing it slightly shorter:
showqueue >/tmp/q.out; head -n 20 /tmp/q.out; echo '...'; tail -n 20 /tmp/q.out
This would not suffer from the same issues as discussed under another answer, but would possibly show the same lines twice if the output was shorter than 40 lines.
edited Mar 26 at 19:46
answered Mar 26 at 19:30
Kusalananda♦Kusalananda
143k18267446
143k18267446
+1 I was thinking of the same solution with the slight variation of usingtee
to let the output go straight intohead
and only using the saved output fortail
.
– Joe
Apr 9 at 10:31
add a comment |
+1 I was thinking of the same solution with the slight variation of usingtee
to let the output go straight intohead
and only using the saved output fortail
.
– Joe
Apr 9 at 10:31
+1 I was thinking of the same solution with the slight variation of using
tee
to let the output go straight into head
and only using the saved output for tail
.– Joe
Apr 9 at 10:31
+1 I was thinking of the same solution with the slight variation of using
tee
to let the output go straight into head
and only using the saved output for tail
.– Joe
Apr 9 at 10:31
add a comment |
awk solution for an arbitrary number of lines shown from the head and the tail (change n=3
to set the amount):
$ seq 99999 | awk -v n=3 'NR <= n; NR > n a[NR] = $0; delete a[NR-n];
END print "..."; for (i = NR-n+1; i <= NR; i++) if (i in a) print a[i]; '
1
2
3
...
99997
99998
99999
As it's written, the head and tail parts will not overlap, even if the input is shorter than 2*n
lines.
In some awk implementations, using for (x in a) print a[x];
in the END
part also works. But in general, it's not guaranteed to return the array entries in the correct order, and doesn't in e.g. mawk.
Ifm
is the number of lines in the file, andm < 2n
, that will print2n - m
empty lines. Also have look here ;-).for (x in a)
will iterate in order only in GNU awk; if you think that it works inmawk
too, test with > 10 values.
– mosvy
Mar 26 at 20:31
@mosvy, oh, whoops, I forgot to re-check that after realizingx in a
wasn't right. Fixed now, thanks. There's also another awk answer in the question Stéphane linked to. I like the modulo trick, though!
– ilkkachu
Mar 26 at 20:42
add a comment |
awk solution for an arbitrary number of lines shown from the head and the tail (change n=3
to set the amount):
$ seq 99999 | awk -v n=3 'NR <= n; NR > n a[NR] = $0; delete a[NR-n];
END print "..."; for (i = NR-n+1; i <= NR; i++) if (i in a) print a[i]; '
1
2
3
...
99997
99998
99999
As it's written, the head and tail parts will not overlap, even if the input is shorter than 2*n
lines.
In some awk implementations, using for (x in a) print a[x];
in the END
part also works. But in general, it's not guaranteed to return the array entries in the correct order, and doesn't in e.g. mawk.
Ifm
is the number of lines in the file, andm < 2n
, that will print2n - m
empty lines. Also have look here ;-).for (x in a)
will iterate in order only in GNU awk; if you think that it works inmawk
too, test with > 10 values.
– mosvy
Mar 26 at 20:31
@mosvy, oh, whoops, I forgot to re-check that after realizingx in a
wasn't right. Fixed now, thanks. There's also another awk answer in the question Stéphane linked to. I like the modulo trick, though!
– ilkkachu
Mar 26 at 20:42
add a comment |
awk solution for an arbitrary number of lines shown from the head and the tail (change n=3
to set the amount):
$ seq 99999 | awk -v n=3 'NR <= n; NR > n a[NR] = $0; delete a[NR-n];
END print "..."; for (i = NR-n+1; i <= NR; i++) if (i in a) print a[i]; '
1
2
3
...
99997
99998
99999
As it's written, the head and tail parts will not overlap, even if the input is shorter than 2*n
lines.
In some awk implementations, using for (x in a) print a[x];
in the END
part also works. But in general, it's not guaranteed to return the array entries in the correct order, and doesn't in e.g. mawk.
awk solution for an arbitrary number of lines shown from the head and the tail (change n=3
to set the amount):
$ seq 99999 | awk -v n=3 'NR <= n; NR > n a[NR] = $0; delete a[NR-n];
END print "..."; for (i = NR-n+1; i <= NR; i++) if (i in a) print a[i]; '
1
2
3
...
99997
99998
99999
As it's written, the head and tail parts will not overlap, even if the input is shorter than 2*n
lines.
In some awk implementations, using for (x in a) print a[x];
in the END
part also works. But in general, it's not guaranteed to return the array entries in the correct order, and doesn't in e.g. mawk.
edited Mar 26 at 20:37
answered Mar 26 at 19:33
ilkkachuilkkachu
63.6k10104182
63.6k10104182
Ifm
is the number of lines in the file, andm < 2n
, that will print2n - m
empty lines. Also have look here ;-).for (x in a)
will iterate in order only in GNU awk; if you think that it works inmawk
too, test with > 10 values.
– mosvy
Mar 26 at 20:31
@mosvy, oh, whoops, I forgot to re-check that after realizingx in a
wasn't right. Fixed now, thanks. There's also another awk answer in the question Stéphane linked to. I like the modulo trick, though!
– ilkkachu
Mar 26 at 20:42
add a comment |
Ifm
is the number of lines in the file, andm < 2n
, that will print2n - m
empty lines. Also have look here ;-).for (x in a)
will iterate in order only in GNU awk; if you think that it works inmawk
too, test with > 10 values.
– mosvy
Mar 26 at 20:31
@mosvy, oh, whoops, I forgot to re-check that after realizingx in a
wasn't right. Fixed now, thanks. There's also another awk answer in the question Stéphane linked to. I like the modulo trick, though!
– ilkkachu
Mar 26 at 20:42
If
m
is the number of lines in the file, and m < 2n
, that will print 2n - m
empty lines. Also have look here ;-). for (x in a)
will iterate in order only in GNU awk; if you think that it works in mawk
too, test with > 10 values.– mosvy
Mar 26 at 20:31
If
m
is the number of lines in the file, and m < 2n
, that will print 2n - m
empty lines. Also have look here ;-). for (x in a)
will iterate in order only in GNU awk; if you think that it works in mawk
too, test with > 10 values.– mosvy
Mar 26 at 20:31
@mosvy, oh, whoops, I forgot to re-check that after realizing
x in a
wasn't right. Fixed now, thanks. There's also another awk answer in the question Stéphane linked to. I like the modulo trick, though!– ilkkachu
Mar 26 at 20:42
@mosvy, oh, whoops, I forgot to re-check that after realizing
x in a
wasn't right. Fixed now, thanks. There's also another awk answer in the question Stéphane linked to. I like the modulo trick, though!– ilkkachu
Mar 26 at 20:42
add a comment |
You can use simple awk script. Added circle buffer there n=3 is for last 3 lines
awk 'NR<=n print $0 A[NR%n]=$0 END for (i=1; i<=n; ++i) if (NR+i>2*n)print A[(NR+i)%n] ' n=3 < <( seq 10 )
After update its not simple anymore though,
1
This would only show the first and last line. To show more than that, you would have to changeNR==1
toNR <= 10
(or something), and then collect lines into a (circular?) buffer that you later output in theEND
block.
– Kusalananda♦
Mar 26 at 19:32
yes let me update
– Abdurrahim
Mar 26 at 19:43
This will print some lines twice when then the number of lines is less than 2 * n.
– mosvy
Mar 26 at 20:39
:) ok adding that as well now I am curious what you will find next :D
– Abdurrahim
Mar 27 at 7:22
add a comment |
You can use simple awk script. Added circle buffer there n=3 is for last 3 lines
awk 'NR<=n print $0 A[NR%n]=$0 END for (i=1; i<=n; ++i) if (NR+i>2*n)print A[(NR+i)%n] ' n=3 < <( seq 10 )
After update its not simple anymore though,
1
This would only show the first and last line. To show more than that, you would have to changeNR==1
toNR <= 10
(or something), and then collect lines into a (circular?) buffer that you later output in theEND
block.
– Kusalananda♦
Mar 26 at 19:32
yes let me update
– Abdurrahim
Mar 26 at 19:43
This will print some lines twice when then the number of lines is less than 2 * n.
– mosvy
Mar 26 at 20:39
:) ok adding that as well now I am curious what you will find next :D
– Abdurrahim
Mar 27 at 7:22
add a comment |
You can use simple awk script. Added circle buffer there n=3 is for last 3 lines
awk 'NR<=n print $0 A[NR%n]=$0 END for (i=1; i<=n; ++i) if (NR+i>2*n)print A[(NR+i)%n] ' n=3 < <( seq 10 )
After update its not simple anymore though,
You can use simple awk script. Added circle buffer there n=3 is for last 3 lines
awk 'NR<=n print $0 A[NR%n]=$0 END for (i=1; i<=n; ++i) if (NR+i>2*n)print A[(NR+i)%n] ' n=3 < <( seq 10 )
After update its not simple anymore though,
edited Mar 27 at 7:22
answered Mar 26 at 19:25
AbdurrahimAbdurrahim
1012
1012
1
This would only show the first and last line. To show more than that, you would have to changeNR==1
toNR <= 10
(or something), and then collect lines into a (circular?) buffer that you later output in theEND
block.
– Kusalananda♦
Mar 26 at 19:32
yes let me update
– Abdurrahim
Mar 26 at 19:43
This will print some lines twice when then the number of lines is less than 2 * n.
– mosvy
Mar 26 at 20:39
:) ok adding that as well now I am curious what you will find next :D
– Abdurrahim
Mar 27 at 7:22
add a comment |
1
This would only show the first and last line. To show more than that, you would have to changeNR==1
toNR <= 10
(or something), and then collect lines into a (circular?) buffer that you later output in theEND
block.
– Kusalananda♦
Mar 26 at 19:32
yes let me update
– Abdurrahim
Mar 26 at 19:43
This will print some lines twice when then the number of lines is less than 2 * n.
– mosvy
Mar 26 at 20:39
:) ok adding that as well now I am curious what you will find next :D
– Abdurrahim
Mar 27 at 7:22
1
1
This would only show the first and last line. To show more than that, you would have to change
NR==1
to NR <= 10
(or something), and then collect lines into a (circular?) buffer that you later output in the END
block.– Kusalananda♦
Mar 26 at 19:32
This would only show the first and last line. To show more than that, you would have to change
NR==1
to NR <= 10
(or something), and then collect lines into a (circular?) buffer that you later output in the END
block.– Kusalananda♦
Mar 26 at 19:32
yes let me update
– Abdurrahim
Mar 26 at 19:43
yes let me update
– Abdurrahim
Mar 26 at 19:43
This will print some lines twice when then the number of lines is less than 2 * n.
– mosvy
Mar 26 at 20:39
This will print some lines twice when then the number of lines is less than 2 * n.
– mosvy
Mar 26 at 20:39
:) ok adding that as well now I am curious what you will find next :D
– Abdurrahim
Mar 27 at 7:22
:) ok adding that as well now I am curious what you will find next :D
– Abdurrahim
Mar 27 at 7:22
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.
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%2f508818%2fbash-method-for-viewing-beginning-and-end-of-file%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
Closed as duplicate, but the question only shares similarities (Mine is more about a command, I learned, and not files), and the answers here didn't show up there, but if you insist
– MadisonCooper
Mar 26 at 19:59
2
Fair enough. I've reopened. Note that the answer that you have accepted will only work for large outputs as noted at my answer at Command to display first few and last few lines of a file (try
seq 30 | (head && tail)
for instance).– Stéphane Chazelas
Mar 26 at 20:16
2
I meant that answer. That Q&A has two answers of mine because another Q&A was merged with it (see question history)
– Stéphane Chazelas
Mar 26 at 20:24