12

Here is the issue, I would like to count the number of jobs I have in the hpc, but it is not one of the readily provided features. So I made this simple script

squeue -u user_name | wc -l

where squeue prints all the jobs like the following

> squeue -u user_name
   JOBID PARTITION NAME     USER ST       TIME  NODES NODELIST(REASON)
 8840441    theory cteq      fxm PD       0:00      1 (Resources)
 8840442    theory cteq      fxm PD       0:00      1 (Priority)
 8840443    theory cteq      fxm PD       0:00      1 (Priority)
 8840444    theory cteq      fxm PD       0:00      1 (Priority)

which would be piped to wc and the number of lines would be counted. However, the first line is not an entry of the job. How may I instruct wc to skip the first line when counting? Or should I just take the output of wc and minus one to it?

Thanks in advance!

zyy
  • 271
  • 1
  • 2
  • 10
  • Referring to this [answer](https://unix.stackexchange.com/a/209070/376814), you would do `squeue -u user_name | sed '1d' | wc -l`. Although, there are many very nice solutions in the answers of this question. – zyy Jan 13 '20 at 14:46

4 Answers4

45

You can supress the header line from squeue with the -h-option. That would eliminate the need to remove the first row.

From the man page of squeue:

-h, --noheader
    Do not print a header on the output.
Bex
  • 768
  • 5
  • 20
  • 6
    Or add an option to `squeue` like `-n` to simply output the count directly. – Scot Jan 11 '20 at 04:30
  • 2
    @Scot you should make that an answer. I would upvote. – Bex Jan 11 '20 at 13:08
  • Thanks, @Bex - think it’s too much a derivative of your answer. Maybe if there was code present, I’d make an answer to add the code to do so, but without that, I’ll leave it as a comment on yours... – Scot Jan 12 '20 at 06:57
33

There are many many ways to do this, the first I thought of was:

squeue -u user_name | tail -n +2 | wc -l

From the man page for tail:

-n, --lines=[+]NUM            output the last NUM lines, instead of the last 10;
                              or use -n +NUM to output starting with line NUM

So fo you -n +2 should skip the first line.

You can also use the sort form of tail: tail +2

user1794469
  • 3,909
  • 1
  • 23
  • 42
  • 3
    `tail` crossed my mind, but don't remember having seen or considered the +n option. For both the idea+explanation and reference: Good one! – Mr. Donutz Jan 10 '20 at 20:04
  • 2
    @Mr.Donutz `head` has a similar `+` flag that leaves off some number of rows. I always have a hard time remembering which does what though. – user1794469 Jan 10 '20 at 20:09
  • 3
    I prefer `sed 1d` bc i have a hard time remembering the counting directions of head and tail – D. Ben Knoble Jan 11 '20 at 19:29
  • 1
    @Ben: Great call. The OP didn't say which OS. I have some AIX boxes and tail -n +2 doesn't work on them. sed 1d does. – Scottie H Jan 13 '20 at 03:54
5

Just for fun. You can subract 1 from the wc output

echo $(( $(squeue -u user_name|wc -l)-1 ))

or

squeue -u user_name|wc -l|awk '{print $0-1}'

When you use awk you can count the lines with awk and so avoid wc

squeue -u user_name|awk 'END{print NR-1}'

END means, that the following block is executed after all lines are read and NR means the number of lines read so far.

miracle173
  • 503
  • 2
  • 12
3

Actually there are a couple of ways to achieve the same result. One other way is to pipe the output of the sequence command to sed, just like below

squeue -u user_name | sed '1d' | wc -l

kokei
  • 69
  • 1
  • 4