Substituting field contents using AWK

Hello, wondering if anybody may be help me. This is the output of a file, from which I need to display a number of fields regarding which users are using licences for two applications we run. Lines which end with "(linger: 1800)" denote licence use for one application and lines which don't contain this denote licence use of the other.

     1      ccm_root phys-agsdev /dev/tty z001368 (v1.0) (Phys-agsdev/19353 481), start Fri 10/30 10:36 (linger: 1800)
     2      ccm_root phys-agsdev /dev/tty z600620 (v1.0) (Phys-agsdev/19353 2478), start Fri 10/30 10:36 (linger: 1800)
     3      ccm_root phys-agsdev /dev/tty z310084 (v1.0) (Phys-agsdev/19353 360), start Fri 10/30 10:37 (linger: 1800)
     4      ccm_root phys-agsdev /dev/tty z310338 (v1.0) (Phys-agsdev/19353 1074), start Fri 10/30 10:38 (linger: 1800)
     5      ccm_root phys-agsdev /dev/tty z001383 (v1.0) (Phys-agsdev/19353 1402), start Fri 10/30 10:42 (linger: 1800)
     6      ccm_root phys-agsdev /dev/tty ChangeAdmin (v1.0) (Phys-agsdev/19353 2155), start Fri 10/30 5:30 (linger: 1800)
     7      ccm_root phys-agsdev /dev/tty u112705 (v1.0) (Phys-agsdev/19353 1613), start Fri 10/30 10:44 (linger: 1800)
     8      ccm_root phys-agsdev /dev/tty z600969 (v1.0) (Phys-agsdev/19353 1885), start Fri 10/30 9:21
     9      ccm_root phys-agsdev /dev/tty z602084 (v1.0) (Phys-agsdev/19353 2379), start Fri 10/30 9:31
    10      ccm_root phys-agsdev /dev/tty z310044 (v1.0) (Phys-agsdev/19353 1279), start Fri 10/30 9:57
    11      ccm_root phys-agsdev /dev/tty z602138 (v1.0) (Phys-agsdev/19353 903), start Fri 10/30 10:02
    12      ccm_root phys-agsdev /dev/tty z310084 (v1.0) (Phys-agsdev/19353 720), start Fri 10/30 10:40
    13      ccm_root phys-agsdev /dev/pts/29 u414014 (v1.0) (Phys-agsdev/19353 580), start Fri 10/30 7:58
    14      ccm_root phys-agsdev /dev/tty u112705 (v1.0) (Phys-agsdev/19353 1991), start Fri 10/30 10:46

I need to be display fields $4, $8, $9, $11 and $12, which I have been able to do. The problem I have though, is that in my output, I need to be able to substitute "(linger: 1800)" with "ChangeBase" and for lines which don't contain "(linger: 1800)" I want to display "CMBase".

In addition to that, I need to read field $4 (user ID) of each line and perform a ypmatch command against it, so I can display the name of the user at the end of each line.

If anybody has any suggestions or pointers, it would be much appreciated.

Many thanks

Can you post the desired output?

something to start with...

nawk '
{
  if (match($0, "[(]linger: 1800[)]"))
    $0=substr($0,1,RSTART-1) "ChangeBase" substr($0, RSTART+RLENGTH+1)
  else
    $0=$0 OFS "CMBase"

  print $4, $8, $9, $11, $12
}' myFile

This is the desired output.

UserID              Process ID      Time Active          Licence Type   User Name
------              ----------      -----------          ------------   ---------
z310084             1079            start Fri 11:43      ChangeBase     J Bloggs
z004021             1618            start Fri 11:53      ChangeBase     A Bloggs
ChangeAdmin         2155            start Fri 5:30       ChangeBase     
z001698             1994            start Fri 12:02      ChangeBase     C Bloggs
z600969             1885            start Fri 9:21       CMBase         D Bloggs
oracled             2083            start Fri 10:51      CMBase         Oracle DBA
z001698             2380            start Fri 10:55      CMBase         F Bloggs
z311361             1393            start Fri 11:29      CMBase         G Bloggs
u414014             580             start Fri 7:58       CMBase         H Bloggs
z310086             1409            start Fri 11:44      CMBase         I Bloggs
 
Users of SYNERGY-ChangeBase: (Total of 11 licenses issued; Total of 4  licences in use)
Users of SYNERGY-CMBase: (Total of 12 licenses issued; Total of 6  licences in use)

I already have a shell script and a couple of awk scripts which worked fine and displayed all of the above with the exception of "User Name".

These scripts as follows:
Script cmsynergy_licence_admin.sh

LICENCEFILE=$HOME/cmsynergy_licences
LICENCE_COUNT_OUTPUT=$HOME/output_of_cmsynergy_licences
FORMATTED_LICENCE_COUNT=$HOME/formatted_licence_count
view_licences()
{
clear
ccm set role ccm_admin
LINENUM=1
/agsdev/continuus/flexnet/lmutil lmstat -a -c /agsdev/continuus/flexnet/license.dat > $LICENCEFILE
nawk -f /agsdev/continuus/ccm_root/bin/cmsynergy_count_licences.awk $LICENCEFILE > $LICENCE_COUNT_OUTPUT
sed 's/$/ CMBase/' $LICENCE_COUNT_OUTPUT > $FORMATTED_LICENCE_COUNT
nawk -F" " -f /agsdev/continuus/ccm_root/bin/cmsynergy_display_licences.awk $FORMATTED_LICENCE_COUNT
INC_BY=`nawk -F" " -f /agsdev/continuus/ccm_root/bin/cmsynergy_display_licences.awk $FORMATTED_LICENCE_COUNT | wc -l`
delete_temp_files
LINENUM=`echo "$LINENUM + $INC_BY -1" | bc`
print_line 1 "View username holding a licence...............[1]"
print_line 1 "Terminate a SYNERGY-CMBase licence............[2]"
print_line 1 "Return to Main Menu...........................[9]"
print_line 1 "Select an option [ ]"
tput cup $LINENUM 19
read V_OPTION
if [ -z "$V_OPTION" ]
then
V_OPTION=0
fi
case "$V_OPTION" in
'1')
print_line 1
print_line 1 "Enter the User ID: "
read USERNAME
print_line 1
ypmatch $USERNAME passwd | nawk -F: '{ print $5 }'
print_line 1
print_line 1 $BOLD_ON"Press return to continue..."$BOLD_OFF
read x
view_licences
;;
'2')
kill_process
;;
'9')
main_menu
;;
*)
view_licences
;;
esac
}
kill_process()
{
print_line 1 "Enter the Process ID of the SYNERGY-CMBase licence you wish to terminate: "
read PID
ask_yesno "ARE YOU SURE? y/n [ ]"
if [ $? -eq 0 ]
then
clear
LINENUM=1
/agsdev/continuus/flexnet/lmutil lmremove -c /agsdev/continuus/flexnet/license.dat -h SYNERGY-CMBase Phys-agsdev 19353 $PID
print_line 1
print_line 1 "Terminating licence associated with $PID..."
sleep 1
# print_line 1 $BOLD_ON"Press return to continue..."$BOLD_OFF
# read x
view_licences
else
echo
echo "Cancelling the termination of Process ID $PID..."
sleep 1
view_licences
fi
view_licences
}
delete_temp_files()
{
rm $LICENCEFILE
rm $LICENCE_COUNT_OUTPUT
rm $FORMATTED_LICENCE_COUNT

Script cmsynergy_display_licences.awk

BEGIN   {
        print ""
        print "User                 Process ID      Time Active          Licence Type"
        print "----                 ----------      -----------          ------------"
        }
NF > 12{changebaselicences++}
NF < 13{cmbaselicences++}
{
gsub(/\(/, " ", $0)
gsub(/\)/, " ", $0)
gsub(","," ", $0)
gsub("linger: 1800","ChangeBase", $0)
print $4, "     ", "  ", $7, "  ", "  ", $8, $9, $11, " ", $12
}
END     {
        printf "\nUsers of SYNERGY-ChangeBase: (Total of 11 licenses issued; Total of %-2d", changebaselicences
        print " licences in use)"
        printf "\nUsers of SYNERGY-CMBase: (Total of 12 licenses issued; Total of %-2d", cmbaselicences
        print " licences in use)" 
        print ""
        print "  NOTE: It is not possible to terminate SYNERGY-ChangeBase licences"
        print ""
        } 
awk -F[\ \)] '{print $4, $8,$10, $11, $13,($14~/linger/)?"ChangeBase":"CMBase"}' urfile
z001368 481 start Fri 10:36 ChangeBase
z600620 2478 start Fri 10:36 ChangeBase
z310084 360 start Fri 10:37 ChangeBase
z310338 1074 start Fri 10:38 ChangeBase
z001383 1402 start Fri 10:42 ChangeBase
ChangeAdmin 2155 start Fri 5:30 ChangeBase
u112705 1613 start Fri 10:44 ChangeBase
z600969 1885 start Fri 9:21 CMBase
z602084 2379 start Fri 9:31 CMBase
z310044 1279 start Fri 9:57 CMBase
z602138 903 start Fri 10:02 CMBase
z310084 720 start Fri 10:40 CMBase
u414014 580 start Fri 7:58 CMBase
u112705 1991 start Fri 10:46 CMBase
nawk '
        (NR == 1){
            print "UserID              Process ID      Time Active          Licence Type   User Name"
            print "------              ----------      -----------          ------------   ---------"
        }

        /\(linger: 1800\)/{ sub("(linger: 1800)", "ChangeBase", $0) }

        ($NF != "(linger: 1800)"){$0 = $0 OFS "CMBase" }
        {
                cmd = "cat /etc/passwd | grep "$4" | cut -d\":\" -f5"
                cmd | getline user
                printf("%-20s%-16s%-6s%-4s%-11s%-15s%-30s\n", $4,substr($7, p, length($7) -2),$8,$9,$11,$12,user)
        }
' infile

here's a little better version - a desired formatting is left as an exercise:

nawk '
  BEGIN {
    cmdYP="ypmatch"
    SEPyp=":"
  }
  {
    if (gsub("[(]linger: 1800[)]", "ChangeBase"))
      chb++
    else {
      $0=$0 OFS "CMBase"
      cmb++
    }
    cmd=cmdYP OFS $4 OFS "passwd"
    cmd | getline yp
    close(cmd)
    user=(split(yp, ypA, SEPyp))? ypA[5] : "unknown"

    print $4, $8, $9, $11, $12, user
  }
  END {
        printf("\nUsers of SYNERGY-ChangeBase: (Total of 11 licenses issued; Total of %-2d licences in use)\n", chb)
        printf("Users of SYNERGY-CMBase: (Total of 12 licenses issued; Total of %-2d licences in use)\n", cmb)
        print "  NOTE: It is not possible to terminate SYNERGY-ChangeBase licences\n"
  }' myFile

Thanks very much for everybody's replies.

I couldn't get this one to work... The initial error I got was:

awk: syntax error near line 1
awk: illegal statement near line 1

I then changed "awk" to "nawk" and got the following fields displayed:

    phys-agsdev CMBase
    phys-agsdev CMBase
    phys-agsdev CMBase
    phys-agsdev CMBase
    phys-agsdev CMBase
    phys-agsdev CMBase
    phys-agsdev CMBase
    phys-agsdev CMBase
    phys-agsdev CMBase
   ccm_root /dev/tty CMBase
   ccm_root /dev/tty CMBase
   ccm_root /dev/tty CMBase
   ccm_root /dev/pts/29 CMBase
   ccm_root /dev/tty CMBase

Will keep trying on Monday. Thanks

$ uname -i
SUNW,Sun-Fire-V445
$ awk -F[\ \)] '{print $4, $8,$10, $11, $13,($14~/linger/)?"ChangeBase":"CMBase"}' urfile
awk: syntax error near line 1
awk: illegal statement near line 1

But with nawk, I can run the command.

$ nawk -F[\ \)] '{print $4, $8,$10, $11, $13,($14~/linger/)?"ChangeBase":"CMBase"}' urfile
z001368 481 start Fri 10:36 ChangeBase
z600620 2478 start Fri 10:36 ChangeBase
z310084 360 start Fri 10:37 ChangeBase
z310338 1074 start Fri 10:38 ChangeBase
z001383 1402 start Fri 10:42 ChangeBase
ChangeAdmin 2155 start Fri 5:30 ChangeBase
u112705 1613 start Fri 10:44 ChangeBase
z600969 1885 start Fri 9:21 CMBase
z602084 2379 start Fri 9:31 CMBase
z310044 1279 start Fri 9:57 CMBase
z602138 903 start Fri 10:02 CMBase
z310084 720 start Fri 10:40 CMBase
u414014 580 start Fri 7:58 CMBase
u112705 1991 start Fri 10:46 CMBase
$