Ubuntu – How to get text from range of dates using grep/sed in large text file

command linegrepsed

I have big file text (almost 3GB) – it is a log file. I want to get lines of text which correspond to a range of dates from this file, from 13 July to 19 July. My log format is:

2016-07-12 < ?xml version>
2016-07-13 < ?xml version>
2016-07-18 < ?xml version>
2016-07-18 < ?xml version>
2016-07-19 < ?xml version>
2016-07-20 < ?xml version>
sample text sample text
sample text sample text
sample text sample text
2016-07-20 < ?xml version>
sample text sample text
2016-07-20 < ?xml version>

so after grep/sed it should be output like this:

2016-07-13 < ?xml version>
2016-07-18 < ?xml version>
2016-07-18 < ?xml version>
2016-07-19 < ?xml version>

How can I get this?

Best Answer

With grep if you know the number of lines you want you can use context option -A to print lines after the pattern

grep -A 3 2016-07-13 file

that will give you the line with 2013-07-13 and the next 3 lines

with sed you can use the dates to delimit like this

sed -n '/2016-07-13/,/2016-07-19/p' file

which will print all lines from the first line with 2016-07-13 up to and including the first line with 2016-07-19. But that assumes you have only one line with 2016-07-19 (it will not print the next line). If there are multiple lines use the next date instead and use d to delete the output from it

sed -n '/2016-07-13/,/2016-07-20/{/2016-07-20/d; p}' file
Related Question