0

I have 6 to 8 log files which contains date and timestamp in this format INFO [07-29 04:13:03.795] (blah.blah) I want to Keep the log files data between last Monday to Sunday and delete rest of the data from those log files using bash script

1

You can use something like for date in 09-{05..11} ; do grep \\[$date logfile > $date.log ; done to split the desired days' logs into their own files, but you should really be using the logrotate utility to manage that for you.

  • This is giving me error like below and log files getting created but with 0 bytes of data egrep: Unmatched [ or [^ egrep: Unmatched [ or [^ egrep: Unmatched [ or [^ egrep: Unmatched [ or [^ egrep: Unmatched [ or [^ egrep: Unmatched [ or [^ egrep: Unmatched [ or [^ – Chandra Sekhar Gali Sep 14 '16 at 20:04
  • Sorry, I didn't have enough escapes. I've updated with a corrected command. – Jesusaur Sep 14 '16 at 21:27
  • Thank you for the correction ,now its not throwing any errors but it wont generate any output either . i am looking for something like awk or sed to compare with the filed values in log file anbd if the value is not within in the time range delete and save the file – Chandra Sekhar Gali Sep 16 '16 at 17:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.