You are not logged in.

#1 2009-08-25 17:49:57

whoops
Member
Registered: 2009-03-19
Posts: 891

[solved] grep today, yesterday & the day before

Wah! (= "hi!")


I'm trying to grep the last three days of a list - It's killing me:

| grep -e "\($(($(date +%d)-0))\|$(($(date +%d)-1))\|$(($(date +%d)-2))\) $(date +%b)"

echo grep[stufffromabove]  says:

grep -e \(25\|24\|23\) Aug

... cat list.txt | grepstufffromabove shows no lines at all. Tried several variations of the line, started all over again a few times but I keep messing it up at some times... yes, and I must admit, I've been confusing myself using about 3 different "regex versions" today (uhm... sed, grep, perl, php?), so maybe I keep messing it up this way again....


Could someone tell me what I'm doing wrong?

Last edited by whoops (2009-08-25 19:08:08)

Offline

#2 2009-08-25 18:12:59

brisbin33
Member
From: boston, ma
Registered: 2008-07-24
Posts: 1,796
Website

Re: [solved] grep today, yesterday & the day before

the following works for me:

#!/bin/bash

day_0=$(date +%d)
day_1=$((day_0-1))
day_2=$((day_0-2))

grep "$day_0\|$day_1\|$day_2" /var/log/everything.log

i guess `| grep -e \(one\|two\)` != `| grep "one\|two"`


EDIT: oh, now i see the issue.  didn't notice that little month hangin on the end there...

try this version:

#!/bin/bash

month="$(date +%b)"
day_0=$(date +%d)
day_1=$((day_0-1))
day_2=$((day_0-2))

grep -E "[$day_0$day_1$day_2] $month" /var/log/everything.log

Last edited by brisbin33 (2009-08-25 18:20:55)

Offline

#3 2009-08-25 18:27:15

whoops
Member
Registered: 2009-03-19
Posts: 891

Re: [solved] grep today, yesterday & the day before

Thanks but...

I just sort of just figured out the obvious and noticed, that I'd even have a big problem if that regex of mine worked.... unluckily, there actually seem to be days with a number smaller than 3, but no negative days in the human calendar... so I'm going to rest first and think it over after that. Most likely I'm going to end up subtracting a day in unix-time and converting back, better ideas are not necessary but welcome wink.

(edit: oh, and thanks again - that might come handy when I ended up with the same $(\[chaos\]) trying the unix time version later)

Last edited by whoops (2009-08-25 18:29:30)

Offline

#4 2009-08-25 18:38:46

brisbin33
Member
From: boston, ma
Registered: 2008-07-24
Posts: 1,796
Website

Re: [solved] grep today, yesterday & the day before

oh, haha hadn't notice that myself.  good news is this might help you:

┌─[ 14:37 ][ blue:~ ]
└─> date +%d\ %b
25 Aug
┌─[ 14:37 ][ blue:~ ]
└─> date +%d\ %b -d "-1 days"
24 Aug
┌─[ 14:37 ][ blue:~ ]
└─> date +%d\ %b -d "-2 days"
23 Aug

wink

Offline

#5 2009-08-25 18:38:54

fogobogo
Member
Registered: 2008-08-24
Posts: 83

Re: [solved] grep today, yesterday & the day before

date --date '-3 days'

Last edited by fogobogo (2009-08-25 18:39:15)

Offline

#6 2009-08-25 19:07:47

whoops
Member
Registered: 2009-03-19
Posts: 891

Re: [solved] grep today, yesterday & the day before

ooooh, ok, that made it easier than what I had in mind in the beginning, thanks. I guess I should just have read the whole date manpage.

| grep "$(date +'%d %b')\|$(date --date '-1 days' +'%d %b')\|$(date --date '-2 days' +'%d %b')" \

... does it.

Offline

#7 2009-08-25 19:32:34

fogobogo
Member
Registered: 2008-08-24
Posts: 83

Re: [solved] grep today, yesterday & the day before

not to mention a lot easier to read wink
coreutils ftw!

Offline

#8 2009-08-25 20:42:43

Profjim
Member
From: NYC
Registered: 2008-03-24
Posts: 658

Re: [solved] grep today, yesterday & the day before

deleted, I misread the previous post

Last edited by Profjim (2009-08-25 20:47:29)

Offline

Board footer

Powered by FluxBB