Home > Mobile >  grep data in file for particular date range
grep data in file for particular date range

Time:10-09

I am trying to search data in a file in given date range (25th May -26th May 2017)

2017-05-25 to 2017-05-26

It search the data but it searches other data as well which is not in this date range. I am using below command for same.

sed -n '/2017-05-25/,/2017-05-26/p' /tmp/CGMJGFTS_FIN_NAM_PRODlog

This looks good

1_V9AGGSWDC16_112_2017_05_25-160523_1001293_464.csv:"CGMJGFTS_FIN_NAM_PROD"!@#"PATROL_GC"!@#"169.177.208.155"!@#""!@#"4100"!@#"169.177.208.155"!@#"ISQL"!@#"PATMON"!@#"2017-05-24"!@#"2017-05-24"!@#"1"!@#"SYBASE"!@#"CONDB"!@#"2017-05-25 00:01:16"

but it also search

@#"2"
1_V9AGGSWDC16_112_2017_05_28-160445_1001293_467.csv:"CGMJGFTS_FIN_NAM_PROD"!@#"PATROL_GC"!@#"169.177.208.155"!@#""!@#"4100"!@#"150.110.82.18"!@#"ISQL"!@#"DBA_LOCAL"!@#"**2017-05-27**

CodePudding user response:

Your current sed pattern will look for matches over multiple rows. (see @twalberg's comment).


If you're looking for individual lines that contain both of the dates (as literals), and with the assumption that said dates are in order (ie, older date listed first, newer date listed later in the line), you could try using a single regexp, eg:

sed -n '/2017-05-25.*2017-05-26/p' <filename>

For example:

$ cat tt
1.some_stuff.2017-05-24_some_more_stuff:2017-05-24;and the end
2.some_stuff.2017-05-25_some_more_stuff:2017-05-26;and the end   # should only find this row
3.some_stuff.2017-05-24_some_more_stuff:2017-05-26;and the end
4.some_stuff.2017-05-25_some_more_stuff:2017-05-27;and the end

$ sed -n '/2017-05-25.*2017-05-26/p' tt
2.some_stuff.2017-05-25_some_more_stuff:2017-05-26;and the end

And of course you've got other options besides sed, eg:

$ egrep "2017-05-25.*2017-05-26" tt
2.some_stuff.2017-05-25_some_more_stuff:2017-05-26;and the end

$ awk '/2017-05-25.*2017-05-26/ {print}' tt
2.some_stuff.2017-05-25_some_more_stuff:2017-05-26;and the end

If on the other hand you're looking for sed to find individual rows where dates (as literals) are within the range of 2 search dates, sed doesn't know anything about date math/comparisons (again, same thing @twalberg's commented on).


If none of the above addresses your issue, then you'll need to edit your question to provide more details of what exactly you're trying to accomplish. And to that end I'd recommend you take a look at How to create a Minimal, Complete and Verifiable example

CodePudding user response:

I think, You want to print those lines which will have either 2017-05-25 or 2017-05-26 as date. If I'm correct then below command might help you. :)

egrep "(2017-05-25.*|2017-05-26.*)" file
  • Related