8

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?

14

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

$ showqueue | (head && tail)
  • 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
  • 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 for head 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 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
2

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 using tee to let the output go straight into head and only using the saved output for tail. – Joe Apr 9 at 10:31
2

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.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.