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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

shell scripts

Status
Not open for further replies.

djessi

IS-IT--Management
Dec 29, 2006
93
CM
Hi,
I would like to write to shell scripts :

1) to transform the date from DDMMYY to the standard date
like "fri feb 17 2007"

2) to extract the code of a country from the telephone number and appending that code after or before the number.
Like 33690875643 will give 33690875643 336 (france).
N.B The telephones numbers are stored in a file.

Thanks for help
Djessi
 
For #2: Provided the phone number is 1 per line in the file datafile:

Code:
!/bin/ksh

while read line
do
   prefix=$(echo "$line"|cut -c 1-3)
   echo $prefix
   var=${line}" "${prefix}
done < data.file
echo "$var"
 
It gets more difficult though - some country codes are 1 digit (USA/Canada = 1 NorthAmericanNumberingPlan, Russia = 7) some are 2 digits (France = 33 - not 336, the 6 is an area code, UK = 44) and most are 3 digits (e.g. Luxemburg = 352). There are even 4 digit dialling codes for international satellite phones...


HTH,

p5wizard
 
For 1), I'll give you a solution in pieces and all you have to do is build the date string. I'm assuming a date string in the format:

mydate="DDMMYY"

First, you need an algorithm for returning a day of the week from a date:

#!/bin/ksh

Code:
# this function returns the day of week where
# $1 - month
# $2 - day
# $3 - year
# from Mike Keith's
# [URL unfurl="true"]http://users.aol.com/s6sj7gt/mikecal.htm[/URL]
# returns 0=Sun, 1=Mon, etc
get_dow ()
{
# [x] means to truncate downward to the nearest integer
# dayofweek = ( [23m/9] + d + 4 + y + [z/4] - [z/100] + [z/400] - 2 (if m >= 3) ) mod 7
typeset -i DOW
typeset -i z
typeset -i multpr

if [ $1 -lt 3 ]
then # year determination
   z=$(($3-1))
else
   z=$3
fi
if [ $1 -ge 3 ]
then # set the multiplier
   multpr=2
else
   multpr=0
fi
DOW=$(( ((23 * $1/9) + $2 + 4 + $3 + ($z/4) - ($z/100) + ($z/400) - $multpr) % 7))

echo $DOW
}

Define the variables and call the function:

Code:
typeset -i mm
typeset -i dd
typeset -i yy

mydate="160207" # Feb 16, 2007, a Friday
# cut out the day, month, and year
dd=$(echo $mydate|cut -c 1-2)
mm=$(echo $mydate|cut -c 3-4)
yy=$(echo $mydate|cut -c 5-6)
#only dates past 2000
((yy+=2000))

# get day of the week, 0-6
# MAKE SURE DATE IS YYYY FORMAT
dow=$(get_dow $mm $dd $yy)

Determine the day string:
Code:
case $dow in
   0) day="Sun";;
   1) day="Mon";;
   2) day="Tues";;
   3) day="Wed";;
   4) day="Thurs";;
   5) day="Fri";;
   6) day="Sat";;
   *) day="error";;
esac

Determine the month string:
Code:
case $mm in
   1) month="Jan";;
   2) month="Feb";;
   3) month="Mar";;
   4) month="Apr";;
   5) month="May";;
   6) month="Jun";;
   7) month="Jul";;
   8) month="Aug";;
   9) month="Sep";;
  10) month="Oct";;
  11) month="Nov";;
  12) month="Dec";;
   *) month="error";;
esac
That should be all you need to build your date string
 
Thanks for your answers.
It's true the difficulty for code extraction is the fact that code lenghts are not the same.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top