Home > front end >  Awk help required
Awk help required

Time:05-12

I wrote the below script to check expiry age of users on psql

#!/bin/bash 
  echo "" 
  echo "Performing User Expiry Check" 
  echo ""
  CONTAINER_ID=$(docker ps -a | grep -i "postgresql" | awk '{print $1}')
  sudo docker exec -i $CONTAINER_ID psql -h localhost -p 5432 -U dbuser
-c "select u.email, now() - to_timestamp(c.created_date/1000)::date password_age from credential c join user_entity u on u.id=c.user_id where now() - to_timestamp (c.created_date/1000)::date > interval '0 days' and c.type='password'" | grep -v "-" | grep -A 5000 -v "email" | grep -v "rows"

This is the result when i run the script.

root@eserver-app-04:/home/dbuserr# ./testkc.sh

Performing  User Expiry Check

[email protected] | 16 days 12:59:19.519782
[email protected] | 16 days 12:59:19.519782

root@eserver-app-04:/home/dbuserr#

The below is working fine.

| awk '{ if ($3 0>60) { print "Major - User Expires in 30 days" } else { print "No Users set to Expire" } } ' >> /home/dbuser/userexpire.txt

I get this output written to the file

No Users set to Expire.

My Question is. How do i add another 2 statements to awk ?

i have

| awk '{ if ($3 0>60) { print "Major - User Expires in 30 days" }

i would like to add

if ($3 0>80) { print "Critical - User Expires in 10 days" }

and

if ($3 0<60) { print "Notice - No Users set to Expire" }

How do i add them all together is the below correct ?

| awk '{ if ($3 0>60) { print "Major - User Expires in 30 days" } { elif ($3 0>80) { print "Critical - User Expires in 10 days" } elif ($3 0<60) { print "Notice - No Users set to Expire" } } ' } ' >> /home/dbuser/userexpire.txt

CodePudding user response:

It's not necessary to put all the commands of your script on the command line. You can put them in a file.

Put this all in a file called "userexpire.awk"

{
    if ($3 0>60) {
        print "Major - User Expires in 30 days"
    }
    else if ($3 0>80) {
        print "Critical - User Expires in 10 days"
    }
    else if ($3 0<60) {
        print "Notice - No Users set to Expire"
    }
}

And then execute it by doing

... whatever ... | awk -f userexpire.awk

The "-f userexpire.awk" tells awk to look in that file for the script instead of the command line.

This makes things much easier to write, test and debug.

CodePudding user response:

Definitively made it too complicated :

mawk/gawk 'BEGIN {
    split((_=    "Notice - No Users set to Expire=")(_)(\
                  "Major - User Expires in 30 days="\
               "Critical - User Expires in 10 days"),___,"[=] ")
        ____ = (__=index(___[ _]=_,"t")) * (__ __—-) __  

} $  NF=___[int((-(/[1-9][0][ \t] days/) $__)/____)]'


                  Notice  -   No    Users            set       to  Expire=
[email protected]    |       1   days  12:59:19.519782  Notice    -   No       Users    set  to  Expire=
[email protected]  |       1   days  12:59:19.519782  Notice    -   No       Users    set  to  Expire=
[email protected]    |       2   days  12:59:19.519782  Notice    -   No       Users    set  to  Expire=
[email protected]  |       2   days  12:59:19.519782  Notice    -   No       Users    set  to  Expire=
[email protected]    |       3   days  12:59:19.519782  Notice    -   No       Users    set  to  Expire=
[email protected]  |       3   days  12:59:19.519782  Notice    -   No       Users    set  to  Expire=
[email protected]    |       4   days  12:59:19.519782  Notice    -   No       Users    set  to  Expire=
[email protected]  |       4   days  12:59:19.519782  Notice    -   No       Users    set  to  Expire=
[email protected]    |       5   days  12:59:19.519782  Notice    -   No       Users    set  to  Expire=
[email protected]  |       5   days  12:59:19.519782  Notice    -   No       Users    set  to  Expire=
[email protected]    |       6   days  12:59:19.519782  Notice    -   No       Users    set  to  Expire=
[email protected]  |       6   days  12:59:19.519782  Notice    -   No       Users    set  to  Expire=
[email protected]    |       7   days  12:59:19.519782  Notice    -   No       Users    set  to  Expire=
[email protected]  |       7   days  12:59:19.519782  Notice    -   No       Users    set  to  Expire=
[email protected]    |       8   days  12:59:19.519782  Notice    -   No       Users    set  to  Expire=
[email protected]  |       8   days  12:59:19.519782  Notice    -   No       Users    set  to  Expire=
[email protected]    |       9   days  12:59:19.519782  Notice    -   No       Users    set  to  Expire=
[email protected]  |       9   days  12:59:19.519782  Notice    -   No       Users    set  to  Expire=
[email protected]    |       10  days  12:59:19.519782  Notice    -   No       Users    set  to  Expire=
[email protected]  |       10  days  12:59:19.519782  Notice    -   No       Users    set  to  Expire=
[email protected]    |       11  days  12:59:19.519782  Notice    -   No       Users    set  to  Expire=
[email protected]  |       11  days  12:59:19.519782  Notice    -   No       Users    set  to  Expire=
[email protected]    |       12  days  12:59:19.519782  Notice    -   No       Users    set  to  Expire=
[email protected]  |       12  days  12:59:19.519782  Notice    -   No       Users    set  to  Expire=
[email protected]    |       13  days  12:59:19.519782  Notice    -   No       Users    set  to  Expire=
[email protected]  |       13  days  12:59:19.519782  Notice    -   No       Users    set  to  Expire=
[email protected]    |       14  days  12:59:19.519782  Notice    -   No       Users    set  to  Expire=
[email protected]  |       14  days  12:59:19.519782  Notice    -   No       Users    set  to  Expire=
[email protected]    |       15  days  12:59:19.519782  Notice    -   No       Users    set  to  Expire=
[email protected]  |       15  days  12:59:19.519782  Notice    -   No       Users    set  to  Expire=
[email protected]    |       16  days  12:59:19.519782  Notice    -   No       Users    set  to  Expire=
[email protected]  |       16  days  12:59:19.519782  Notice    -   No       Users    set  to  Expire=
[email protected]    |       17  days  12:59:19.519782  Notice    -   No       Users    set  to  Expire=
[email protected]  |       17  days  12:59:19.519782  Notice    -   No       Users    set  to  Expire=
[email protected]    |       18  days  12:59:19.519782  Notice    -   No       Users    set  to  Expire=
[email protected]  |       18  days  12:59:19.519782  Notice    -   No       Users    set  to  Expire=
[email protected]    |       19  days  12:59:19.519782  Notice    -   No       Users    set  to  Expire=
[email protected]  |       19  days  12:59:19.519782  Notice    -   No       Users    set  to  Expire=
[email protected]    |       20  days  12:59:19.519782  Notice    -   No       Users    set  to  Expire=
[email protected]  |       20  days  12:59:19.519782  Notice    -   No       Users    set  to  Expire=
[email protected]    |       21  days  12:59:19.519782  Notice    -   No       Users    set  to  Expire
[email protected]  |       21  days  12:59:19.519782  Notice    -   No       Users    set  to  Expire
[email protected]    |       22  days  12:59:19.519782  Notice    -   No       Users    set  to  Expire
[email protected]  |       22  days  12:59:19.519782  Notice    -   No       Users    set  to  Expire
[email protected]    |       23  days  12:59:19.519782  Notice    -   No       Users    set  to  Expire
[email protected]  |       23  days  12:59:19.519782  Notice    -   No       Users    set  to  Expire
[email protected]    |       24  days  12:59:19.519782  Notice    -   No       Users    set  to  Expire
[email protected]  |       24  days  12:59:19.519782  Notice    -   No       Users    set  to  Expire
[email protected]    |       25  days  12:59:19.519782  Notice    -   No       Users    set  to  Expire
[email protected]  |       25  days  12:59:19.519782  Notice    -   No       Users    set  to  Expire
[email protected]    |       26  days  12:59:19.519782  Notice    -   No       Users    set  to  Expire
[email protected]  |       26  days  12:59:19.519782  Notice    -   No       Users    set  to  Expire
[email protected]    |       27  days  12:59:19.519782  Notice    -   No       Users    set  to  Expire
[email protected]  |       27  days  12:59:19.519782  Notice    -   No       Users    set  to  Expire
[email protected]    |       28  days  12:59:19.519782  Notice    -   No       Users    set  to  Expire
[email protected]  |       28  days  12:59:19.519782  Notice    -   No       Users    set  to  Expire
[email protected]    |       29  days  12:59:19.519782  Notice    -   No       Users    set  to  Expire
[email protected]  |       29  days  12:59:19.519782  Notice    -   No       Users    set  to  Expire
[email protected]    |       30  days  12:59:19.519782  Notice    -   No       Users    set  to  Expire
[email protected]  |       30  days  12:59:19.519782  Notice    -   No       Users    set  to  Expire
[email protected]    |       31  days  12:59:19.519782  Notice    -   No       Users    set  to  Expire
[email protected]  |       31  days  12:59:19.519782  Notice    -   No       Users    set  to  Expire
[email protected]    |       32  days  12:59:19.519782  Notice    -   No       Users    set  to  Expire
[email protected]  |       32  days  12:59:19.519782  Notice    -   No       Users    set  to  Expire
[email protected]    |       33  days  12:59:19.519782  Notice    -   No       Users    set  to  Expire
[email protected]  |       33  days  12:59:19.519782  Notice    -   No       Users    set  to  Expire
[email protected]    |       34  days  12:59:19.519782  Notice    -   No       Users    set  to  Expire
[email protected]  |       34  days  12:59:19.519782  Notice    -   No       Users    set  to  Expire
[email protected]    |       35  days  12:59:19.519782  Notice    -   No       Users    set  to  Expire
[email protected]  |       35  days  12:59:19.519782  Notice    -   No       Users    set  to  Expire
[email protected]    |       36  days  12:59:19.519782  Notice    -   No       Users    set  to  Expire
[email protected]  |       36  days  12:59:19.519782  Notice    -   No       Users    set  to  Expire
[email protected]    |       37  days  12:59:19.519782  Notice    -   No       Users    set  to  Expire
[email protected]  |       37  days  12:59:19.519782  Notice    -   No       Users    set  to  Expire
[email protected]    |       38  days  12:59:19.519782  Notice    -   No       Users    set  to  Expire
[email protected]  |       38  days  12:59:19.519782  Notice    -   No       Users    set  to  Expire
[email protected]    |       39  days  12:59:19.519782  Notice    -   No       Users    set  to  Expire
[email protected]  |       39  days  12:59:19.519782  Notice    -   No       Users    set  to  Expire
[email protected]    |       40  days  12:59:19.519782  Notice    -   No       Users    set  to  Expire
[email protected]  |       40  days  12:59:19.519782  Notice    -   No       Users    set  to  Expire
[email protected]    |       41  days  12:59:19.519782  Notice    -   No       Users    set  to  Expire
[email protected]  |       41  days  12:59:19.519782  Notice    -   No       Users    set  to  Expire
[email protected]    |       42  days  12:59:19.519782  Notice    -   No       Users    set  to  Expire
[email protected]  |       42  days  12:59:19.519782  Notice    -   No       Users    set  to  Expire
[email protected]    |       43  days  12:59:19.519782  Notice    -   No       Users    set  to  Expire
[email protected]  |       43  days  12:59:19.519782  Notice    -   No       Users    set  to  Expire
[email protected]    |       44  days  12:59:19.519782  Notice    -   No       Users    set  to  Expire
[email protected]  |       44  days  12:59:19.519782  Notice    -   No       Users    set  to  Expire
[email protected]    |       45  days  12:59:19.519782  Notice    -   No       Users    set  to  Expire
[email protected]  |       45  days  12:59:19.519782  Notice    -   No       Users    set  to  Expire
[email protected]    |       46  days  12:59:19.519782  Notice    -   No       Users    set  to  Expire
[email protected]  |       46  days  12:59:19.519782  Notice    -   No       Users    set  to  Expire
[email protected]    |       47  days  12:59:19.519782  Notice    -   No       Users    set  to  Expire
[email protected]  |       47  days  12:59:19.519782  Notice    -   No       Users    set  to  Expire
[email protected]    |       48  days  12:59:19.519782  Notice    -   No       Users    set  to  Expire
[email protected]  |       48  days  12:59:19.519782  Notice    -   No       Users    set  to  Expire
[email protected]    |       49  days  12:59:19.519782  Notice    -   No       Users    set  to  Expire
[email protected]  |       49  days  12:59:19.519782  Notice    -   No       Users    set  to  Expire
[email protected]    |       50  days  12:59:19.519782  Notice    -   No       Users    set  to  Expire
[email protected]  |       50  days  12:59:19.519782  Notice    -   No       Users    set  to  Expire
[email protected]    |       51  days  12:59:19.519782  Notice    -   No       Users    set  to  Expire
[email protected]  |       51  days  12:59:19.519782  Notice    -   No       Users    set  to  Expire
[email protected]    |       52  days  12:59:19.519782  Notice    -   No       Users    set  to  Expire
[email protected]  |       52  days  12:59:19.519782  Notice    -   No       Users    set  to  Expire
[email protected]    |       53  days  12:59:19.519782  Notice    -   No       Users    set  to  Expire
[email protected]  |       53  days  12:59:19.519782  Notice    -   No       Users    set  to  Expire
[email protected]    |       54  days  12:59:19.519782  Notice    -   No       Users    set  to  Expire
[email protected]  |       54  days  12:59:19.519782  Notice    -   No       Users    set  to  Expire
[email protected]    |       55  days  12:59:19.519782  Notice    -   No       Users    set  to  Expire
[email protected]  |       55  days  12:59:19.519782  Notice    -   No       Users    set  to  Expire
[email protected]    |       56  days  12:59:19.519782  Notice    -   No       Users    set  to  Expire
[email protected]  |       56  days  12:59:19.519782  Notice    -   No       Users    set  to  Expire
[email protected]    |       57  days  12:59:19.519782  Notice    -   No       Users    set  to  Expire
[email protected]  |       57  days  12:59:19.519782  Notice    -   No       Users    set  to  Expire
[email protected]    |       58  days  12:59:19.519782  Notice    -   No       Users    set  to  Expire
[email protected]  |       58  days  12:59:19.519782  Notice    -   No       Users    set  to  Expire
[email protected]    |       59  days  12:59:19.519782  Notice    -   No       Users    set  to  Expire
[email protected]  |       59  days  12:59:19.519782  Notice    -   No       Users    set  to  Expire
[email protected]    |       60  days  12:59:19.519782  Notice    -   No       Users    set  to  Expire
[email protected]  |       60  days  12:59:19.519782  Notice    -   No       Users    set  to  Expire

[email protected]    |       61  days  12:59:19.519782  Major     -   User     Expires  in   30  days
[email protected]  |       61  days  12:59:19.519782  Major     -   User     Expires  in   30  days
[email protected]    |       62  days  12:59:19.519782  Major     -   User     Expires  in   30  days
[email protected]  |       62  days  12:59:19.519782  Major     -   User     Expires  in   30  days
[email protected]    |       63  days  12:59:19.519782  Major     -   User     Expires  in   30  days
[email protected]  |       63  days  12:59:19.519782  Major     -   User     Expires  in   30  days
[email protected]    |       64  days  12:59:19.519782  Major     -   User     Expires  in   30  days
[email protected]  |       64  days  12:59:19.519782  Major     -   User     Expires  in   30  days
[email protected]    |       65  days  12:59:19.519782  Major     -   User     Expires  in   30  days
[email protected]  |       65  days  12:59:19.519782  Major     -   User     Expires  in   30  days
[email protected]    |       66  days  12:59:19.519782  Major     -   User     Expires  in   30  days
[email protected]  |       66  days  12:59:19.519782  Major     -   User     Expires  in   30  days
[email protected]    |       67  days  12:59:19.519782  Major     -   User     Expires  in   30  days
[email protected]  |       67  days  12:59:19.519782  Major     -   User     Expires  in   30  days
[email protected]    |       68  days  12:59:19.519782  Major     -   User     Expires  in   30  days
[email protected]  |       68  days  12:59:19.519782  Major     -   User     Expires  in   30  days
[email protected]    |       69  days  12:59:19.519782  Major     -   User     Expires  in   30  days
[email protected]  |       69  days  12:59:19.519782  Major     -   User     Expires  in   30  days
[email protected]    |       70  days  12:59:19.519782  Major     -   User     Expires  in   30  days
[email protected]  |       70  days  12:59:19.519782  Major     -   User     Expires  in   30  days
[email protected]    |       71  days  12:59:19.519782  Major     -   User     Expires  in   30  days
[email protected]  |       71  days  12:59:19.519782  Major     -   User     Expires  in   30  days
[email protected]    |       72  days  12:59:19.519782  Major     -   User     Expires  in   30  days
[email protected]  |       72  days  12:59:19.519782  Major     -   User     Expires  in   30  days
[email protected]    |       73  days  12:59:19.519782  Major     -   User     Expires  in   30  days
[email protected]  |       73  days  12:59:19.519782  Major     -   User     Expires  in   30  days
[email protected]    |       74  days  12:59:19.519782  Major     -   User     Expires  in   30  days
[email protected]  |       74  days  12:59:19.519782  Major     -   User     Expires  in   30  days
[email protected]    |       75  days  12:59:19.519782  Major     -   User     Expires  in   30  days
[email protected]  |       75  days  12:59:19.519782  Major     -   User     Expires  in   30  days
[email protected]    |       76  days  12:59:19.519782  Major     -   User     Expires  in   30  days
[email protected]  |       76  days  12:59:19.519782  Major     -   User     Expires  in   30  days
[email protected]    |       77  days  12:59:19.519782  Major     -   User     Expires  in   30  days
[email protected]  |       77  days  12:59:19.519782  Major     -   User     Expires  in   30  days
[email protected]    |       78  days  12:59:19.519782  Major     -   User     Expires  in   30  days
[email protected]  |       78  days  12:59:19.519782  Major     -   User     Expires  in   30  days
[email protected]    |       79  days  12:59:19.519782  Major     -   User     Expires  in   30  days
[email protected]  |       79  days  12:59:19.519782  Major     -   User     Expires  in   30  days
[email protected]    |       80  days  12:59:19.519782  Major     -   User     Expires  in   30  days
[email protected]  |       80  days  12:59:19.519782  Major     -   User     Expires  in   30  days

[email protected]    |       81  days  12:59:19.519782  Critical  -   User     Expires  in   10  days
[email protected]  |       81  days  12:59:19.519782  Critical  -   User     Expires  in   10  days
[email protected]    |       82  days  12:59:19.519782  Critical  -   User     Expires  in   10  days
[email protected]  |       82  days  12:59:19.519782  Critical  -   User     Expires  in   10  days
[email protected]    |       83  days  12:59:19.519782  Critical  -   User     Expires  in   10  days
[email protected]  |       83  days  12:59:19.519782  Critical  -   User     Expires  in   10  days
[email protected]    |       84  days  12:59:19.519782  Critical  -   User     Expires  in   10  days
[email protected]  |       84  days  12:59:19.519782  Critical  -   User     Expires  in   10  days
[email protected]    |       85  days  12:59:19.519782  Critical  -   User     Expires  in   10  days
[email protected]  |       85  days  12:59:19.519782  Critical  -   User     Expires  in   10  days
[email protected]    |       86  days  12:59:19.519782  Critical  -   User     Expires  in   10  days
[email protected]  |       86  days  12:59:19.519782  Critical  -   User     Expires  in   10  days
[email protected]    |       87  days  12:59:19.519782  Critical  -   User     Expires  in   10  days
[email protected]  |       87  days  12:59:19.519782  Critical  -   User     Expires  in   10  days
[email protected]    |       88  days  12:59:19.519782  Critical  -   User     Expires  in   10  days
[email protected]  |       88  days  12:59:19.519782  Critical  -   User     Expires  in   10  days
  • Related