Help streamlining output of a command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help streamlining output of a command
# 1  
Old 11-30-2017
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:
Code:
bjobs -u all -m C7CRA

I now get the following:
Code:
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:
Code:
#!/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:
Code:
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.
# 2  
Old 11-30-2017
Please show the (shortened) output of ypcat.

Last edited by RudiC; 11-30-2017 at 10:24 AM..
# 3  
Old 11-30-2017
The ypcat command will generally return:

Code:
 
 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:

Code:
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

# 4  
Old 11-30-2017
Perhaps something like this:
Code:
#!/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
}


Last edited by Scrutinizer; 11-30-2017 at 01:02 PM..
# 5  
Old 12-01-2017
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.
# 6  
Old 12-01-2017
With
Quote:
Originally Posted by crimso
The ypcat command will generally return:

Code:
 
 John Doe Department

. . .
it's no surprise that
Quote:
Originally Posted by crimso
. . .
USERNAME=`ypcat passwd | grep ${USERID} | cut -d: -f5`
. . .
doesn't yield the desired.


EDIT: How about sth like

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


Last edited by RudiC; 12-01-2017 at 09:42 AM.. Reason: typo
# 7  
Old 12-01-2017
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to execute a command on each line of output from another command?

Hello :) new to bash not to programming. I have an on-going need to change the owning group on sets of files and directories from the one they were created with or changed to on update to the one they need to have going forward. find {target_root} -group wrong_group gets me a newline... (4 Replies)
Discussion started by: naftali
4 Replies

2. Shell Programming and Scripting

Insert title as output of command to appended file if no output from command

I am using UNIX to create a script on our system. I have setup my commands to append their output to an outage file. However, some of the commands return no output and so I would like something to take their place. What I need The following command is placed at the prompt: TICLI... (4 Replies)
Discussion started by: jbrass
4 Replies

3. UNIX for Dummies Questions & Answers

Set Command to output a log of every command executed in the script

Hi Guys, I like to output every command executed in the script to a file. I have tried set -x which does the same. But it is not giving the logs of the child script which is being called from my script. Is there any parameters in the Set command or someother way where i can see the log... (2 Replies)
Discussion started by: mac4rfree
2 Replies

4. Red Hat

Command understanding the output file destination in case of standard output!!!!!

I ran the following command. cat abc.c > abc.c I got message the following message from command cat: cat: abc.c : input file is same as the output file How the command came to know of the destination file name as the command is sending output to standard file. (3 Replies)
Discussion started by: ravisingh
3 Replies

5. Shell Programming and Scripting

Want to terminate command execution when string found in the command output

Hi Experts, I am very much new to linux scripting, I am currently working on reducing my manual work and hence writing a script to automate few task. I am running below command to snmpwalk the router.. snmpwalk -v 3 -u WANDL_SU -a MD5 -A vfipmpls -x DES -X VfIpMpLs -l authPriv... (19 Replies)
Discussion started by: Hanumant.madane
19 Replies

6. UNIX for Dummies Questions & Answers

passing command output from one command to the next command in cshell

HI Guys, I hope you are well. I am trying to write a script that gets executed every time i open a shell (cshell). I have two questions about that 1) I need to enter these commands $ echo $DISPLAY $ setenv $DISPLAY output_of_echo_$display_command How can i write a... (2 Replies)
Discussion started by: kaaliakahn
2 Replies

7. UNIX for Dummies Questions & Answers

read command - using output from command substitution

Hey, guys! Trying to research this is such a pain since the read command itself is a common word. Try searching "unix OR linux read command examples" or using the command substitution keyword. :eek: So, I wanted to use a command statement similar to the following. This is kinda taken... (2 Replies)
Discussion started by: ProGrammar
2 Replies

8. UNIX for Advanced & Expert Users

Repeat output of last command w/o repeating last command

Is there a way to repeat the output of the last command for filtering without running the command again? All I could think of was to copy all the data to a text file and process it that way, is there another way? Like say I want to grep server.server.lan from a dtrace that was pages long after I... (5 Replies)
Discussion started by: glev2005
5 Replies

9. UNIX for Dummies Questions & Answers

Command display output on console and simultaneously save the command and its output

Hi folks, Please advise which command/command line shall I run; 1) to display the command and its output on console 2) simultaneous to save the command and its output on a file I tried tee command as follows; $ ps aux | grep mysql | tee /path/to/output.txt It displayed the... (7 Replies)
Discussion started by: satimis
7 Replies

10. UNIX for Dummies Questions & Answers

problem with output of find command being input to basename command...

Hi, I am triying to make sure that there exists only one file with the pattern abc* in path /path/. This directory is having many huge files. If there is only one file then I have to take its complete name only to use furter in my script. I am planning to do like this: if ; then... (2 Replies)
Discussion started by: new_learner
2 Replies
Login or Register to Ask a Question