0

For example, I have a file with data:

[2023-03-21 12:27:19 +0000] some_data
[2023-03-21 12:27:19 +0000] some_data
[2023-03-22 12:27:19 +0000] some_data
[2023-03-23 12:27:19 +0000] some_data
[2023-03-23 12:27:19 +0000] some_data
[2023-03-25 12:27:19 +0000] some_data
[2023-03-26 12:27:19 +0000] some_data
[2023-03-29 12:27:19 +0000] some_data
[2023-03-29 12:27:19 +0000] some_data
[2023-03-29 12:27:19 +0000] some_data
[2023-03-29 12:27:19 +0000] some_data

and I want to grep all lines which includes values from 2023-03-22 to 2023-03-26

so, I want to grep its lines

[2023-03-22 12:27:19 +0000] some_data
[2023-03-23 12:27:19 +0000] some_data
[2023-03-23 12:27:19 +0000] some_data
[2023-03-25 12:27:19 +0000] some_data
[2023-03-26 12:27:19 +0000] some_data

How can I do it with grep or maybe with sed or awk?

And another case:

I have a file with data

[2023-03-21 00:45:20 +0000] some_data
[2023-03-21 01:10:11 +0000] some_data
[2023-03-21 02:04:56 +0000] some_data
[2023-03-21 12:09:15 +0000] some_data
...
...
in this part the lines with the next data with some_data
...
...
[2023-03-23 21:40:19 +0000] some_data
[2023-03-23 22:15:11 +0000] some_data
[2023-03-23 23:40:06 +0000] some_data

How I can grep lines from [2023-03-21 01:10:11 +0000] some_data to [2023-03-23 22:15:11 +0000] some_data

Maybe Can I grep with flag -n ? I get the number of string for [2023-03-21 01:10:11 +0000] and then grep value [2023-03-23 22:15:11 +0000] with flag -n. Its command back me numbers of line for these lines.

And how can I get massive of date from specific line to spicific line if I know number of lines?

1
  • For this specific instance, you can do grep 2023-03-2[2-6] input.txt | grep something_specific.
    – RonJohn
    Commented Mar 30, 2023 at 15:36

3 Answers 3

0

A simple way to do this would be to specify the characters you want in that spot as members of a "bracket expression":

grep '2023-03-2[23456]' file_name

When you're looking for a set of characters that are consecutive in the sorting/collation scheme (as you are with your dates), you can use a range in the bracket expression:

grep '2023-03-2[2-6]' file_name

More complex ways to match the contents of the lines are available, but your question is handled by a nice, simple bracket expression.

4
  • The "more complex" matching techniques would make a good answer.
    – RonJohn
    Commented Mar 30, 2023 at 16:12
  • @RonJohn, true, but as far as I can tell, it would be an answer to a different question.
    – Sotto Voce
    Commented Mar 31, 2023 at 16:48
  • Your answer is what I put in a comment. Didn't make it an answer because it's too specific.
    – RonJohn
    Commented Mar 31, 2023 at 16:51
  • The question was specific about matching the range of dates, and omitted any need to match other fields. My answer was specific where the question was specific. (the question was updated with a new request since I submitted my answer)
    – Sotto Voce
    Commented Mar 31, 2023 at 17:01
0

I founded an answer: If I want to print massive of data from file - from value to value, so I can use command sed in this case:

sed -n '/\[2023-03-21 01:10:11/,/\[2023-03-23 22:15:11/p' file_name

I forgot that sed it's a stream editor for string data.

1
  • That would fail if either of those specific timestamps did not exist in the input - don't do exact comparisons like that when trying to match within a range, use <= and/or >= comparisons instead (which you can't do with sed so use awk) so your code will work even if the exact range start/end strings are not present in the input, e.g. see unix.stackexchange.com/a/741504/133219.
    – Ed Morton
    Commented Apr 7, 2023 at 13:41
0
$ awk -F'[[ ]' '("2023-03-22" <= $2) && ($2 <= "2023-03-26")' file
[2023-03-22 12:27:19 +0000] some_data
[2023-03-23 12:27:19 +0000] some_data
[2023-03-23 12:27:19 +0000] some_data
[2023-03-25 12:27:19 +0000] some_data
[2023-03-26 12:27:19 +0000] some_data

Consider how trivial it'd be to modify that for any requirements, e.g.:

$ awk -F'[][ ]' '("2023-03-26" <= $2)' file
[2023-03-26 12:27:19 +0000] some_data
[2023-03-29 12:27:19 +0000] some_data
[2023-03-29 12:27:19 +0000] some_data
[2023-03-29 12:27:19 +0000] some_data
[2023-03-29 12:27:19 +0000] some_data

$ awk -F'[][ ]' '("2023-03-26" == $2)' file
[2023-03-26 12:27:19 +0000] some_data

$ awk -F'[][ ]' '("2023-03-22" == $2) || ("2023-03-26" == $2)' file
[2023-03-22 12:27:19 +0000] some_data
[2023-03-26 12:27:19 +0000] some_data

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .