Grepping Date Variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grepping Date Variable
# 1  
Old 05-25-2010
Java 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!

HTML Code:
#!/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
# 2  
Old 05-25-2010
Quote:
Originally Posted by ravzter
I did some searching, but I still don't understand why I'm not seeing any result.
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. :

Code:
#!/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
# 3  
Old 05-25-2010
Thanks Bakunin!

I'm getting the following error message:

HTML Code:
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.

HTML Code:
#!/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

Last edited by ravzter; 05-25-2010 at 10:45 PM..
# 4  
Old 05-26-2010
Quote:
Originally Posted by ravzter
Code:
line 4: read: `Enter date: MM-DD-YYYY?vdate': not a valid identifier

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

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

I hope this helps.

bakunin
# 5  
Old 05-26-2010
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:

HTML Code:
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.

HTML Code:
#!/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.
HTML Code:
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
HTML Code:
1        2     3      4       5 6 7 8 9 1011
22:35 1614 1609  96%  0 0 0 3 4 0 0
Percentage Calculation
HTML Code:
$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!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grepping for one variable while using awk to parse an associated variable

Im trying to search for a single variable in the first field and from that output use awk to extract out the lines that contain a value less than a value stored in another variable. Both the variables are associated with each other. Any guidance is appreciated. File that contains the... (6 Replies)
Discussion started by: ncwxpanther
6 Replies

2. UNIX for Beginners Questions & Answers

How to replace a parameter(variable) date value inside a text files daily with current date?

Hello All, we what we call a parameter file (.txt) where my application read dynamic values when the job is triggered, one of such values are below: abc.txt ------------------ line1 line2 line3 $$EDWS_DATE_INSERT=08-27-2019 line4 $$EDWS_PREV_DATE_INSERT=08-26-2019 I am trying to... (1 Reply)
Discussion started by: pradeepp
1 Replies

3. Shell Programming and Scripting

Grepping the data between 2 date ranges

Hi There, Good Day !! I have txt file containing data in the below format. There are many lines, here i have mentioned for example. cat remo.txt 2/3/2017 file1 3/4/2016 file2 6/6/2015 file5 1/1/2018 file3 4/3/2014 file4 - - - I need to grep the file names for given date rage... (11 Replies)
Discussion started by: kumar85shiv
11 Replies

4. Shell Programming and Scripting

How to handle grepping variable data containing wildcards?

I have a lot of files with keywords and unique names. I'm using a shell script to refer to a simple pattern file with comma separated values in order to match on certain keywords. The problem is that I don't understand how to handle the wildcard values when I want to skip over the unique names. ... (5 Replies)
Discussion started by: abercrom
5 Replies

5. Shell Programming and Scripting

Convert a date stored in a variable to epoch date

I am not able to pass date stored in a variable as an argument to date command. I get current date value for from_date and to_date #!/usr/bin/ksh set -x for s in server ; do ssh -T $s <<-EOF from_date="12-Jan-2015 12:02:09" to_date="24-Jan-2015 13:02:09" echo \$from_date echo... (7 Replies)
Discussion started by: raj48
7 Replies

6. Shell Programming and Scripting

Grepping the file into a variable after SSH into a server

Hi All, When i am logged into a server , i am able to assign grep value to a variable as follows. VAR=`grep 'Listener stopped' /logs/perf.log` However, when i log out of the server, and try to execute the following command by first SSHing into server, it throws error. $ VAR=`ssh Server... (4 Replies)
Discussion started by: srkmish
4 Replies

7. Shell Programming and Scripting

Grepping for variable in brackets

I have a script which reads a number out of a log file. The pertinent line is this: cat /tmp/listofnumbers When I run cat /tmp/listofnumbers what I am seeing is on each line. I am trying to make the script read from that file and grep for a variable like the following line should do: ... (4 Replies)
Discussion started by: newbie2010
4 Replies

8. Shell Programming and Scripting

Display file date after grepping a string in the file

Hi All, I need to recursively grep several folders for a MAC address and display the results with the date of the file name at the start. Even better would be if the final results were displayed chronologically so the newest file is always at the end. Oldest at the top, regardless of what... (8 Replies)
Discussion started by: quemalr
8 Replies

9. Shell Programming and Scripting

Grepping file and returning passed variable if the value does not exist in file at all.

I have a list of fields that I want to check a file for, returning that field if it not found at all in the file. Is there a way to do a grep -lc and return the passed variable too rather then just the count? I am doing some crappy work-around now but I was not sure how to regrep this for :0 so... (3 Replies)
Discussion started by: personalt
3 Replies

10. UNIX for Dummies Questions & Answers

grepping a variable

I need to pass a parameter that will then be grepped. I need it to grep /paramater and then have a space so if 123 was passed my grep would be grep '/123 ' sample.log this works fine from the command line but when i try and set it searchThis="/$2 " and then run grep $searchThis... (6 Replies)
Discussion started by: magnia
6 Replies
Login or Register to Ask a Question