Unix shell scripting

Hi All,

I have one file called date1.txt and it contains dates like

130112
140112
150112
160112
170112
180112
190112
201012
 

so i need a script to read this file line by line and find out the day of each date and assign this value in one variable. And validate Weekday="Mon" then do something like that i need unix shell script.

Thanks for your help....

Thanks
Viswanathan

Welcome to the forum.

You might get lots of similar threads about reading a file in UNIX.
Please have a look at them.

In short,

while read line 
do
 #do whatever with each line
 echo the current line is "$line"
done < file

For your weekday requirement, refer to this thread

# cat yourdata
130112
140112
150112
160112
170112
180112
190112
201012
# ./calc yourdata
13-01-2012 -> This is a weekday = Fr
14-01-2012 -> This is a weekend = Sa
15-01-2012 -> This is a weekend = Su
16-01-2012 -> This is a weekday = Mo
17-01-2012 -> This is a weekday = Tu
18-01-2012 -> This is a weekday = We
19-01-2012 -> This is a weekday = Th
20-10-2012 -> This is a weekend = Sa
#!/bin/bash
days=(Su Mo Tu We Th Fr Sa)
weekday="2 3 4 5 6"
weekend="1 7"
enddays=$(echo "$weekend"|tr -d " ")
while read datex ; do
year=$(echo $datex|sed 's/.*\(..\)$/\1/')
month=$(echo $datex|sed 's/.*\(..\).*..$/\1/')
day=$(echo $datex|sed 's/\(..\).*/\1/')
century=$(date +%C) ## its equal 20 or colud be use direct that is (e.g., 20.)
dayc=$(cal $month ${century}$year|awk 'NR>1&&$0~/'$day'/{split($0,a);for(i in a)if($i~/'$day'/)print i}')
for i in $weekend $weekday ; do
if [[ $(echo $dayc|grep $i) ]] ; then
if [[ $(echo $i|grep "[$enddays]") ]] ; then
echo "$day-$month-${century}$year -> This is a weekend = ${days[$i-1]}"
else
echo "$day-$month-${century}$year -> This is a weekday = ${days[$i-1]}"
fi
fi
done
done<$1

regards
ygemici

 
$ perl test.pl test.txt                                                                                                                 
The day of the week for 130112 is: 5 (Fri)
The day of the week for 140112 is: 6 (Sat)
The day of the week for 150112 is: 0 (Sun)
The day of the week for 160112 is: 1 (Mon)
The day of the week for 170112 is: 2 (Tues)
The day of the week for 180112 is: 3 (Wed)
The day of the week for 190112 is: 4 (Thru)
The day of the week for 201012 is: 6 (Sat)

 
$ cat test.txt
130112
140112
150112
160112
170112
180112
190112
201012

 
$ cat test.pl
#!/bin/perl
use Time::Local;
open FH, "<$ARGV[0]" or die $!;
my @days = qw(Sun Mon Tues Wed Thru Fri Sat); 
while (my $date = <FH>) {
   chomp($date);
   my $mday = substr($date,0,2);
   my $mon = substr($date,2,2);
   my $year = substr($date,4,2);
   my $time = timelocal(0,0,0,$mday,$mon-1,$year);
   my $wday = (localtime($time))[6];
   print "The day of the week for $date is: $wday ($days[$wday])\n";
}

---------- Post updated at 03:33 PM ---------- Previous update was at 03:31 PM ----------

If you have a gnu date, then try this.

Not tested

 
while read date
do
     echo $(date -d "$date" +%a)
done < input.txt

Late to offer, but my solution

DAYS=SunMonTueWedThrFriSat
while read date 
do 
   echo ${DAYS:(( $(date +%w  -d "20${date:4}-${date:2:2}-${date:0:2} 00:00") *3)):3};
done <test.dat
Fri
Sat
Sun
Mon
Tue
Wed
Thr
Sat

EDIT Itkamaraj's %a advice for GNU date works if you shuffle the date into ISO format

 while read date 
do
     date +%a  -d "20${date:4}-${date:2:2}-${date:0:2} 00:00"; 
done <test.dat
1 Like