I have a log file with every line that starts with the following date format.

2020-06-21 12:39:49 enginestart [INFO] total records found

I want to fetch logs dated from 2020-06-15 to 2020-06-21 and to display as output.

I have tried to grep, egrep, and sed and getting the expected result.

grep -E "2020-06-19|2020-06-20" logfile.* > file1

But the above-fetched log only from 20th

Thank you

  • Hi and welcome to SuperUser. I'm confused, what have you tried with grep? Somehow I don't see how something like grep - P '^2020-06-(1[56789]|2[01])' would not work?! ... could you please edit your question and include what you have tried? Then we can figure out exactly why it didn't work ... – 0xC0000022L Jun 22 '20 at 8:02
  • Thanks @0xC0000022L for the update. I tried with sed as follows sudo sed -n '/^2020-06-21/p' logfile.* > file1 . But not sure how to add multiple dates. Also I tried the command which you shared grep -P '^2020-06-(1[56789]|2[01])', the result is like "logfile.1:2020-06-20 04:01:37 " only but not started from 15th and output contains logfile name – Wonderhost Jun 22 '20 at 8:29
  • Well, you can get rid of the file name by passing -h. The man grep manual is your friend. The lines in your log file do not start with 2020 then? Because my regex anchors it at the beginning of the line (the ^). – 0xC0000022L Jun 22 '20 at 8:33
  • the grep -E line you gave is lacking () to group the alternatives. I'm not sure it will work without. But the fact that your grep -E invocation and my grep -P command both only show output from the 20th makes me think that you may want to look at the files. Also if some of the log files have been compressed (e.g. .gz suffix), grep will typically ignore them except when told otherwise. – 0xC0000022L Jun 22 '20 at 8:35
  • Added -h argument and removed log file in the beginning but it is not fetching the log from 15th instead it collected from 20th only. Thanks for your help – Wonderhost Jun 22 '20 at 8:36

Using sed : sed -n /From/,/To/p.

sed -n /2020-06-19/,/2020-06-20/p logfile.* > file1

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.