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
# 1  
Old 02-13-2012
Shell script (KSH) to list ONLY the ID of male employees whose last loging time was during the last

Can someone help me on my issue please Smilie


I have a table like this format:

Code:
# cat myfile.txt
 
 Employee  Gender    NAME      Last Login 
    ID                                       Time        
-------------------------------------------------
210125       M         ABC        02/03/2012 08:07 
451235       F         EFG         02/012/2012 16:53  
241567       M         GHI         02/01/2012 14:03  
314584       M         JKL         02/02/2012 12:43

*****************

I need shell script (KSH) to list ONLY the ID of male employees whose last loging time was during the last two days.


the output should be list of IDs whic meet the requirements

Moderator's Comments:
Mod Comment Please use code tags. Please use a descriptive title for a thread:
https://www.unix.com/unix-dummies-que...om-forums.html

Last edited by Sara_84; 02-13-2012 at 08:33 AM.. Reason: more descriptive title
# 2  
Old 02-13-2012
Is the second line of your data typed correctly? I think we can guess that the date format is American mm/dd/yyyy but that line is different.

What Operating System and version do you have? Do you have the GNU "date" command?
How do you define "during the last two days"? Do you mean 48 hours from the time it is at the moment you run the script, or is this something to do with the date?

Are you trying to run the script as a one-off for today only or is this a regular requirement?
# 3  
Old 02-13-2012
I'm using AIX 6.1. yes I have the "date" command

the date format is American.

I mean by "during the last two days" from the the moment I run the script.

The purpose of the script to list every IDs of employees who logged in during the last two days.

Thaaanks Smilie
# 4  
Old 02-13-2012
MySQL

below script will provide you the expected output. It will work only for the same year.

Code:
#! /bin/bash

da_date="`date +%d`"
da_month=`date "+%m"`

 prev_date=0
 prev_mon=0
da_year=`date "+%Y"`
if [ "$da_date" -gt "1" ];then
  da_dat=`expr $da_date - 2`
  da_dat="""$da_dat"
else
  da_dat=1
  prev_mon=`expr $da_month - 1`
  prev_date=`cal $prev_mon $da_year | xargs -n1 | tail -1`
  prev_date="""$prev_date"
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 [ \( "$op_da" -gt "$da_dat" \) -a \( "$op_mo" -eq "$da_month" \) -a \( "$op_year" -eq "$da_year" \) ];then
 echo $line1 | awk '{print $1}'
fi

#If date is 1
if [ \( "$da_dat" -eq "1" \) -a \( \
 \( \( "$op_da" -eq "$prev_date" \) -a \( "$op_mo" -eq "$prev_mon" \) -a "$op_year" -eq "$da_year" \) -o \
\( \( "$op_da" -eq "1" \) -a  \( "$op_mo" -eq "$da_month" \) -a \( "$op_year" -eq "$da_year" \) \) \) ];then

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

fi
done < inp6

Input: inp6
 
210125 M ABC 02/13/2012 08:07
451235 F EFG 02/012/2012 16:53
241567 M GHI 02/12/2012 14:03
314584 M JKL 02/02/2012 12:43
Output
210125
241567


Thanks,
Kalai
This User Gave Thanks to kalpeer For This Post:
# 5  
Old 02-13-2012
If Perl is an option, then -

Code:
$
$ cat myfile.txt
 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
$
$ perl -lne 'BEGIN {use Time::Local; $prior = time-2*24*60*60}
             if (m|^(\d+)\s+([MF])\s+\w+\s+(\d+)/(\d+)/(\d+)\s+(\d+):(\d+)$|){
               ($id, $g, $mo, $d, $y, $h, $mi) = ($1, $2, $3, $4, $5, $6, $7);
               $y -= 1900; $mo--;
               $ts = timelocal(0,$mi,$h,$d,$mo,$y);
               print $id if $ts > $prior and $g eq "M";
             }' myfile.txt
123456
$
$

tyler_durden
This User Gave Thanks to durden_tyler For This Post:
# 6  
Old 02-14-2012
Quote:
Originally Posted by kalpeer
below script will provide you the expected output. It will work only for the same year.

Code:
#! /bin/bash
 
da_date="`date +%d`"
da_month=`date "+%m"`
 
 prev_date=0
 prev_mon=0
da_year=`date "+%Y"`
if [ "$da_date" -gt "1" ];then
  da_dat=`expr $da_date - 2`
  da_dat="""$da_dat"
else
  da_dat=1
  prev_mon=`expr $da_month - 1`
  prev_date=`cal $prev_mon $da_year | xargs -n1 | tail -1`
  prev_date="""$prev_date"
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 [ \( "$op_da" -gt "$da_dat" \) -a \( "$op_mo" -eq "$da_month" \) -a \( "$op_year" -eq "$da_year" \) ];then
 echo $line1 | awk '{print $1}'
fi
 
#If date is 1
if [ \( "$da_dat" -eq "1" \) -a \( \
 \( \( "$op_da" -eq "$prev_date" \) -a \( "$op_mo" -eq "$prev_mon" \) -a "$op_year" -eq "$da_year" \) -o \
\( \( "$op_da" -eq "1" \) -a  \( "$op_mo" -eq "$da_month" \) -a \( "$op_year" -eq "$da_year" \) \) \) ];then
 
 echo $line1 | awk '{print $1}'
fi
 
fi
done < inp6

Input: inp6
 
210125 M ABC 02/13/2012 08:07
451235 F EFG 02/012/2012 16:53
241567 M GHI 02/12/2012 14:03
314584 M JKL 02/02/2012 12:43
Output
210125
241567


Thanks,
Kalai
Thaaanks Kalai.. if you could please make comments on the code , so I can modify it.. the code is great but didnt work with me

---------- Post updated at 01:28 AM ---------- Previous update was at 01:27 AM ----------

Quote:
Originally Posted by durden_tyler
If Perl is an option, then -

Code:
$
$ cat myfile.txt
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
$
$ perl -lne 'BEGIN {use Time::Local; $prior = time-2*24*60*60}
            if (m|^(\d+)\s+([MF])\s+\w+\s+(\d+)/(\d+)/(\d+)\s+(\d+):(\d+)$|){
              ($id, $g, $mo, $d, $y, $h, $mi) = ($1, $2, $3, $4, $5, $6, $7);
              $y -= 1900; $mo--;
              $ts = timelocal(0,$mi,$h,$d,$mo,$y);
              print $id if $ts > $prior and $g eq "M";
            }' myfile.txt
123456
$
$

tyler_durden
thanks but unfortunately perl is not an option
# 7  
Old 02-14-2012
please provide me the sample input with more record
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