Bash method for viewing beginning and end of fileCommand 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

Finding all intervals that match predicate in vector

How can I get through very long and very dry, but also very useful technical documents when learing a new tool?

Is exact Kanji stroke length important?

Do I need a multiple entry visa for a trip UK -> Sweden -> UK?

Curses work by shouting - How to avoid collateral damage?

What are the ramifications of creating a homebrew world without an Astral Plane?

Everything Bob says is false. How does he get people to trust him?

Is there a problem with hiding "forgot password" until it's needed?

How can my private key be revealed if I use the same nonce while generating the signature?

Hostile work environment after whistle-blowing on coworker and our boss. What do I do?

HashMap containsKey() returns false although hashCode() and equals() are true

What is the oldest known work of fiction?

apt-get update is failing in debian

Why did Kant, Hegel, and Adorno leave some words and phrases in the Greek alphabet?

Can somebody explain Brexit in a few child-proof sentences?

Should my PhD thesis be submitted under my legal name?

Invariants between two isomorphic vector spaces

Why does John Bercow say “unlock” after reading out the results of a vote?

Where in the Bible does the greeting ("Dominus Vobiscum") used at Mass come from?

Opposite of a diet

Why are all the doors on Ferenginar (the Ferengi home world) far shorter than the average Ferengi?

how to analyze "是其于主也至忠矣"

MaTeX, font size, and PlotLegends

How can I use the arrow sign in my bash prompt?



Bash method for viewing beginning and end of file


Command 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













4















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?










share|improve this question









New contributor




MadisonCooper is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • 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
    1 hour ago







  • 1





    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
    1 hour ago







  • 1





    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
    57 mins ago
















4















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?










share|improve this question









New contributor




MadisonCooper is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • 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
    1 hour ago







  • 1





    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
    1 hour ago







  • 1





    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
    57 mins ago














4












4








4








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?










share|improve this question









New contributor




MadisonCooper is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












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






share|improve this question









New contributor




MadisonCooper is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




MadisonCooper is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 2 hours ago









Rui F Ribeiro

41.7k1483142




41.7k1483142






New contributor




MadisonCooper is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 2 hours ago









MadisonCooperMadisonCooper

233




233




New contributor




MadisonCooper is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





MadisonCooper is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






MadisonCooper is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • 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
    1 hour ago







  • 1





    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
    1 hour ago







  • 1





    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
    57 mins ago


















  • 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
    1 hour ago







  • 1





    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
    1 hour ago







  • 1





    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
    57 mins ago

















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
1 hour ago






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
1 hour ago





1




1





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
1 hour ago






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
1 hour ago





1




1





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
57 mins ago






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
57 mins ago











4 Answers
4






active

oldest

votes


















8














Are you looking to do something like the following? Shows output from both head and tail.



$ showqueue | (head && tail)





share|improve this answer


















  • 1





    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
    2 hours ago











  • Yeah, combined with @Kusalananda's comment it's much more succinct than my solution. I'll mark as correct for now! Thanks.

    – MadisonCooper
    1 hour ago






  • 1





    @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
    1 hour ago












  • So, while this is actually pretty neat, it might only work for sufficiently long outputs. @MadisonCooper, you may want to check that it works correctly

    – ilkkachu
    1 hour ago






  • 1





    @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
    1 hour ago



















1














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.






share|improve this answer

























  • 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
    50 mins ago











  • @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
    39 mins ago



















0














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) print A[(NR+i)%n] ' n=3 < <( showqueue )





share|improve this answer










New contributor




Abdurrahim is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.















  • 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
    1 hour ago











  • yes let me update

    – Abdurrahim
    1 hour ago











  • This will print some lines twice when then the number of lines is less than 2 * n.

    – mosvy
    42 mins ago


















0














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.






share|improve this answer
























    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
    );



    );






    MadisonCooper is a new contributor. Be nice, and check out our Code of Conduct.









    draft saved

    draft discarded


















    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









    8














    Are you looking to do something like the following? Shows output from both head and tail.



    $ showqueue | (head && tail)





    share|improve this answer


















    • 1





      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
      2 hours ago











    • Yeah, combined with @Kusalananda's comment it's much more succinct than my solution. I'll mark as correct for now! Thanks.

      – MadisonCooper
      1 hour ago






    • 1





      @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
      1 hour ago












    • So, while this is actually pretty neat, it might only work for sufficiently long outputs. @MadisonCooper, you may want to check that it works correctly

      – ilkkachu
      1 hour ago






    • 1





      @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
      1 hour ago
















    8














    Are you looking to do something like the following? Shows output from both head and tail.



    $ showqueue | (head && tail)





    share|improve this answer


















    • 1





      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
      2 hours ago











    • Yeah, combined with @Kusalananda's comment it's much more succinct than my solution. I'll mark as correct for now! Thanks.

      – MadisonCooper
      1 hour ago






    • 1





      @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
      1 hour ago












    • So, while this is actually pretty neat, it might only work for sufficiently long outputs. @MadisonCooper, you may want to check that it works correctly

      – ilkkachu
      1 hour ago






    • 1





      @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
      1 hour ago














    8












    8








    8







    Are you looking to do something like the following? Shows output from both head and tail.



    $ showqueue | (head && tail)





    share|improve this answer













    Are you looking to do something like the following? Shows output from both head and tail.



    $ showqueue | (head && tail)






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered 2 hours ago









    Timothy PulliamTimothy Pulliam

    1,335924




    1,335924







    • 1





      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
      2 hours ago











    • Yeah, combined with @Kusalananda's comment it's much more succinct than my solution. I'll mark as correct for now! Thanks.

      – MadisonCooper
      1 hour ago






    • 1





      @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
      1 hour ago












    • So, while this is actually pretty neat, it might only work for sufficiently long outputs. @MadisonCooper, you may want to check that it works correctly

      – ilkkachu
      1 hour ago






    • 1





      @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
      1 hour ago













    • 1





      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
      2 hours ago











    • Yeah, combined with @Kusalananda's comment it's much more succinct than my solution. I'll mark as correct for now! Thanks.

      – MadisonCooper
      1 hour ago






    • 1





      @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
      1 hour ago












    • So, while this is actually pretty neat, it might only work for sufficiently long outputs. @MadisonCooper, you may want to check that it works correctly

      – ilkkachu
      1 hour ago






    • 1





      @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
      1 hour ago








    1




    1





    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
    2 hours ago





    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
    2 hours ago













    Yeah, combined with @Kusalananda's comment it's much more succinct than my solution. I'll mark as correct for now! Thanks.

    – MadisonCooper
    1 hour ago





    Yeah, combined with @Kusalananda's comment it's much more succinct than my solution. I'll mark as correct for now! Thanks.

    – MadisonCooper
    1 hour ago




    1




    1





    @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
    1 hour ago






    @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
    1 hour ago














    So, while this is actually pretty neat, it might only work for sufficiently long outputs. @MadisonCooper, you may want to check that it works correctly

    – ilkkachu
    1 hour ago





    So, while this is actually pretty neat, it might only work for sufficiently long outputs. @MadisonCooper, you may want to check that it works correctly

    – ilkkachu
    1 hour ago




    1




    1





    @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
    1 hour ago






    @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
    1 hour ago














    1














    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.






    share|improve this answer

























    • 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
      50 mins ago











    • @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
      39 mins ago
















    1














    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.






    share|improve this answer

























    • 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
      50 mins ago











    • @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
      39 mins ago














    1












    1








    1







    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.






    share|improve this answer















    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.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited 44 mins ago

























    answered 1 hour ago









    ilkkachuilkkachu

    62.6k10103180




    62.6k10103180












    • 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
      50 mins ago











    • @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
      39 mins ago


















    • 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
      50 mins ago











    • @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
      39 mins ago

















    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
    50 mins ago





    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
    50 mins ago













    @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
    39 mins ago






    @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
    39 mins ago












    0














    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) print A[(NR+i)%n] ' n=3 < <( showqueue )





    share|improve this answer










    New contributor




    Abdurrahim is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.















    • 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
      1 hour ago











    • yes let me update

      – Abdurrahim
      1 hour ago











    • This will print some lines twice when then the number of lines is less than 2 * n.

      – mosvy
      42 mins ago















    0














    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) print A[(NR+i)%n] ' n=3 < <( showqueue )





    share|improve this answer










    New contributor




    Abdurrahim is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.















    • 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
      1 hour ago











    • yes let me update

      – Abdurrahim
      1 hour ago











    • This will print some lines twice when then the number of lines is less than 2 * n.

      – mosvy
      42 mins ago













    0












    0








    0







    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) print A[(NR+i)%n] ' n=3 < <( showqueue )





    share|improve this answer










    New contributor




    Abdurrahim is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.










    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) print A[(NR+i)%n] ' n=3 < <( showqueue )






    share|improve this answer










    New contributor




    Abdurrahim is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.









    share|improve this answer



    share|improve this answer








    edited 1 hour ago





















    New contributor




    Abdurrahim is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.









    answered 1 hour ago









    AbdurrahimAbdurrahim

    1012




    1012




    New contributor




    Abdurrahim is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.





    New contributor





    Abdurrahim is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






    Abdurrahim is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.







    • 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
      1 hour ago











    • yes let me update

      – Abdurrahim
      1 hour ago











    • This will print some lines twice when then the number of lines is less than 2 * n.

      – mosvy
      42 mins ago












    • 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
      1 hour ago











    • yes let me update

      – Abdurrahim
      1 hour ago











    • This will print some lines twice when then the number of lines is less than 2 * n.

      – mosvy
      42 mins ago







    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
    1 hour ago





    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
    1 hour ago













    yes let me update

    – Abdurrahim
    1 hour ago





    yes let me update

    – Abdurrahim
    1 hour ago













    This will print some lines twice when then the number of lines is less than 2 * n.

    – mosvy
    42 mins ago





    This will print some lines twice when then the number of lines is less than 2 * n.

    – mosvy
    42 mins ago











    0














    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.






    share|improve this answer





























      0














      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.






      share|improve this answer



























        0












        0








        0







        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.






        share|improve this answer















        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.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited 1 hour ago

























        answered 1 hour ago









        KusalanandaKusalananda

        138k17258426




        138k17258426




















            MadisonCooper is a new contributor. Be nice, and check out our Code of Conduct.









            draft saved

            draft discarded


















            MadisonCooper is a new contributor. Be nice, and check out our Code of Conduct.












            MadisonCooper is a new contributor. Be nice, and check out our Code of Conduct.











            MadisonCooper is a new contributor. Be nice, and check out our Code of Conduct.














            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Are there any AGPL-style licences that require source code modifications to be public? Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Announcing the arrival of Valued Associate #679: Cesar Manara Unicorn Meta Zoo #1: Why another podcast?Force derivative works to be publicAre there any GPL like licenses for Apple App Store?Do you violate the GPL if you provide source code that cannot be compiled?GPL - is it distribution to use libraries in an appliance loaned to customers?Distributing App for free which uses GPL'ed codeModifications of server software under GPL, with web/CLI interfaceDoes using an AGPLv3-licensed library prevent me from dual-licensing my own source code?Can I publish only select code under GPLv3 from a private project?Is there published precedent regarding the scope of covered work that uses AGPL software?If MIT licensed code links to GPL licensed code what should be the license of the resulting binary program?If I use a public API endpoint that has its source code licensed under AGPL in my app, do I need to disclose my source?

            2013 GY136 Descoberta | Órbita | Referências Menu de navegação«List Of Centaurs and Scattered-Disk Objects»«List of Known Trans-Neptunian Objects»

            Button changing it's text & action. Good or terrible? The 2019 Stack Overflow Developer Survey Results Are Inchanging text on user mouseoverShould certain functions be “hard to find” for powerusers to discover?Custom liking function - do I need user login?Using different checkbox style for different checkbox behaviorBest Practices: Save and Exit in Software UIInteraction with remote validated formMore efficient UI to progress the user through a complicated process?Designing a popup notice for a gameShould bulk-editing functions be hidden until a table row is selected, or is there a better solution?Is it bad practice to disable (replace) the context menu?