Bappy2015
Bappy2015

Reputation: 13

How to extract logs from a timestamp till the end of the file in shell script

I want to extract logs from a log file, say the file name is todayslog.log

My logs are in the following format:

[12/21/16 4:21:00:083 MST] blah blah 1
[12/21/16 4:22:00:102 MST] blah blah 2
[12/21/16 4:23:00:128 MST] blah blah 3

I want to extract logs from the first occurrence of say "12/21/16 4:21:00:083 MST" till the end of file.

I have tried the following codes but it isn't working because of the '/' present in my date.

sed -e '/12/21/16 1:30:54:663/,$p' todayslog.log
sed -n '/12/21/16 4:21:00:028/,/12/21/16 4:21:00:128/p' todayslog.log

Upvotes: 0

Views: 489

Answers (3)

Marek Nowaczyk
Marek Nowaczyk

Reputation: 257

Here it comes sed with:

sed -n '/12\/21\/16 4:21:00:083 MST/ {:end; p; n; b end}'

Upvotes: 0

umläute
umläute

Reputation: 31284

there's also dategrep, a small tool that allows one to filter entries by date. Something like:

dategrep --start "12/21/16 4:21:00" --format "%x %X" todayslog.log

Upvotes: 1

sjsam
sjsam

Reputation: 21965

I would use the below technique

$ cat 41265401 # this is my file with data
[12/21/16 4:21:00:083 MST] blah blah 1
[12/21/16 4:22:00:102 MST] blah blah 2
[12/21/16 4:23:00:128 MST] blah blah 3
[12/21/16 6:24:00:128 MST] blah blah 6
[12/21/16 8:29:00:101 MST] blah blah 8
bla bla
bla bla

$ cat 41265401.sh 
#!/bin/bash
find_me="12/21/16 4:23:00:128 MST" # you can also pass the string as argument
start_pos=$(grep -nm1 "$find_me" 41265401)
start_pos=${start_pos%%:*} # Shell param expansion, see [1]
awk -v start_pos="$start_pos" 'FNR>=start_pos' 41265401 > result

$ ./41265401.sh # script in action

$ cat result  # from first match till end
[12/21/16 4:23:00:128 MST] blah blah 3
[12/21/16 6:24:00:128 MST] blah blah 6
[12/21/16 8:29:00:101 MST] blah blah 8
bla bla
bla bla

References

[1] : Shell Param expansion

Upvotes: 0

Related Questions