Grepping Date Variable

Hello,

I would like for the user to input the date for a particular log file, then have the input sent to a variable, which is then used via grep to extra the logs for the specific date the user request.

I did some searching, but I still don't understand why I'm not seeing any result.

Please help!

#!/bin/bash
echo Enter date: MM-DD-YYYY
read vdate
vdate=`date '+%m-%d-%Y'`
grep $vdate /log/log.csv |awk -F, '{print $1, $2}' > log_mm_dd_yyyy

The reason is you set "vdate"s value to the output of the command "date +...", overwriting whatever the user might have entered. The "date" command does not format any value supposed to represent some date, but gives back the system date (and time) in the format specified.

I have slightly modified your script and it should work as expected, as long as the user enters the date in a correct format. No input validation is being performed in the script. Notice that i have replaced the (outmoded) backticks with the modern and recommended "$(...)" construct, used "read"s capability to display a message preeding the input field (which is saving the "echo") and replaced the "grep" with the built-in functionality of awk as you use this anyway. There is no necessity to use two tools when one already suffices. :

#!/bin/bash

typeset vdate=$(date '+%m-%d-%Y')
read "Enter date: MM-DD-YYYY"?vdate
awk -F, '/'"$vdate"'/ {print $1, $2}' /log/log.csv > log_mm_dd_yyyy

I hope this helps.

bakunin

Thanks Bakunin!

I'm getting the following error message:

line 4: read: `Enter date: MM-DD-YYYY?vdate': not a valid identifier

I've also tried switching the "?" inside the quotes, and just a minor change at the end of the output log, and I get the same result.

I'll try to debug a bit more.

#!/bin/bash
typeset vdate=$(date '+%m-%d-%Y')
read "Enter date: MM-DD-YYYY"?vdate
awk -F, '/'"$vdate"'/ {print $1, $2}' /log/log.csv > log_mm_dd_$vdate

Sorry, my bad. Actually it was a typo. The correct line should read (the text and the variables name should be reversed):

read vdate?"Enter date: MM-DD-YYYY"

I hope this helps.

bakunin

Actually it was my bad! I'm getting get the results now, however I would like additional format and column; allow me to illustrate.

This pretty much the log:

05-25-2010 21:45:49 1616 1615 0 0 0 0 0 0 0
05-23-2010 21:55:49 1495 1493 0 0 0 3 0 0 0
05-23-2010 22:05:49 1750 1750 0 0 0 0 0 0 0
05-24-2010 22:15:49 1839 1837 0 0 0 0 1 0 0
05-24-2010 22:25:49 1693 1687 0 0 0 3 1 0 1
05-25-2010 22:35:49 1614 1609 0 0 0 3 4 0 0
05-25-2010 22:45:49 1599 1594 0 0 0 0 3 0 0

I tried using the awk portion of your script, but couldn't get it to work.

#!/bin/bash
echo "You must type the date in this format ---> MM-DD-YYYY"
read vdate
grep $vdate /log/log.csv |awk -F, '{print $1, $2, $3, $12, $13, $14, $15, $16, $17,$18}' |tee /tmp/bk_$vdate.csv

However now I would like I would like the output to display:

  1. Time format instead of 22:35:49 I want just 22:35
  2. I would like to insert a percentage column between 3 and 5, please see below for the actual results.
1               2            3      4     5 6 7 8 9 1011
05-25-2010 22:35:49 1614 1609 0 0 0 3 4 0 0
05-25-2010 22:45:49 1599 1594 0 0 0 0 3 0 0

Actual output without date column

1        2     3      4       5 6 7 8 9 1011
22:35 1614 1609  96%  0 0 0 3 4 0 0

Percentage Calculation

$3/($2-$11)

I've been at this all night so I'm gonna try and get some sleep now.
Any help would be appreciated.

Thanks again Bakunin!