3

I have this large file that I need to go through using date and time. Every line in this file has a date and time in it.

I need to search through the lines and get the lines that lie between two specific dates and times.

Example file (the field for date and time is $2 and $3):

SERVER 2015-12-12 05:00:20 some_text_here something  
SERVER 2015-11-02 12:22:40 some_text_here something
SERVER 2015-12-11 20:00:00 some_text_here something
SERVER 2015-12-11 23:00:00 some_text_here something
SERVER 2015-12-12 00:30:00 some_text_here something
SERVER 2015-12-12 00:59:00 some_text_here something
SERVER 2015-09-20 03:28:11 some_text_here something
SERVER 2015-04-16 04:49:59 some_text_here something

The command I tried (users have to supply 4 arguments when running the script):

AAA="$1 $2"
BBB="$3 $4"

awk '$2" "$3>="'"$AAA"'" && $2" "$3<="'"$BBB"'"' file.txt > newfile.txt

I am using the lines above as a script but its not working.

newfile.txt should contain (using arguments 2015-12-11 20:00:00 2015-12-12 01:00:00):

SERVER 2015-12-11 20:00:00 some_text_here something
SERVER 2015-12-11 23:00:00 some_text_here something
SERVER 2015-12-12 00:30:00 some_text_here something
SERVER 2015-12-12 00:59:00 some_text_here something
  • Just to be sure: Can you also tell us which lines you want to see in newfile.txt and which lines you do see in newfile.txt? – Lucas Apr 13 '16 at 4:28
  • i should be able to copy the whole line that satisfy the condition (date/time) – capslock13 Apr 13 '16 at 4:31
  • I think no line does match the condition because even for the first line the time is after the BBB time and for all other lines the date is before the AAA date. – Lucas Apr 13 '16 at 4:36
  • im not exactly sure what you mean, but I just put the sample file there for reference. The actual file contain thousands of line. and the AAA should be "earlier" than BBB – capslock13 Apr 13 '16 at 4:40
  • I think in your example lines the command should not print any lines to newfile.txt as none of these lines satisfies the condition. Or which of this lines satisfies the condition in your opinion (please tell me the line or line number from you example above)? – Lucas Apr 13 '16 at 4:43
0

Maybe you need a script that you can input the two specific dates and times. enter image description here

Code:

#!/bin/bash
if [ $# -ne 4 ];then
exit 0
fi
AAA=$1" "$2
BBB=$3" "$4
awk -v begintime="${AAA}" -v endtime="${BBB}" '$2" "$3>=begintime && $2" "$3<=endtime' exa > newfile.txt
3

The only real problem is that you assign to $AAA and $BBB instead of AAA and BBB. So if you do (nearly the same as your code):

AAA="2015-12-11 20:00:00"
BBB="2015-12-12 01:00:00"
awk '$2" "$3>="'"$AAA"'" && $2" "$3<="'"$BBB"'"' file.txt > newfile.txt

it should already work. But I recommend the following further changes in order to reduce potentioal quoting problems (especially if you happen to reuse this apprach somewhere else or put special chars in AAA or BBB):

AAA="2015-12-11 20:00:00"
BBB="2015-12-12 01:00:00"
awk -v string1="$AAA" -v string2="$BBB" '$2" "$3>=string1 && $2" "$3<=string2' file.txt > newfile.txt

You can read about -v in the man page of awk.

  • apologies. i missed that. that should really be without $. also, added some details – capslock13 Apr 13 '16 at 6:20
-1

awk is not the best tool.

sed -e  "/^$AAA/,/^$BBB/,p" file.txt

and man sed.

  • I get sed: -e expression #1, char 46: unknown command: `,'. You have to remove the second comma. – Lucas Apr 13 '16 at 4:59
  • This is only would be correct if the file is sorted. But the example above is not sorted (even though the result is correct in this case). – Lucas Apr 13 '16 at 5:03
  • I just saw that the command is not correct at all because it uses the ^ anchor and doesn't exclude unmatched lines with -n. – Lucas Apr 13 '16 at 5:19

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.