Help streamlining output of a command

Greetings,

I DON'T actually need a response since I have a solution but the education would be nice.
Some history:
We've recently converted from alpha login ID's in the LDAP world to something called workday ID's(not my idea!). What this meant for us Linux admins was we had change everyone's alpha ID they'd used for years to a numeric ID. Not a big deal with the exception of a particular command we use to list jobs on the cluster. If I execute:

bjobs -u all -m C7CRA

I now get the following:

JOBID   USER    STAT  QUEUE      FROM_HOST   EXEC_HOST   JOB_NAME   SUBMIT_TIME
452790  257738  RUN   crash_para t70cra110   16*t70c7n22 *art_ODB_5 Nov 27 10:00
                                             16*t70c7n222
                                             16*t70c7n223
                                             16*t70c7n224
                                             16*t70c7n227
                                             16*t70c7n226
                                             16*t70c7n228
                                             16*t70c7n225
452803  257738  RUN   crash_para t70cra110   16*t70c7n22 *L_Corr_03 Nov 27 10:55
                                             16*t70c7n230
                                             16*t70c7n231
                                             16*t70c7n234
                                             16*t70c7n236
                                             16*t70c7n233
                                             16*t70c7n232
                                             16*t70c7n235
452878  257738  RUN   crash_para t70cra110   16*t70c7n25 *L_Corr_04 Nov 27 17:01
                                             16*t70c7n261
                                             16*t70c7n262
                                             16*t70c7n263
                                             16*t70c7n264
                                             16*t70c7n258
                                             16*t70c7n260
                                             16*t70c7n259

The second field of the job ID line is the user's name now and we have been using ypcat to figure out who's who. A real pain, so I wrote a script (bjobs.sh) to replace the workday numeric ID with the 5th field from the ypcat passwd output.
Script:

#!/bin/bash

echo "JOBID   USER         STAT  QUEUE      FROM_HOST   EXEC_HOST   JOB_NAME   SUBMIT_TIME"
for JOBID in `bjobs $* | grep RUN | awk '{print $1}'`
do
        USERID=`bjobs ${JOBID} | grep RUN | awk '{print $2}'`
        USERNAME=`ypcat passwd | grep ${USERID} | cut -d: -f5`
        bjobs ${JOBID} | grep -v STAT | sed "s/${USERID}/${USERNAME}/"
done

Executed as:

bjobs.sh -u all -m C76CRA

While this works just fine I though I'd put this up here to see if anyone had a better way of doing this. Maybe a one line "bjobs -u all -m C7CRA" with awk and/or sed to grab that second field of the jobid line and replace it as the script does using the variable it gets from the ypcat passwd output. I tried various methods, got frustrated and simply went with what I knew would work. Thanks in advance for anyone willing to chime in.

Please show the (shortened) output of ypcat .

The ypcat command will generally return:

 
 John Doe Department
 

---------- Post updated at 11:02 AM ---------- Previous update was at 10:57 AM ----------

So what we're getting now when using the script is:

JOBID   USER           STAT  QUEUE      FROM_HOST   EXEC_HOST   JOB_NAME   SUBMIT_TIME
452790  John Doe VPD5  RUN   crash_para t70cra110   16*t70c7n22 *art_ODB_5 Nov 27 10:00
                                             16*t70c7n222
                                             16*t70c7n223
                                             16*t70c7n224
                                             16*t70c7n227
                                             16*t70c7n226
                                             16*t70c7n228
                                             16*t70c7n225
452803  John Doe VPD5  RUN   crash_para t70cra110   16*t70c7n22 *L_Corr_03 Nov 27 10:55
                                             16*t70c7n230
                                             16*t70c7n231
                                             16*t70c7n234
                                             16*t70c7n236
                                             16*t70c7n233
                                             16*t70c7n232
                                             16*t70c7n235
452878  John Doe VPD5  RUN   crash_para t70cra110   16*t70c7n25 *L_Corr_04 Nov 27 17:01
                                             16*t70c7n261
                                             16*t70c7n262
                                             16*t70c7n263
                                             16*t70c7n264
                                             16*t70c7n258
                                             16*t70c7n260
                                             16*t70c7n259

Perhaps something like this:

#!/bin/bash
linefmt="%-8s%-13s%s\n"
bjobs "$@" | 
{
  read jobid uid rest
  printf "$linefmt" "$jobid" "$uid" "$rest"
  while read line
  do
    if [[ $line == *RUN* ]]; then
      read jobid uid rest <<< "$line"
      gecos=$(getent passwd "$uid" | cut -d: -f5)
      printf "$linefmt" "$jobid" "$gecos" "$rest"
    else
      printf "%50s%s\n" " " "$line"
    fi
  done
}

Good morning,

Thanks for the response Scrutinizer. The line formatting was a good idea as it gave me something to go on with the other jobs like PEND and EXIT since they format a bit differently. I can now actually arrange the total output into something formatted better than the default.
I ended up using ypcat rather than getent since getent won't return user ID's that are all numeric. After some tinkering, getent proved to only return ID's that have at least one alpha character in them.

With

it's no surprise that

doesn't yield the desired.

EDIT: How about sth like

bjobs -u all -m C7CRA | awk -F: 'NR == FNR {T[$1] = $5; next} NF == 10 {$2 = T[$2]} 1 ' /etc/passwd FS=" *" -

Hello RudiC,
Thanks for the response. The only issue with the single line command is it only works on the NIS master with the passwd file at /var/yp/files/passwd. All the other nodes in the cluster have the most basic passwd file. Which is why I felt forced to ypcat the passwd file for the 'real name'.
I had originally tried to assign a variable the value from ypcat on the awk command line but failed.

That presumably is because getent passwd takes both uid and user name as parameter and has to make a decision which one is the case, where all-numeric is likely deemed a uid ...

With yp commands it is more efficient to use ypmatch , rather than ypcat . So you could try:

ypmatch "$uid" passwd.byuid

--

You are welcome. Another aspect of my suggestion is that it tries to reduce the number of external utility calls. That means it becomes more efficient and perhaps takes up less time..