passing grepped date var into code


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers passing grepped date var into code
# 1  
Old 10-02-2012
passing grepped date var into code

Hi Everyone,
I am a bit new at learning this bash syntax. I have a problem at work that needs to be addressed. We find we are spending quite a bit of time killing old processes created by oracle replication that have been restarted later in the week. Because the replication takes time to complete (in days) I want to kill those processes that are 4 days and older.

I found this bit of code that will work just fine but I can’t seem to hook it up to my ps statement and I am at a loss as how to do this.
NOTE: the grep word “hald” is actually one of the linux processes. Something to work with.
There are two variables the first one (print $2) is the pid and the second is the date ($9). The numbers 2 and 9 are the columns numbered left to right. That output looks like this:
Code:
[root@localhost script]# ps aux | grep -i "hald" | awk '{print $2 "   " $9}'
1958   10:56
1959   10:56
1999   10:56
2004   10:56
4729   13:39

I am trying to pass in the date ($9) to the “todate” but am getting confused with the available formatting.
Once I get this to work returning the number of days then I can test it and issue the kill command for the associated pid ($2).

Running the code alone with the dates already there works fine.
The default date format is the following: Tue Oct 2 14:04:31 PDT 2012 . All I want is the day which is represented by date +%d and this returns 02. How can I represent $9 as date +%d ??
Any reference book recommended for reading would be appreciated.
Code:
#!/bin/sh
ps aux | grep -i "hald" | awk '{print $2 " " $9}'  
|
fromdate=01.04.2010
todate=24.05.2010
from=`echo $fromdate | awk  -F\. '{print $3$2$1}'`
to=`echo $todate | awk  -F\. '{print $3$2$1}'`
START_DATE=`date --date=$from +"%s"`
END_DATE=`date --date=$to +"%s"`
DAYS=$((($END_DATE - $START_DATE) / 86400 ))
echo $DAYS

Moderator's Comments:
Mod Comment Please use code tags, avoid odd font settings thanks.

Last edited by jim mcnamara; 10-02-2012 at 06:21 PM..
# 2  
Old 10-02-2012
Code:
#!/bin/sh
tdate=$(date +%Y%m%d)
psdate=$(ps aux | grep -i "hald" | grep -v grep | awk -v tdate=$tdate '$NF ~ /^hald$/ {if ($9 ~ ":") $9=tdate; print $9;}')
fromdate=01.04.2010
#todate=24.05.2010
from=`echo $fromdate | awk  -F\. '{print $3$2$1}'`
#to=`echo $todate | awk  -F\. '{print $3$2$1}'`
START_DATE=`date --date=$from +"%s"`
END_DATE=`date --date=$psdate +"%s"`
DAYS=$((($END_DATE - $START_DATE) / 86400 ))
echo $DAYS

# 3  
Old 10-03-2012
Good Morning,

Thank you for the quick response. All this bash scripting is new to me so I will go over in great detail what was just written to understand this code. I need to get this stuff in my head and the only way to do it for me is just to "do it". I appreciate you being a mentor.

I will let you know how it runs.

regards,

al

---------- Post updated at 07:20 AM ---------- Previous update was at 06:52 AM ----------

Good Morning,

I am reading the code over and I did some research on the portion | grep -v grep|. The formula is grep pattern filename1 filename2.

I can't seem to understand why this is done nor where the -v is coming from.

A brief explanation would be appreciated.

thanks

al
# 4  
Old 10-03-2012
Code:
ps aux | grep -i "hald"

Will print the grep in output.

-v : print all lines that don't match the pattern

try man grep
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Print the grepped string alongwith the filename

We have C shell and we are executing the below script: #!/bin/csh -f if ($#argv != 2) then echo "Usage $0 DirecotryPath Inputfilename" exit 1 endif set dir=$1 set fname=$2 echo $dir foreach line ( `cat $fname` ) echo \ ======================================== >>... (2 Replies)
Discussion started by: donisback
2 Replies

2. Shell Programming and Scripting

Passing PERL var values to SH Shell Script

Greetings all, If I have a SH script that calls a PERL script in the following way: perl $HOME/scripts/config.properties And in the config.properties PERL file, this Perl script only sets a number of environmental parameters in the following way: #!/usr/bin/perl $VAR1 = ( ... (3 Replies)
Discussion started by: gikyo12
3 Replies

3. Shell Programming and Scripting

How to get a number from a grepped sentence of a file?

I want get a number(ID) from a sentence which has been grepped from file using error number. For Example: #!/bin/ksh echo "Enter RRS ID: " read rrs echo "Enter error number:" read err scp -pr ptc-avdbamdw102:/home/icsprd/M3logs/Accurate/logs/corp_post/$rrs.*.err.txt $HOME/daemon_mail/... (7 Replies)
Discussion started by: JayDoshi
7 Replies

4. Shell Programming and Scripting

Passing date into awk...

Hi All.... I need to pass date into awk and parse logfile based on that.... i used both awk and /usr/xpg4/bin/awk... both are throwing up error..... So here is the stuff... when i use /usr/xpg4/bin/awk : DATE=`date '+%Y %b %d'` START=00 END=23 /usr/xpg4/bin/awk -v DATE={"$DATE"} -v... (3 Replies)
Discussion started by: Nithz
3 Replies

5. Shell Programming and Scripting

Replacing grepped text.

Dear Friends, I need your help once again. I have a flat file from which I have grepped something and kept in a separate file for some reason. I want to replace these grepped srtings with the strings in another file. E.g. Actual_file.txt COLOR: 33179 Lakme SEPT2011 Lot No: BR25324 ... (5 Replies)
Discussion started by: anushree.a
5 Replies

6. Shell Programming and Scripting

Passing CURL a date argument (formatting)

Im trying to pass curl a list of arguments... one of which is a date... When I run from the command line it works fine but when i try to run the same from a script passing variables it reformats the date for some reason and doesn't work. Example: curlstring=xxxxxxxxxxx.xxx.xxx:8090/csv/... (1 Reply)
Discussion started by: atelford
1 Replies

7. Solaris

SFTP passing variables date

Hi, Anyone can help me on how to solve my problem not getting the actual $DATE saying . Here my scripts; #!/bin/sh DATE='20110331' sftp -oUserKnownHostsFile=/.ssh/known_hosts -oIdentityFile=/.ssh/id_rsa -b /source/transfer.sh server1@sftp.com <<EOF #tranfer.sh put... (3 Replies)
Discussion started by: fspalero
3 Replies

8. Shell Programming and Scripting

Unix Script to email grepped results

I am following the tread number 81556 to grep a file and send the results in email. #/bin/bash /bin/grep -i 'invite sip' /var/log/asterisk/full if echo "found" | mail -s 'invite sip' mail@gmail.com When I just build it to grep and mail, it works fine. However, the if statement causes... (4 Replies)
Discussion started by: klysdale
4 Replies

9. Shell Programming and Scripting

passing date parameter in cronjob

Hi All, I have a crontab entry for every 5 min running. 5 * * * * /tmp/scripts/ksh/CsVues_H.sh `date +%Y%m%d_%H` >> /tmp/scripts/ksh/Cronresult.log and I am passing the date parameter. ( `date +%Y%m%d_%H` ) But the parameter values which i am expecting inside the script is... (6 Replies)
Discussion started by: scorpio
6 Replies

10. Shell Programming and Scripting

Passing Date as parameter with sh script

Hi I need to pass a date argument with my shell script. Can any one send me code for the same. eg For 15th Aug 2006. Iwant to pass parameter like this ./startenv.sh 08/15/2006. Everyday date argument must change. Will the below code work ? ./startenv.sh date '+%m/%d/%y' THanks... (1 Reply)
Discussion started by: pankajkrmishra
1 Replies
Login or Register to Ask a Question