Operating System - HP-UX
1756547 Members
2596 Online
108848 Solutions
New Discussion юеВ

Want to pull particular dated lines out of a file

 
SOLVED
Go to solution
Angie_1
Regular Advisor

Want to pull particular dated lines out of a file

I want to pull a certain date out of the syslog.log file but can't get it to work. Its not seeing only todays date but it also outputed January 10th. I thought this would be a simple script but I am missing something.

DATE=`date "+%b %d"`
grep $DATE /var/adm/syslog/syslog.log

Please post back.

Thanks!

Angie
5 REPLIES 5
Thierry Poels_1
Honored Contributor
Solution

Re: Want to pull particular dated lines out of a file

hi,

the date contains blanks so you have to use quotes:
grep "$DATE" /var/adm/syslog/syslog.log

It was grepping for "Jan" in files "13" & syslog.log, and gave an error it could not find file "13".

regards,
Thierry.
All unix flavours are exactly the same . . . . . . . . . . for end users anyway.
Florian Heigl (new acc)
Honored Contributor

Re: Want to pull particular dated lines out of a file

I think the reason why You get the Jan 10 results is that they simply contain '13', too.

like in the below:
OLDsyslog.log:Jan 11 16:00:35 snowwhite sshd[7831]: Accepted publickey for wwwsync from 62.138.60.51 port 65260 ssh2
OLDsyslog.log:Jan 13 16:00:41 snowwhite sshd[29392]: Accepted publickey for wwwsync from 62.138.60.51 port 64093 ssh2

This will -hopefully - change if You simply change it to:
snowwhite:/var/adm/syslog# grep "$DATE" OLDsyslog.log

The hiphens (I think that's what they're called ;) will tell ksh to pass $DATE to grep as a single string instead of two separate ones.

best wishes,
florian
yesterday I stood at the edge. Today I'm one step ahead.
A. Clay Stephenson
Acclaimed Contributor

Re: Want to pull particular dated lines out of a file

Essentially you have a quoting problem but I would also enhane your search string using grep -E to use an anchored search. Only if the string is found at the beginning of a line does it match.

DATE=$(date '+^%b %d') # note the anchor "^"
grep -E "${DATE}" /var/adm/syslog/syslog.log # note the quotes around ${DATE}


If it ain't broke, I can fix that.
A. Clay Stephenson
Acclaimed Contributor

Re: Want to pull particular dated lines out of a file

I should go ahead and correct another booboo while I'm at it. You should replace your '%b' with '%e' otherwise your search is going to fail on any days of the month less than 10. Man date for details.
If it ain't broke, I can fix that.
Angie_1
Regular Advisor

Re: Want to pull particular dated lines out of a file

WOW all of you guys are such brainiacs!

Thank you so much! I will make the modification and test it out.

Angie