Date to Day in loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Date to Day in loop
# 1  
Old 01-15-2013
Date to Day in loop

Hi All,
I have a file in the following format.I need to pick up 25th field which is a date and convert it into a day and add it as a field in the file.

Code:
"AAGENAS,PEARL L"|"S00834219"|"131186147D"|"SUP"|"600"|""|"00001236BIME"|DIAMOND|"PROVIDER|"NC|"20110824"|"733.90"|"V76.12"|"786.2"|"490"|"627.2"|""|""|""|""|"6125"|"20110827"|"20110826"|"20110826"|"N|"6125"|"Prev Rad:Bone Density(MS)"|"P"|"P"|"I"
"AARENS,MIMI"|"S00462243"|"110262446A"|"H2256"|""|""|"00001293B3IV"|DIAMOND|"PROVIDER|"C|"20111020"|"786.2"|""|""|""|""|""|""|""|""|"6120"|"20111021"|"20111021"|"20111022"|"N|"6120"|"Radiology-MF POS"|"P"|"P"|"I"
"AARENS,MIMI"|"S00462243"|"110262446A"|"H2256"|""|"GEOGRAPHIC LOCATION INCONVENIENT"|"00001293B3IV"|DIAMOND|"PROVIDER|"C|"20111020"|"786.2"|""|""|""|""|""|""|""|""|"6120"|"20111021"|"20111021"|"20111021"|"N|"6120"|"Radiology-MF POS"|"P"|"P"|"I"

I have the code which converts date to day in perl, but not sure how to put this in the loop.

Code:
perl -e '
		use POSIX qw(strftime);
		$fmt = "%A";  # %a = abbreviated weekday %u = weekday number	
		$mday = substr("$ARGV[0]", 6, 2);
		$mon =  substr("$ARGV[0]", 4 ,2);
		$year = substr("$ARGV[0]", 0 ,4);
		$weekday =
		  strftime($fmt, 0, 0, 0, $mday , $mon - 1, $year - 1900, -1, -1, -1);
		print "$weekday";
		' 20110827

Also since the file has thousands of records , I am not sure if perl is the right way to go.

Any help will be appreciated.

Desired format of the file

Code:
"AAGENAS,PEARL L"|"S00834219"|"131186147D"|"SUP"|"600"|""|"00001236BIME"|DIAMOND|"PROVIDER|"NC|"20110824"|"733.90"|"V76.12"|"786.2"|"490"|"627.2"|""|""|""|""|"6125"|"20110827"|"20110826"|"20110826"|"Friday"|"N|"6125"|"Prev Rad:Bone Density(MS)"|"P"|"P"|"I"
"AARENS,MIMI"|"S00462243"|"110262446A"|"H2256"|""|""|"00001293B3IV"|DIAMOND|"PROVIDER|"C|"20111020"|"786.2"|""|""|""|""|""|""|""|""|"6120"|"20111021"|"20111021"|"20111022"|"Saturday"|"N|"6120"|"Radiology-MF POS"|"P"|"P"|"I"
"AARENS,MIMI"|"S00462243"|"110262446A"|"H2256"|""|"GEOGRAPHIC LOCATION INCONVENIENT"|"00001293B3IV"|DIAMOND|"PROVIDER|"C|"20111020"|"786.2"|""|""|""|""|""|""|""|""|"6120"|"20111021"|"20111021"|"20111021"|"Friday"|"N|"6120"|"Radiology-MF POS"|"P"|"P"|"I"

# 2  
Old 01-15-2013
You can use perl's <> operator to read each line from stdin into perl's builtin $_ variable...then parse $_ to extract the fields and process them as required.
Code:
while (<>) {
    # parse $_
    ...
    # process the extracted fields
    ...
}

# 3  
Old 01-15-2013
Thanks shamrock! Is there a way of doing this in shell or awk since its thousands of records in the file.
# 4  
Old 01-15-2013
Quote:
Originally Posted by nua7
Thanks shamrock! Is there a way of doing this in shell or awk since its thousands of records in the file.
Even using the shell or awk to do this you will have to process 1 line at a time...there is no way around that...besides i'd prefer perl for date time math.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Replace date in file every day with current date

I Have text like XXX_20190908.csv.gz need to replace Only date in this format with current date every day Thanks! (1 Reply)
Discussion started by: yamasani1991
1 Replies

2. Shell Programming and Scripting

Need to minus one day from a date given

Hello Folks, I have a variable output holding date as below - output = "20141220" I need to extract a day out of it and store it in another variable i.e. something similar to below - output1=20141219" and if the month is changing i.e. date in on 31st or 1st it should be taken care of "date... (5 Replies)
Discussion started by: ektubbe
5 Replies

3. Shell Programming and Scripting

Adding one day to a date

Hi All, I want to add one day to a date and store it in a variable. From GUI we are passing value (last day of the month)to $t_date. This $t_date will give me the value like this %Y%m%d 20150531. Now I want to add one day to this value and store it in a variable "datein". datein should... (8 Replies)
Discussion started by: ROCK_PLSQL
8 Replies

4. AIX

Need to get the next day's date of the user entered date

I need to get the next day's date of the user entered date for example: Enter date (yyyy/mm/yy): 2013/10/08I need to get the next day's date of the user entered date Desired Output: 2013/10/09Though there are ways to achieve this is Linux or Unix environment (date command) ,I need to... (1 Reply)
Discussion started by: rpm120
1 Replies

5. Shell Programming and Scripting

To Get the day of given date in aix

Hi, Can any one help to find out the day for the given date in AIX. If we giving date as "YYYYMMDD" it should display its day. eg:if the input is "20120103", expected output is "tuesday" :wall: Pls help (7 Replies)
Discussion started by: novaothers
7 Replies

6. Shell Programming and Scripting

finding the previous day date and creating a file with date

Hi guys, I had a scenario... 1. I had to get the previous days date in yyyymmdd format 2. i had to create a file with Date inthe format yyyymmdd.txt format both are different thanks guys in advance.. (4 Replies)
Discussion started by: apple2685
4 Replies

7. UNIX for Dummies Questions & Answers

Getting date -1 day not using GNU date

It's easy as pie to get the date minus one day on opensolaris: date -d "-1 day" +"%Y%m%d"run this command on our crappy Solaris 10 machines however (which I'm guessing doesn't have GNU date running on it) and you get: date: illegal option -- d date: illegal option -- 1 date: illegal option --... (5 Replies)
Discussion started by: rich@ardz
5 Replies

8. Shell Programming and Scripting

how to obtain date and day of the week from `date` command

Hi, does anybody know how to format `date` command correctly to return the day of the week? Thanks -A I work in ksh.... (1 Reply)
Discussion started by: aoussenko
1 Replies

9. UNIX for Dummies Questions & Answers

date - 1 day

Hi, I have been trying just about every unix command to come up with yesterday's date (today's date - 1). I have seen all of the help on this forum, and none of it seems to work for me here. We are using Sun Solaris 9 Unix. I am using this script to create a .txt file with ftp commands that I will... (2 Replies)
Discussion started by: sfedak
2 Replies

10. Shell Programming and Scripting

Getting day from a date...

Hi, I have a date input in MMDDYYYY format.. I have to give the day (whether that DD is sunday/monday...) Is there any command for it... Or do I have to write a script for that... Thanks in Advance Yeheya (1 Reply)
Discussion started by: yeheyaansari
1 Replies
Login or Register to Ask a Question