System Administration
cancel
Showing results for 
Search instead for 
Did you mean: 

Question about cat and grep commands with awk

 
SOLVED
Go to solution
Mahesh Alexander
Frequent Advisor

Question about cat and grep commands with awk


Hi all,

I am trying to execute a command which takes the today's current date and performs a search in the syslog file. I was first trying with the followin commands with no success:

grep ""date |awk ' { print $2" "$3} '"" /var/adm/syslog/syslog.log

cat /var/adm/syslog/syslog.log | grep "date | awk ' { print $2" "$3 } '"

After a while I got what I was looking for by issuing the following command:

cat /var/adm/syslog/syslog.log | grep "`date | cut -c 5-10`"

Can anybody tell what is failing in the first two commands? And, does anybody know alternative ways to achieve the same thing bye other commands or methods?

Thanks all in advance!
6 REPLIES
James R. Ferguson
Acclaimed Contributor
Solution

Re: Question about cat and grep commands with awk

Hi:

By using the 'date' formatting directives you can get the short month name and space-filled day value consistent with what matches the 'syslog' format:

# grep "$(date '+%b %e')" /var/adm/syslog/syslog.log

Regards!

...JRF...
Mahesh Alexander
Frequent Advisor

Re: Question about cat and grep commands with awk

Could you please explain why you used '+%b %e' ??

Thx!
Mel Burslan
Honored Contributor

Re: Question about cat and grep commands with awk

from the man pages of date command

%b Abbreviated month name. For example, Jan.

...

%d Day of the month as a two-digit decimal number [01-31].

You know, any flavor of unix has this wonderful utility called "man" used in conjunction with the command, which gives you the command usage manual. In your particular case, the magic command would be

# man date

please use the command liberally throughout your day.
________________________________
UNIX because I majored in cryptology...
James R. Ferguson
Acclaimed Contributor

Re: Question about cat and grep commands with awk

Hi (again):

> Could you please explain why you used '+%b %e' ??

I did. This format directive yields the short month name and _space-filled_ day value consistent with what matches the 'syslog' format:

# date '+%b %e'
Jul 28

This is better than doing:

# date|cut -c5-10

...which requires a second process (the 'cut') to be spawned.

By the way, anytime you see 'grep' and 'awk' and/or 'cat' in a pipeline, remember that 'grep' and 'awk' can read input directly by specifying the file(s) on their command line. Too, 'awk' was designed to pattern match piping the output of 'grep' to it is generally another wasted process.

Regards!

...JRF...
Mahesh Alexander
Frequent Advisor

Re: Question about cat and grep commands with awk

Thanks for your contributions, specially you Mel. You can't imagine how much you helped me.. :P
Mahesh Alexander
Frequent Advisor

Re: Question about cat and grep commands with awk

Question was answered in the thread accordingly.