Unix shell scripting


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Unix shell scripting
# 1  
Old 01-13-2012
Unix shell scripting

Hi All,

I have one file called date1.txt and it contains dates like
Code:
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

Moderator's Comments:
Mod Comment Please use next time code tags For your code and data...

Last edited by vbe; 01-13-2012 at 05:10 AM..
# 2  
Old 01-13-2012
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,
Code:
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
# 3  
Old 01-13-2012
Code:
# cat yourdata
130112
140112
150112
160112
170112
180112
190112
201012

Code:
# ./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

Code:
#!/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
# 4  
Old 01-13-2012
Code:
 
$ 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

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

# 5  
Old 01-13-2012
Late to offer, but my solution
Code:
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
Code:
 while read date 
do
     date +%a  -d "20${date:4}-${date:2:2}-${date:0:2} 00:00"; 
done <test.dat


Last edited by Skrynesaver; 01-13-2012 at 06:29 AM.. Reason: formatted code
This User Gave Thanks to Skrynesaver For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

UNIX Shell Scripting

Describe in short the word completion feature of the tcsh Completion works anywhere in the command line, not at just the end, for both commands and filenames. Type part of a word and hit the Tab key, and the shell replaces the incomplete word with the complete one in the input buffer. The... (1 Reply)
Discussion started by: Elena Lauren
1 Replies

2. Programming

unix Shell scripting

Hi All, need help to complete the automation but stuck at a perticular situation below is the code <code> fixed_function_name { code.... code.... variable_map= { a="/a" b="/b" c="/c" so on... } (7 Replies)
Discussion started by: yadavricky
7 Replies

3. Shell Programming and Scripting

UNIX shell scripting

I am new to Unix.. Can someone please help me to understand the concept of Login shell and non login shell ? what exactly the difference between them :confused: (1 Reply)
Discussion started by: lokita jain
1 Replies

4. UNIX for Dummies Questions & Answers

Unix Shell Scripting

I'm sorry if this doesn't go here, but I'm in depserate need of help with my last unix homework. Anyways, I'm taking summer classes, and one of them is UNIX. I've understood everything thus far, but I'm having a killer time with how my instructor has worded the problems for shell scripting. I... (3 Replies)
Discussion started by: dw15
3 Replies

5. UNIX for Dummies Questions & Answers

Unix Shell Scripting( Calling from Unix to PLSQL)

Hello Experts, I have the following questions to be discussed here at this esteemed discussion forum. I have two Excel sheets which contain Unix Commands llike creating directory the structure/ftp/Copy/Zip etc to basically create an environment. I need help in understanding some of... (1 Reply)
Discussion started by: faizsaadq
1 Replies

6. Shell Programming and Scripting

Unix Shell Scripting

I 'm new to unix shell scripting can some one guide me to any e-book or link from where i can learn unix shell scripting .. i want to learn create interactive scripts for my day to day solaris work. Any help would be appreciated (1 Reply)
Discussion started by: fugitive
1 Replies

7. UNIX for Advanced & Expert Users

Need your Help on Unix Shell Scripting.........

Hi Friends, 1. Bash Shell Scrpt to take backup at evening 2. I need a bash shell script for killing all processes. (5 Replies)
Discussion started by: vinayraj
5 Replies

8. Shell Programming and Scripting

Unix Shell Scripting

Hi All, Greetings!! I am trying to write a script that will get me the syslog.log file output of last week... That is ...my cron will run on Monday and will get me the syslog output of previous week , last monday-last sunday. I tried using date formatting and tail..but did not succeed.... (4 Replies)
Discussion started by: premamadhuri
4 Replies

9. Shell Programming and Scripting

Unix shell scripting

Hi, we are writing this fields dynamically retrieved from database and writing into the file. $bmpRec = $bmpRec.'|'.$cust_id; # sp4 $bmpRec = $bmpRec.'|'.$serv_id; # sp5 $bmpRec = $bmpRec.'|'.$site_id; # sp6 $bmpRec = $bmpRec.'|'.$loc_id; # sp7 ... (4 Replies)
Discussion started by: Maruthi Kunnuru
4 Replies

10. Shell Programming and Scripting

difference between AIX shell scripting and Unix shell scripting.

please give the difference between AIX shell scripting and Unix shell scripting. (2 Replies)
Discussion started by: haroonec
2 Replies
Login or Register to Ask a Question