Unable to convert date into no. using date -d +%s syntax in ksh shell


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Unable to convert date into no. using date -d +%s syntax in ksh shell
# 8  
Old 06-06-2012
What is the script in post #7 intended to do? Hard to guess from uncommented faulty code.
Are you trying to find out the age of each file or something?
# 9  
Old 06-07-2012
Hi Methyl,
I have made the function which assigns numeric value to each mon.
then i am comparing the date of each file with todays date and i intend to gunzip all the files which are not of todays date.

comparison code i have removed for now,i m just tryin to print month date and filenames but its givin error related to arrays.
below is code for gunzipping files which is removed.

code :

Code:
v1=`ls -lrt | awk '{print $6,$7,$8,$9}'| egrep -v "gz$" |  egrep "MDCR.*" | wc -l`
v2=`date | awk '{print $2}'`
v3=`date | awk '{print $3}'`
datecalc $v2
v4=$?
i=1
while [ $i -le $v1 ]
do
A[$i]=`ls -lrt | awk '{print $6,$7,$8,$9}'| egrep -v "gz$" |  egrep "MDCR.*" | awk '{print $1}' | head -$i | tail -1`
B[$i]=`ls -lrt | awk '{print $6,$7,$8,$9}'| egrep -v "gz$" |  egrep "MDCR.*" | awk '{print $2}' | head -$i | tail -1`
C[$i]=`ls -lrt | awk '{print $9}'| egrep -v "gz$" |  egrep "MDCR.*" | awk '{print $1}' | head -$i | tail -1`
x=${A[$i]}
datecalc $x
v5=$?
y=${B[$i]}
if [ $v5 -lt $v4 ]
then
gzip ${A[$i]}
elif [ $v5 = $v4 && $y -lt $v3 ]
then
gzip ${A[$i]}
fi
i=`expr $i + 1`
done

Moderator's Comments:
Mod Comment Please use [code]...[/code] tags. Video tutorial on how to use them


---------- Post updated 06-07-12 at 03:53 AM ---------- Previous update was 06-06-12 at 10:22 AM ----------

Hi All,

Any luck with above queries.

Last edited by Scrutinizer; 06-06-2012 at 12:46 PM.. Reason: code tags
# 10  
Old 06-07-2012
The code posted in post #9 does not tie up with the explanation. It appears to be code to gzip files rather than to gunzip files.
Are you actually trying to gzip files which are not dated today and which have not already been zipped?

Need to understand about your directory structure. I notice that you have ls -lar. Does this mean that there are files in subdirectories or would ls -la give exactly the same output?

Are you going to run this script manually or from cron. If it is to be run from cron daily, what time will it run?

I think that the script as posted does not work if any file is dated last year (which changes the format of ls -la). Consider what would happen if the script ran on Jan 1st .

Let's clear up what the script is meant to do, then look at a total rewrite.
# 11  
Old 06-07-2012
Hi Methyl,
sorry for typo ,the code is for gzipping the files.
ls -lart gives same o/p as ls -lrt

absolutely corrct ,i am trying to gzip files older than today which are of a certain pattern.
I will be setting a cron for this which will run at 2300 hrs everyday.
My script comapres the month first with todays date,then the day.
this script will fail on 1st jan as jan is assigned a no. 1 which is less than 12(dec),
so all files for dec 31st will no get gzipped.

also Please find the full code for sun OS 5.8

Code:
 
datecalc()
{
if [ $1 = "Jan" ]
then
return 1;
elif  [ $1 = "Feb" ]
then
return 2;
elif  [ $1 = "Mar" ]
then
return 3;
elif  [ $1 = "Apr" ]
then
return 4;
elif [ $1 = "May" ]
then
return 5;
elif  [ $1 = "Jun" ]
then
return 6;
elif  [ $1 = "Jul" ]
then
return 7;
elif  [ $1 = "Aug" ]
then
return 8;
elif [ $1 = "Sep"
then
return 9;
elif  [ $1 = "Oct" ]
then
return 10;
elif  [ $1 = "Nov" ]
then
return 11;
elif  [ $1 = "Dec" ]
then
return 12;
fi
}
v1=`ls -lrt | awk '{print $6,$7,$8,$9}'| egrep -v "gz$" |  egrep "MDCR.*" | wc -l`
v2=`date | awk '{print $2}'`
v3=`date | awk '{print $3}'`
datecalc $v2
v4=$?
i=1
while [ $i -le $v1 ]
do
A[$i]=`ls -lrt | awk '{print $6,$7,$8,$9}'| egrep -v "gz$" |  egrep "MDCR.*" | awk '{print $1}' | head -$i | tail -1`
B[$i]=`ls -lrt | awk '{print $6,$7,$8,$9}'| egrep -v "gz$" |  egrep "MDCR.*" | awk '{print $2}' | head -$i | tail -1`
C[$i]=`ls -lrt | awk '{print $9}'| egrep -v "gz$" |  egrep "MDCR.*" | awk '{print $1}' | head -$i | tail -1`
x=${A[$i]}
datecalc $x
v5=$?
y=${B[$i]}
if [ $v5 -lt $v4 ]
then
gzip ${A[$i]}
elif [ $v5 = $v4 && $y -lt $v3 ]
then
gzip ${A[$i]}
fi
i=`expr $i + 1`
done

thanks
jcpratap
# 12  
Old 06-07-2012
Total rewrite using find rather than ls .
Create a reference file timestamped at the start of day and then look for files of the right name which are older than the reference file.

Code:
YYYYMMDD="`date +%Y%m%d`"       # Reversed date yyyymmdd
REFERENCE=/tmp/reference.${YYYYMMDD}.$$
touch -t ${YYYYMMDD}0000 ${REFERENCE}   # First thing today
#
cd /CP2_logs/wls21/SAS/MDCR/Applicationlogs/WLS13
find . -type f \( -name 'MDCR.*' -a ! -name '*.gz' -a ! -newer ${REFERENCE} \) -print | while read FILENAME
do
                # Remove echo when tested
                echo gzip "${FILENAME}"
done
rm -f "${REFERENCE}"

# 13  
Old 06-07-2012
Hi Methyl,

THANKS A LOT.

very lucid and effecient code,learned new things from this code.

thanks
jcpratap
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Print start date to end date, given $1 & $2 in ksh

Dear all, I have an user passing 2 parameter 31/03/2015 and 02/04/2015 to a ksh script. How to print the start date to end date. Expected output is : 31/03/2015 01/04/2015 02/04/2015 Note : 1. Im using aix and ksh 2. I have tried to convert the given input into a date, didnt... (0 Replies)
Discussion started by: mr.rajaravi
0 Replies

2. Shell Programming and Scripting

Convert a date stored in a variable to epoch date

I am not able to pass date stored in a variable as an argument to date command. I get current date value for from_date and to_date #!/usr/bin/ksh set -x for s in server ; do ssh -T $s <<-EOF from_date="12-Jan-2015 12:02:09" to_date="24-Jan-2015 13:02:09" echo \$from_date echo... (7 Replies)
Discussion started by: raj48
7 Replies

3. Shell Programming and Scripting

Convert date into days using shell script

Hi, I have date format in an excel sheet as 07/03/2014 11:21. I need to check this date with current date and need to find whether it is more than 30 days or not. I know how to current date into days, but someone please help me to change the date in the excel sheet to days, so that i can check... (2 Replies)
Discussion started by: Arasu123
2 Replies

4. Shell Programming and Scripting

Convert date arg to sql date

Doing a bcp load to sybase and need to convert datearg which comes in as 20130501 to 2013-05-01 which is the best way to do this (4 Replies)
Discussion started by: tasmac
4 Replies

5. Shell Programming and Scripting

How to convert string(variable) into date( epoch) in ksh on HPUX machine?

Hi all, I have used a bash script which ultimately converts a string into date using date --date option: DATE=$DATE" "$TIME" "`date +%Y` //concatenating 2 strings TMRW_DATE=`date --date="$DATE" +"%s"` //applying date command on string and getting the unixtime Please use code tags... (7 Replies)
Discussion started by: Rashu123
7 Replies

6. UNIX for Dummies Questions & Answers

Unable to add date to current date.

Hi, I am trying to display future date from the current date but unable to do so in UNIX (not in PERL). For eg: if today is March 5 then I want a variable wherein I can store Mar 7 date, but unable to get the future date from the current date. I have tried many possible ways as mentioned below... (11 Replies)
Discussion started by: amit.mathur08
11 Replies

7. Shell Programming and Scripting

ksh compare dates INSIDE a file (ie date A is > date B)

In KSH, I am pasting 2 almost identical files together and each one has a date and time on each line. I need to determine if the first instance of the date/time is greater than the 2nd instance of the date/time. If the first instance is greater, I just need to echo that line. I thought I would... (4 Replies)
Discussion started by: right_coaster
4 Replies

8. Shell Programming and Scripting

convert Julian date to calender date

Hi, I have script in unix which creates a julian date like 126 or 127 I want convert this julian date into calender date ex : input 127 output 07/may/2007 or 07/05/2007 or 07/05/07 rgds srikanth (6 Replies)
Discussion started by: srikanthus2002
6 Replies

9. Shell Programming and Scripting

Newbie convert date ksh

If I have start = 02282006; end = 03152006; How do I get startdate = 02/28/2006; enddate = 03/15/2006; Thanks, (3 Replies)
Discussion started by: britney
3 Replies
Login or Register to Ask a Question