Substituting field contents using AWK


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Substituting field contents using AWK
# 1  
Old 10-30-2009
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.

Code:
     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
# 2  
Old 10-30-2009
Can you post the desired output?
# 3  
Old 10-30-2009
something to start with...
Code:
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

# 4  
Old 10-30-2009
This is the desired output.
Code:
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
Code:
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
Code:
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 ""
        }

# 5  
Old 10-30-2009
Code:
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


Last edited by rdcwayx; 10-30-2009 at 11:57 AM..
# 6  
Old 10-30-2009
Code:
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


Last edited by Franklin52; 11-05-2009 at 05:54 AM.. Reason: Adding code tags
# 7  
Old 10-30-2009
here's a little better version - a desired formatting is left as an exercise:
Code:
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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Problem with getting awk to multiply a field by a value set based on condition of another field

Hi, So awk is driving me crazy on this one. I have searched everywhere and read man, docs and every related post Google can find and still no luck. The actual files I need to run this on are sensitive in nature, but it is the same thing as if I needed to calculate weighted grades for multiple... (15 Replies)
Discussion started by: cotilloe
15 Replies

2. Shell Programming and Scripting

awk to change contents of field based on condition in same file

In the awk below I am trying to copy the entire contents of $6 there may be multiple values seperated by a ;, to $8, if $8 is . (lines 1 and 3 are examples). If that condition $8 is not . (line2 is an example) then that line is skipped and printed as is. The awk does execute but prints the output... (3 Replies)
Discussion started by: cmccabe
3 Replies

3. Shell Programming and Scripting

awk to update field using matching value in file1 and substring in field in file2

In the awk below I am trying to set/update the value of $14 in file2 in bold, using the matching NM_ in $12 or $9 in file2 with the NM_ in $2 of file1. The lengths of $9 and $12 can be variable but what is consistent is the start pattern will always be NM_ and the end pattern is always ;... (2 Replies)
Discussion started by: cmccabe
2 Replies

4. Shell Programming and Scripting

awk to update value in field of out file using contents of another Ask

In the out.txt below I am trying to use awk to update the contents of $9.. If $9 contains a + or - then $8 of out.txt is used as a key to lookup in $2 of file. When a match ( there will always be one) is found the $3 value of that file is used to update $9 of out.txt separated by a :. So the... (6 Replies)
Discussion started by: cmccabe
6 Replies

5. Shell Programming and Scripting

AWK: Pattern match between 2 files, then compare a field in file1 as > or < field in file2

First, thanks for the help in previous posts... couldn't have gotten where I am now without it! So here is what I have, I use AWK to match $1 and $2 as 1 string in file1 to $1 and $2 as 1 string in file2. Now I'm wondering if I can extend this AWK command to incorporate the following: If $1... (4 Replies)
Discussion started by: right_coaster
4 Replies

6. UNIX for Dummies Questions & Answers

substituting variable value in AWK

Hi All, my requirement is as below. I need to replace a value in a particular column with a substitution variable(date value) and modified value of the current column value in the same position. for ex. i have a record like 02;aaaa;bbbbb;cccccc;dddddd;123456789;hhhhh;12hs;asdf ;... (3 Replies)
Discussion started by: ganesh_248
3 Replies

7. Shell Programming and Scripting

awk, comma as field separator and text inside double quotes as a field.

Hi, all I need to get fields in a line that are separated by commas, some of the fields are enclosed with double quotes, and they are supposed to be treated as a single field even if there are commas inside the quotes. sample input: for this line, 5 fields are supposed to be extracted, they... (8 Replies)
Discussion started by: kevintse
8 Replies

8. Shell Programming and Scripting

Substituting variable value in AWK /start/,/stop/

Hi all u brilient people on the forum... I am trying to call the variable value in awk command for search pattern /start/,/stop/ but i am nt able to do this .... wat i did is ..i have created two variable YESTERDAY and TODAY and passed the y'day n 2'days dates in it...like this ... (14 Replies)
Discussion started by: whomi
14 Replies

9. Shell Programming and Scripting

Join file contents via common field

I have 2 files with a common parm - Jobname File 1 0507 1202 JOBA 0507 1302 JOBB 0507 1452 JOBC 0507 1552 JOBA 0507 1553 JOBA File2 JOBA abcdefg server4 JOBB defghij server22 JOBC vwxyz12 server55 I would like to take each line from File1 and match the jobname with the jobname... (8 Replies)
Discussion started by: Northerner
8 Replies

10. Shell Programming and Scripting

Remove spaces from first field, and write entire contents into other text file

Hi all, I have searched and found various threads about removing spaces from a field within a text file. Unfortunately, I have not found exactly what I'm looking for, nor am I adept enough to modify what I've found into what I need. I use the following command to remove the first line... (3 Replies)
Discussion started by: carriehoff
3 Replies
Login or Register to Ask a Question