Grep out only today's date

Hi,

I have some log files. I've been asked to grep out error messages that have happened ONLY today. (However, the logs keep messages a long time so they have error messages going back weeks)

They log details in these fields (order):

Month Day Time Server message

I can grep out the month and day. However, on some of the log files....there is 1 space between Month and Day, and on other log files there is 2 spaces.

SO my grep works with the 1 space, but not the 2 space log files.

this is my command:

cat file_name | cut -d " " -f 1,2

how do i get it to grep out the dates even with 2 spaces between the Month and Time?

Also, i would then like to email out if the date matches today's date....and if not, email out that nothing has changed or been logged today.

Please do not leave people guessing. Show a representative sample of input, desired output, attempts at a solution and specify what OS and versions being used, or this thread will be closed.

That's also a useless use of cat award.

excuse my ignorance, i'm a newbie to Unix.

i'm using Solaris

the output of the log files are:

May 19 20:34:33 server_name  Process: Error_message_timed_out

I can grep out the month and day. However, on some of the log files....there is 1 space between Month and Day, and on other log files there is 2 spaces.

SO my grep works with the 1 space, but not the 2 space log files.

this is my command:

cat file_name | cut -d " " -f 1,2 

tho i'm guessing (after getting my award) :slight_smile: that it should be

<file_name | cut -d " " -f 1,2 

how do i get it to grep out the dates even with 2 spaces between the Month and Time?

Also, i would then like to email out if the date matches today's date....and if not, email out that nothing has changed or been logged today.

thanks for any responses

How about..

$ mon=$(date +'%b')
$ day=$(date +'%d')
$ grep -E "$mon[ ]+$day" xx
May  23 20:34:33 server_name Process: Error_message_timed_out
May 23 20:34:33 server_name Process: Error_message_timed_out
May    23 20:34:33 server_name Process: Error_message_timed_out
$ 
$ 
$ 
$ cat xx
May  23 20:34:33 server_name Process: Error_message_timed_out
May 23 20:34:33 server_name Process: Error_message_timed_out
other msg
other msg
May    23 20:34:33 server_name Process: Error_message_timed_out
May 19 20:34:33 server_name Process: Error_message_timed_out
other msg
May 19 20:34:33 server_name Process: Error_message_timed_out
May 19 20:34:33 server_name Process: Error_message_timed_out
May 19 20:34:33 server_name Process: Error_message_timed_out
$ 

You have to deal with the other patterns if any. Just update the pattern.

1 Like

hi thanks for your help.

that seemed to do the trick! thanks very much!

first put the contents of file in a temp file with trimming the space then try to search the pattern, words will be separated by max 1 space.

cat file_name|tr -s " " >temp_file
1 Like

Please also post sample data for a day of the month with a single-digit day.
Also sample data for every possible variation in the date field of the input record format.

Almost, you need to leave out the "|"

<file_name cut -d " " -f 1,2 

or

cut -d " " -f 1,2 < filename

To be independent of spacing it is best to use awk:

awk '{print $1,$2}' < filename
1 Like

Since your data is stored in log files as records, using 'awk' would be good option. AWK deals with column-oriented text data. Try this out.

or

Here you can print any desired column like $1,$2,$3,etc. Just ensure u put a comma after column number (like $1).

1 Like