Treating string as date ?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Treating string as date ?
# 1  
Old 02-15-2012
Treating string as date ?

Is there a way to treat a string as date and compare it to the current date?

lets assum inpu lik

Code:
$ cat myfile
 
Name   Last login
**************************
Sara      2/13/2012
kalpeer   2/15/2012
ygemici   2/14/2012


we want to display the name who logged in during the last # of days.

"`date +%D`" will give us the day of the year ( which is 043 for today)
is there a method to compare the dates in the input to the current day?

Last edited by Franklin52; 02-15-2012 at 08:35 AM.. Reason: Please use code tags for code and data samples, thank you
# 2  
Old 02-15-2012
MySQL

Hi,
Below command will give you the date in the format of 02/15/2012
Quote:
date "+%m/%d/%Y"
Check this thread.
https://www.unix.com/shell-programmin...ring-last.html
Thanks,
Kalai
# 3  
Old 02-15-2012
Quote:
Originally Posted by kalpeer
Hi,
Below command will give you the date in the format of 02/15/2012
Check this thread.
https://www.unix.com/shell-programmin...ring-last.html
Thanks,
Kalai
Thanks Kalai, but my aim is not get this format. It is to read the string in the input file as a date and compare it to the current actual date.
# 4  
Old 02-15-2012
Something like this:

Code:
#! /bin/bash

while read x y
do
    if [ `date -d "$y" +%j` -le `date +%j` ]
    then
        echo "Less than or equal to curr date"
    else
        echo "Greater than curr date"
    fi
done < myfile

This User Gave Thanks to balajesuri For This Post:
# 5  
Old 02-15-2012
You could parse the date like this

Code:
$ cat compare_dates.ksh
#!/bin/ksh

USERDATE="2/15/2012"
IFS=/
set -- $USERDATE
typeset -Z2 MONTH=$1
typeset -Z2 DAY=$2
YEAR=$3
MYDATE="$YEAR$MONTH$DAY"
echo $MYDATE

TODAY=$(date +%Y%m%d)

if [ $TODAY -eq $MYDATE ]; then
     echo "We have a match!"
fi

exit 0

$ ./compare_dates.ksh
20120215
We have a match!

Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Converting String Date into UNIX Date

Hi, I have a string date to my unix script(sun solaris). I wanted to convert it into unix date so that I can use it in a conditional statement. Please see below: MyTest.sh -s 2018-05-09 suppdt=$1 # string date passed via arguement as 2018-04-09 curryr=`date '+%Y'` nextyr=`expr... (2 Replies)
Discussion started by: Saanvi1
2 Replies

2. Shell Programming and Scripting

AIX to RHEL migration - awk treating 0e[0-9]+ as 0 instead of string issue

Greetings Experts, We are migrating from AIX to RHEL Linux. I have created a script to verify and report the NULLs and SPACEs in the key columns and duplicates on key combination of "|" delimited set of big files. Following is the code that was successfully running in AIX. awk -F "|" 'BEGIN {... (5 Replies)
Discussion started by: chill3chee
5 Replies

3. Shell Programming and Scripting

Compare Date with String in date format

Hi, I am new to this forum, searched, but for some reason did not find much help, so a new thread. I am trying to compare date with a string which is in date format , but for some reason, system does not give me the right result. Date is coming in a file and i am comparing the same with... (2 Replies)
Discussion started by: mkstool
2 Replies

4. Shell Programming and Scripting

awk - treating remaining columns as one

Hi all, For no particular reason, I would like to use awk on a file that contains multiple columns, but let's say only columns 1 and 2 have some text values, and the remainder of the line contains text that I would like to treat as one column, considering I have spaces as delimiter for the... (33 Replies)
Discussion started by: ppucci
33 Replies

5. Shell Programming and Scripting

Treating Strings with spaces

I have a file list.txt which has a list of file names with spaces between the file names like /emptydir/file 1 how do i browse through the list.txt displaying the filenames. Almost all the file names in list.txt have space between them.This file list.txt is formed by using the find statement to... (5 Replies)
Discussion started by: kinny
5 Replies

6. Homework & Coursework Questions

Date comparison with 'string date having slashes and time zone' in Bash only

1. The problem statement, all variables and given/known data: I have standard web server log file. It contains different columns (like IP address, request result code, request type etc) including a date column with the format . I have developed a log analysis command line utility that displays... (1 Reply)
Discussion started by: TariqYousaf
1 Replies

7. Shell Programming and Scripting

TRAP treating

Hi, I'm looking for a script that receives the traps from a windows machine and treate them. For exemple just write a line in a file on UNIX server. Can you help me ? Thank you. (2 Replies)
Discussion started by: big123456
2 Replies

8. Shell Programming and Scripting

treating special chars

Hi, I need some advise on treating non printable chars over ascii value 126 Case 1 : On some fields in the text , I need to retiain then 'as-is' and load to a database.I understand it also depends on database codepage. but i just wanna know how do i ensure it do not change while loading... (1 Reply)
Discussion started by: braindrain
1 Replies
Login or Register to Ask a Question