Shell script (KSH) to list ONLY the ID of male employees whose last loging time was during the last


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell script (KSH) to list ONLY the ID of male employees whose last loging time was during the last
# 8  
Old 02-14-2012
Quote:
Originally Posted by kalpeer
please provide me the sample input with more record
Thanks , here is more records of my input..

Code:
Employee  Gender    NAME      Last Login
    ID                                       Time
-------------------------------------------------
210125       M         ABC         02/03/2012 08:07
451235       F         EFG         02/02/2012 16:53
241567       M         GHI         02/01/2012 14:03
314584       M         JKL         02/02/2012 12:43
123456       M         MNO         02/12/2012 00:00
654321       F         PQR         02/12/2012 00:00
210120       M         ZVF         02/03/2012 08:07
451230       F         UNM         02/02/2012 16:53
241560       M         IOP         02/05/2012 14:03
314580       M         UOP         02/04/2012 12:43
123450       M         MNN         02/12/2012 00:00
654320       F         PQP         02/11/2012 00:00


Last edited by Scott; 02-14-2012 at 06:17 AM.. Reason: Use code tags, please...
# 9  
Old 02-14-2012
@Sara,

The right way to do it would be to pass an SQL statement directly at database level to extract the information you need and spool it to a file.
# 10  
Old 02-14-2012
Quote:
Originally Posted by ctsgnb
@Sara,

The right way to do it would be to pass an SQL statement directly at database level to extract the information you need and spool it to a file.
I'm not using SQL
# 11  
Old 02-14-2012
MySQL

i done a little modification to my script and display the last two days and today report
Code:
#! /bin/bash

#retrieve current date and month
da_date="`date +%d`"
#da_date="2"
da_month=`date "+%m"`
#da_month="02"

 prev_date=0
 prev_mon=0
da_year=`date "+%Y"`
# Check  whether date is above 3
if [ "$da_date" -ge "3" ];then
  da_dat=`expr $da_date - 3`
  da_dat="""$da_dat"
else
 # If date is 2, it display the record of 2,1 and last day of previous month
 if  [ "$da_date" -eq "2" ];then
  da_dat=2
  prev_mon=`expr $da_month - 1`
  prev_date=`cal $prev_mon $da_year | xargs -n1 | tail -1`
  prev_date="""$prev_date"
 else
 # If date is 1, it display the record of 1 and last two days of previous month
   da_dat=1
  prev_mon=`expr $da_month - 1`
  prev_date=`cal $prev_mon $da_year | xargs -n1 | tail -2| head -1`
  prev_date="""$prev_date"

  fi
fi

while read line1
do
line=`echo $line1 |grep -v Employee  |  awk '{if($2=="M") print $4}'`
if [ ! -z "$line" ];then
op_da=`echo $line | awk -F/ '{print $2}'`
op_mo=`echo $line | awk -F/ '{print $1}'`
op_year=`echo $line | awk -F/ '{print $3}'`

#if date is 3 and above following block will be executed
if [ \( \( "$da_dat" -eq "`expr $op_da - 3`" \) -o  \( "$da_dat" -eq "`expr $op_da - 2`" \) -o  \( "$da_dat" -eq "`expr $op_da - 1`" \) \) \
 -a \( "$op_mo" -eq "$da_month" \) -a \( "$op_year" -eq "$da_year" \) -a \( "$prev_date" -eq "0" \)  ];then
 echo $line1 | awk '{print $1}'
 #echo $line1
fi

#if date is 2 or 1 following block will be executed
if [ \( "$da_dat" -lt "3" \) -a \( \
 \( \( "$op_da" -ge "$prev_date" \) -a \( "$op_mo" -eq "$prev_mon" \) -a "$op_year" -eq "$da_year" \) -o \
\( \( "$op_da" -le "$da_dat" \) -a  \( "$op_mo" -eq "$da_month" \) -a \( "$op_year" -eq "$da_year" \) \) \) ];then

 echo $line1 | awk '{print $1}'
 #echo $line1
fi

fi
done < inp6

Input: inp6
Code:
Employee  Gender    NAME      Last Login
    ID                                       Time
-------------------------------------------------
210125       M         ABC         02/03/2012 08:07
451235       F         EFG         02/02/2012 16:53
241567       M         GHI         02/01/2012 14:03
314584       M         JKL         02/02/2012 12:43
123456       M         MNO         02/12/2012 00:00
654321       F         PQR         02/12/2012 00:00
210120       M         ZVF         02/03/2012 08:07
451230       F         UNM         02/02/2012 16:53
241560       M         IOP         02/05/2012 14:03
314580       M         UOP         02/04/2012 12:43
123450       M         MNN         02/12/2012 00:00
654320       F         PQP         02/11/2012 00:00
65430       M         PQP         01/30/2012 00:00
65432       M         PQP         01/31/2012 00:00
245432       M         PQP         01/29/2012 00:00

When i execute the script i got the below output.
Output
Quote:
123456
123450
Today date is 02/14/2012.
It displayed the record from 02/12/2012 to 02/14/2012.

123456 M MNO 02/12/2012 00:00
123450 M MNN 02/12/2012 00:00


Thanks,
Kalai
This User Gave Thanks to kalpeer For This Post:
# 12  
Old 02-14-2012
if your os support "date '+%s'" format,you can try this Smilie
Code:
# cat file1
Employee ID Gender    NAME          Last Login Time
-------------------------------------------------
210125       M         ABC         02/03/2012 08:07
451235       F         EFG         02/12/2012 16:53
241567       M         GHI         02/01/2012 14:03
314584       M         JKL         02/02/2012 12:43

Code:
# ./justdoit file1 2
451235       F         EFG         02/12/2012 16:53

Code:
# ./justdoit file1 12
210125       M         ABC         02/03/2012 08:07
451235       F         EFG         02/12/2012 16:53

Code:
# cat justdoit
#!/usr/bin/ksh
## justdoit @ygemici unix.com simple date calculator ##
tmpf='times.tmp.bak1X';file="$1";day=$2
awk '/^[0-9]/{if(!f)f=NR;b[c++]=$(NF-1);split($NF,a,":");aa[x++]=a[1]*60*60+a[2]*60}
END{for(j=0;j<c;j++){system("date '+%s' -d" b[j]);printf "%d %d %s\n", aa[xx++],f++,FILENAME}
}' $file|sed 'N;s/\n/ /'|awk -vtmpf=$tmpf 'BEGIN{printf "">tmpf}{print $1+$2,$3,$4>>tmpf}'
awk -vnow=$(date '+%s') -vt=$day '{if($1<now&&$1>now-t*24*60*60)system("awk NR=="$2"{print} " $NF)}' $tmpf

regards
ygemici

Last edited by ygemici; 02-15-2012 at 04:50 AM..
This User Gave Thanks to ygemici For This Post:
# 13  
Old 02-14-2012
Code:
# DEADLINE contains J-2 in a YYYYMMDDHHMM format

DEADLINE=$(perl -e 'printf "%s\n",scalar(localtime(time - 2*86400 ))' | awk -F"[ :]" 'BEGIN{
split("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec",d," ")}{
for(i=0;++i<=12;) if (d[i]==$2) m=i}{printf "%s%02d%s\n",$NF,m,$3 $4 $5}')

Code:
# Then build the date in YYYYMMDDHHMM from your file then performs check
# if M and last log within DEADLINE the || NR<4 is to ensure that the header is also displayed

awk -v d="$DEADLINE" '{split($4,l,"/");split($5,t,":")}$2=="M"&&((l[3]l[1]l[2]t[1]t[2])+0>d)||NR<4' yourfile

Code:
$ cat tst
Employee  Gender    NAME      Last Login
    ID                                       Time
-------------------------------------------------
210125       M         ABC         02/03/2012 08:07
451235       F         EFG         02/02/2012 16:53
241567       M         GHI         02/01/2012 14:03
314584       M         JKL         02/02/2012 12:43
123456       M         MNO         02/12/2012 23:00
654321       F         PQR         02/12/2012 20:10
210120       M         ZVF         02/03/2012 08:07
451230       F         UNM         02/02/2012 16:53
241560       M         IOP         02/05/2012 14:03
314580       M         UOP         02/04/2012 12:43
123450       M         MNN         02/13/2012 00:00
654320       F         PQP         02/11/2012 00:00
$ date
mardi 14 février 2012, 19:24:45 (UTC+0100)
$ DEADLINE=$(perl -e 'printf "%s\n",scalar(localtime(time - 2*86400 ))' | awk -F"[ :]" 'BEGIN{
> split("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec",d," ")}{
> for(i=0;++i<=12;) if (d[i]==$2) m=i}{printf "%s%02d%s\n",$NF,m,$3 $4 $5}')
$ echo $DEADLINE
201202121924
$ awk -v d="$DEADLINE" '{split($4,l,"/");split($5,t,":")}$2=="M"&&((l[3]l[1]l[2]t[1]t[2])+0>d)||NR<4' tst
Employee  Gender    NAME      Last Login
    ID                                       Time
-------------------------------------------------
123456       M         MNO         02/12/2012 23:00
123450       M         MNN         02/13/2012 00:00


Last edited by ctsgnb; 02-14-2012 at 03:00 PM..
This User Gave Thanks to ctsgnb For This Post:
# 14  
Old 02-15-2012
Wrench

Quote:
Originally Posted by kalpeer
i done a little modification to my script and display the last two days and today report


Thanks,
Kalai

Thaaanks darling Smilie

the code is working great

But it seems that dealing with dates is a bit hard and confusing. the code is working fine but when I try to understand it and modify it to to run on a different table , it did not work Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need Help in ksh Script to list files older than 365 days from specified directories

Requirement is to list the files older than 365 days from multiple directories and delete them and log the list of files which are deleted to a log file. so 1 script should only list files older than 365 days for each directory separately to a folder The other script should read these files... (7 Replies)
Discussion started by: prasadn
7 Replies

2. UNIX for Dummies Questions & Answers

Help with ksh script to list files, cp it to another UNIX server

Hi, I'm quite new to ksh scripting, can someone help me with this. Requirements: I need to create a script that list the files from a user input date range. e. g. format of file: *c1*log.2012-12-22-14-00* *c1*log.2012-12-22-14-00* *c1*log.2012-12-22-14-00*... (7 Replies)
Discussion started by: chococrunch6
7 Replies

3. UNIX for Advanced & Expert Users

Help with ksh script to list, then cp files from a user input date range

Hi, I'm quite new to ksh scripting, can someone help me with this. Requirements: I need to create a script that list the files from a user input date range. e. g. format of file: *c1*log.2012-12-22-14-00* *c1*log.2012-12-22-14-00* *c1*log.2012-12-22-14-00*... (1 Reply)
Discussion started by: chococrunch6
1 Replies

4. Shell Programming and Scripting

ksh script throwing arg list too long for mv cp wc - everything

i have a ksh script which internally calls another ksh script. this inner script has simple commands like shown in the code window. In the script im trying to do a mv - it fails with arg list too long. then i try to perform cp and cat - and both are failing with similar error. :wall: How is... (4 Replies)
Discussion started by: nyc68
4 Replies

5. Shell Programming and Scripting

Help with ksh Shell Script

My goal is to create a script that will check if in a test or production environment. I wrote this script to check $host variable to check which server I'm on but this script does not work. if then BASE=/home/fmtest; export BASE else BASE=/home/fmprod; export BASE fi ... (5 Replies)
Discussion started by: Bperl1967
5 Replies

6. Shell Programming and Scripting

time calculation in ksh script

I"m trying to calculate the duration of of backup within a ksh shell script but I get an error. #!/bin/ksh STTIM=`date '+%T'` EDTIM=`date '+%T'` .... .... echo "DURATION OF BACKUP: $((EDTIM - STTIM))" (5 Replies)
Discussion started by: Bperl1967
5 Replies

7. Shell Programming and Scripting

Shell script to convert epoch time to real time

Dear experts, I have an epoch time input file such as : - 1302451209564 1302483698948 1302485231072 1302490805383 1302519244700 1302492787481 1302505299145 1302506557022 1302532112140 1302501033105 1302511536485 1302512669550 I need the epoch time above to be converted into real... (4 Replies)
Discussion started by: aismann
4 Replies

8. Shell Programming and Scripting

what does this ksh shell script do?

Can someone tell me when the script is called, what does it do? I can't see it is going to run anything. (1 Reply)
Discussion started by: dp100022
1 Replies

9. Shell Programming and Scripting

Help with ksh shell script

I am using /usr/bin/ksh in AIX I am reading the values of $dbname, $dbatmpdir/dbdir.$$, and $scope from a different file All I have to do is check if $dbname exists in file $dbatmpdir/dbdir.$$ and $scope should have a value either 'TABLE' or 'SCHEMA'. When I execute the following code. I am... (3 Replies)
Discussion started by: tenderfoot
3 Replies

10. UNIX for Dummies Questions & Answers

Unix Shell Scripting -- update employees not present in input file

ALL, My shell script takes a employee file as input. I have to identify the list of employees not in the input file and update their status in the database. Approach I followed: by traversing through the input file add all the emplid's to a variable. update the status of employees not in... (2 Replies)
Discussion started by: sailussr
2 Replies
Login or Register to Ask a Question