Grep file containing dates

How to grep a file containing dates to only last 30 days then move to another folder

Welcome kmarcus,

It sort of seems like a question that has been asked before in many different ways, but in order for us to help, you would need to show us some sample data to include/exclude.

Additionally, when you say "then move to another folder", which of these do you mean? :-

  • move the whole files that contain a recent or old date
  • extract the matching records to a separate file and move that
  • extract the matching records (removing from the original file) to a new file and move that
  • something else?

I have a few general to questions pose in response first:-

  • Is this homework/assignment? There are specific forums for these.
  • What have you tried so far?
  • What output/errors do you get?
  • What OS and version are you using?
  • What are your preferred tools? (C, shell, perl, awk, etc.)
  • What logical process have you considered? (to help steer us to follow what you are trying to achieve)

Most importantly, What have you tried so far?

There are probably many ways to achieve most tasks, so giving us an idea of your style and thoughts will help us guide you to an answer most suitable to you so you can adjust it to suit your needs in future.

We're all here to learn and getting the relevant information will help us all.

Kind regards, and hoping to help,
Robin

2 Likes
$ awk '$NF < "2018-05-01"' ip.txt

hello I am using Unix the script I am looking for should automatically pick the date from the system current date rather than manually typing the date and move text lines containing lines older than 30 days to another folder say myname.dat

Hi Kmarcus,

You'll need to be a little more specific, please look at rbattle1's reply to your post and provide us with as much of the information as you can.

There are many members here who will be able to assist you, however you have to enable us to assist by providing us with the information we need to assist you in solving your problem.

Regards

Gull04

1 Like

grep is a program, not a verb -- a program that does exactly what you do not want. It is almost never a useful descriptive term.

Most linux distributions have dgrep available - date grep. Primarily, it uses the standard library to convert dates to number of seconds since Jan 1, 1970.
Then dgrep does what grep does with the transformed date - select lines from a file

dgrep linux command man page

1 Like
awk -v t=$(date +%Y-%m-%d) -F "'" '$1 < t' myname.dat

I have the above unix command to filter text with dates older than the current date on unix HP-UX
how can I modify it to filter dates 30 days older than the current date such as 30 days ago without getting the date: illegal option error

You really should have mentioned HP-UX from the start. So can we assume ksh88? The reason for this should be obvious - not all UNIXes have exactly the same commands or even exactly the same syntax for a given command. Solaris even has multiple versions of commands like awk. HP-UX usually supports ksh88, and comes with an ancient C compiler by default - as examples. So our answers can be wrong right out of the box. Wasting your time and ours.