Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help for Script

Status
Not open for further replies.

arunux

IS-IT--Management
Jul 26, 2002
10
0
0
CA
Hi,
I want to grip the lines based on date from last command

echo "enter the date:"
read date
last |grep -i "$date"

The above grep command giving correct output for double digit dates (fro 10-30) but for single digit date (1-9) how can i use read the date, coz in last out put single digit date is right aliened and there is no zero.

Arun
Arun
Sun Solarix admin
India
 
Here's an old script (includes / between the parts of the date, though - You'd have to edit 'em out and amend 'cut' positions)

read INPUT
SL1=`echo ${INPUT} | cut -c3,3`
SL2=`echo ${INPUT} | cut -c6,6`
if [ "${SL1}" = "/" -a "${SL2}" = "/" ]
then
continue
else
echo '\nDate must be dd/mm/yy please\n\n'
ERROR=1
exit ${ERROR}
fi
if [ ${#INPUT} -eq 8 ]
then
YEAR=`echo ${INPUT} | cut -c7,8`
elif [ ${#INPUT} -eq 10 ]
then
YEAR=`echo ${INPUT} | cut -c9,10`
else
echo '\nDate must be dd/mm/yy (8 chars) please\n\n'
ERROR=1
exit ${ERROR}
fi

DATE=`echo ${INPUT} | cut -c1,2`
MONTH=`echo ${INPUT} | cut -c4,5`

if [ ${MONTH} -gt 12 -o ${MONTH} -lt 1 ]
then
echo '\nEnter a Month between 01 - 12 please\n\n'
ERROR=1
exit ${ERROR}
fi
case $MONTH in
01|03|05|07|08|10|12)

if [ ${DATE} -gt 31 -o ${DATE} -lt 1 ]
then
echo '\nEnter a Day between 01 - 31 please\n\n'
ERROR=1
exit ${ERROR}
fi
;;
04|06|09|11)
if [ ${DATE} -gt 30 -o ${DATE} -lt 1 ]
then
echo '\nEnter a Day between 01 - 30 please\n\n'
ERROR=1
exit ${ERROR}
fi
;;
02)
MODU=`expr ${YEAR} % 4`
if [ ${MODU} -eq 0 ]
then
if [ ${DATE} -gt 29 -o ${DATE} -lt 1 ]
then
echo '\nEnter a Day between 01 - 29 please\n\n'
ERROR=1
exit ${ERROR}
fi
else
if [ ${DATE} -gt 28 -o ${DATE} -lt 1 ]
then
echo '\nEnter a Day between 01 - 28 please\n\n'
ERROR=1
exit ${ERROR}
fi
fi
;;
esac

HTH ;-) Dickie Bird

Honi soit qui mal y pense
 
No validation on days in month performed here:

[tt]#!/bin/ksh

# assuming input of form m-d-y

echo "Enter date: \c"
read INPDATE

MM=`echo ${INPDATE} | awk -F'-' '{print $1}'`
DD=`echo ${INPDATE} | awk -F'-' '{print $2}'`
YY=`echo ${INPDATE} | awk -F'-' '{print $3}'`

printf "%02i-%02i-%04i\n" ${MM} ${DD} ${YY}[/tt]

Things to change if needed:
if your separating is / instead of -, change the - in the -F's to the awks, and in the format field of printf

if you don't want leading zeros in the output, removing the zeros in the format field of printf

if you want dd/mm/yy input/output, adjust order of input processing and output as necessary.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top