comparing field to current year


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers comparing field to current year
# 1  
Old 07-28-2010
comparing field to current year

Hi,

I have a comma delimited file that contains name, account number, and account date/time(example record below). I want to pull off all the records that have an account date greater than 8/1 of the current year, and create a new file with those records. So for this year, it would take records with account date > 8/1/2010. On 1/1/2011, it would start comparing to 8/1/2011. Any suggestions?


"smith","0000208352","08/25/2010 12:00:00 AM"
# 2  
Old 07-28-2010
Code:
$ cat urfile
"smith","0000208352","08/25/2010 12:00:00 AM"
"smith","0000208352","06/25/2010 12:00:00 AM"
"smith","0000208352","07/25/2011 12:00:00 AM"
"smith","0000208352","08/25/2011 12:00:00 AM"

$ awk -v d="0801" -F "[\"/]" '{if ($6$7>d) print}' urfile
"smith","0000208352","08/25/2010 12:00:00 AM"
"smith","0000208352","08/25/2011 12:00:00 AM"

This User Gave Thanks to rdcwayx For This Post:
# 3  
Old 07-28-2010
Quote:
Originally Posted by rdcwayx
Code:
$ cat urfile
"smith","0000208352","08/25/2010 12:00:00 AM"
"smith","0000208352","06/25/2010 12:00:00 AM"
"smith","0000208352","07/25/2011 12:00:00 AM"
"smith","0000208352","08/25/2011 12:00:00 AM"
 
$ awk -v d="0801" -F "[\"/]" '{if ($6$7>d) print}' urfile
"smith","0000208352","08/25/2010 12:00:00 AM"
"smith","0000208352","08/25/2011 12:00:00 AM"

I think that's close, but not quite correct. I think you're only comparing the month and day. I probably didn't explain it clearly. Given your input file, the 7/25/2011 should be on the file because it is greater than 8/1 of the current year(2010). If I run this on 1/1/2011, only the 8/25/2011 would be on the new file because it would be the only one greater than 8/1/2011.
# 4  
Old 07-28-2010
Something like this?
Code:
awk -F"[/\"]" -v d="8/1/2011" '
BEGIN{
  split(d,a,"/")
  dat=sprintf("%d%02d%02d",a[3],a[1],a[2])
}
sprintf("%d%02d%02d",$8,$6,$7) > dat {
  print > "new_dat"
  next
}
1' file > old_dat

# 5  
Old 07-28-2010
Quote:
Originally Posted by Franklin52
Something like this?
Code:
awk -F"[/\"]" -v d="8/1/2011" '
BEGIN{
  split(d,a,"/")
  dat=sprintf("%d%02d%02d",a[3],a[1],a[2])
}
sprintf("%d%02d%02d",$8,$6,$7) > dat {
  print > "new_dat"
  next
}
1' file > old_dat

I'm not sure if I'm following...is this always comparing to 8/1/2011? If so, then no. What I'm hoping for is that the compare will dynamically use the current year. So when I run the script now, it will compare the account date to 8/1/2010. When I run it 2011, it will compare the account date to 8/1/2011, and so on.
# 6  
Old 07-28-2010
Try this:
Code:
awk -F"[/\"]" -v y=$(date "+%Y") '
BEGIN{
  dat=y "0801"
}
sprintf("%d%02d%02d",$8,$6,$7) > dat {
  print > "new_dat"
  next
}
1' file > old_dat

This User Gave Thanks to Franklin52 For This Post:
# 7  
Old 07-28-2010
Not sure which one you need.

Show all records after August from current year.
Code:
awk -v y=$(date "+%Y") -F "[\" ]"  'BEGIN{d=y "0801"} {split($6,a,"/")} (a[3]a[1]a[2]>d)' urfile

"smith","0000208352","08/25/2010 12:00:00 AM"
"smith","0000208352","07/25/2011 12:00:00 AM"
"smith","0000208352","08/25/2011 12:00:00 AM"

Show records after August which only in current year.
Code:
awk -v y=$(date "+%Y") -v d="0801" -F "[\" ]"  '{split($6,a,"/")} (a[1]a[2]>d&&a[3]==y)' urfile
"smith","0000208352","08/25/2010 12:00:00 AM"


Last edited by rdcwayx; 07-28-2010 at 06:17 PM..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. HP-UX

Comparing the timestamp of the file to current time

I have a file like this -rwxr-xr-x 1 rewq other 168 Jan 13 07:05 check_files.sh I want to compare (check_files.sh time) with the current time to see if its is older than 2 hours or not if it is not older than 2 hrs then do something.can someone help me on this?.I dont... (7 Replies)
Discussion started by: haadiya
7 Replies

2. Shell Programming and Scripting

Comparing the dates with the current date in perl scripting

Hi i have a file containg dates likebelow 4/30/2013 3/31/2013 4/30/2013 4/16/2013 4/30/2013 4/30/2013 5/30/2013 5/30/2013 4/30/2013 5/30/2013 5/30/2013 3/31/2013 now i want to compare the above dates with current date and i want to display the difference . (10 Replies)
Discussion started by: siva kumar
10 Replies

3. Shell Programming and Scripting

How to pass current year and month in FOR LOOP in UNIX shell scripting?

Hi Team, I have created a script and using FOR LOOP like this and it is working fine. for Month in 201212 201301 201302 201303 do echo "Starting the statistics gathering of $Month partitions " done But in my scripts the " Month " variable is hard-coded. Can you please any one... (3 Replies)
Discussion started by: shoan
3 Replies

4. UNIX for Dummies Questions & Answers

Change time with current year

Hi, How can i change the time below (red font) with the current year? Thank You in advance. hostname 2007-Feb-9 /u100/DEVCO/Patching a.log hostname 2010-Jun-25 /u100/DEVCO/DumpCleaner a.log hostname 2011-Jun-25 /u100/DEVCO/DumpCleaner/sample a.log hostname 23:44-Jun-25... (2 Replies)
Discussion started by: lienyca
2 Replies

5. Shell Programming and Scripting

Compare Field in Current Line with Field in Previous

Hi Guys I have the following file Essentially, I am trying to find the right awk/sed syntax in order to produce the following 3 distinct files from the file above: Basically, I want to print the lines of the file as long as the second field of the current line is equal to the... (9 Replies)
Discussion started by: moutaye
9 Replies

6. UNIX for Dummies Questions & Answers

Comparing two files with datestamp to current date

Hi, I am new to unix and I am stuck on how to compare two .zip file with date stamp in my directory. I need to compare out of the two file which is oldest to current date and unzip it after that done continue to unzip the second zip file. Thanks for your help. (5 Replies)
Discussion started by: lilvi3tboix1
5 Replies

7. Shell Programming and Scripting

ksh comparing current and previous lines

Hi, I am currently trying to work out how to compare one line with the last line I have read in via ksh. I have a file which has sorted output from a previous sort command so all the lines are in order already and the file would look something like show below. Each line has a name and a time... (5 Replies)
Discussion started by: paulie
5 Replies

8. Shell Programming and Scripting

Comparing current date

Hi, I have start date and end date in the following format. I need to check the current date is greater than the start date and less than the end date. if i use the command date --date "Tue 6:00 AM", it takes next Tues day not the current week's Tues day. Is there a way to get the current Tues... (9 Replies)
Discussion started by: bharathappriyan
9 Replies

9. Shell Programming and Scripting

read file and print additional rows till current year

Hi all i have a file like 2006,1,2 2007,2,3 2008,3,4 I will read this and my output should be like 2006,1,2 2007,1,2 2008,1,2 2007,2,3 2008,2,3 2008,3,4 Giving the explanation, we will read the first line of the file and if the year any other than current year, we will print as many... (1 Reply)
Discussion started by: vasuarjula
1 Replies

10. UNIX for Dummies Questions & Answers

Comparing CRON PID w/Current PPID

All, I've got a script that needs to check if it was started by cron. The code seems to be right, but it's not running correctly if cron starts it. Am I getting the pid's correctly? I'm not having any luck figuring it out. :confused: Any help is appreciated! CRON_ID=$(ps -aef | grep... (1 Reply)
Discussion started by: GregWold
1 Replies
Login or Register to Ask a Question