How do I grep a Date correctly


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How do I grep a Date correctly
# 1  
Old 03-09-2009
Question How do I grep a Date correctly

I am still a novice at this stuff, but I have searched everywhere and I cant seem to get this working.

I am using a database program that I need to pull information from. The command I am using is the following.

search /project | grep "date -v -1m "+%Y-%m""

This returns no results, however if I break it down it doesnt make sense to me because,

date -v -1m "+%Y-%m""
will return the result of "2009-02"

If I run the command
search /project | grep "2009-02"
I get the results I expect (every file with a creation date or modification date of 2009-02

If I create a variable x=2009-02
then run the command
search /project | grep "$x"
this works and returns all the files created or modified in 2009-02

So why is it that if I set my variable as the date of the previous month using
lastmonth=" date -v -1m "+%Y-%m""
and then run the search with
search /project | grep "$lastmonth"
gives me no results?

Also, FYI, I tried running the whole command together
/search /project | grep " date -v -1m "+%Y-%m""
and still got no results.

Please help. I feel like I've been going in circles on this for 2 days now.

Thanks

-- Jason
# 2  
Old 03-09-2009
If you do this:
var=date

echo $var
echo "$var"
you're just going to get the work "date" displayed. To run the date command and use its output, you need to do:
echo $(date)
echo $($var)
if you're using a modern shell. If you're using an antique shell:
echo `date`
echo `$var`

And next time please tell us which shell you're using.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Grep Regexp not working correctly

Consider the following code: grep -o -e '^STEAM_::\d+$' workfile3.tmp A sample format of a valid string for the regexp would be: STEAM_0:1:12345678 Here is an example line from the workfile3.tmp file: 465:L 01/02/2012 - 00:05:33: "Spartan1-1-7<8><STEAM_0:1:47539638><>" connected No... (2 Replies)
Discussion started by: spinner0205
2 Replies

2. Shell Programming and Scripting

Date not displaying correctly

Hi Experts, I tried to stay away from posting stuff here and asking for help. I want to print date valeu for a given variable and that is not working. #!/bin/bash START=`echo $1 | tr -d _`; FV=`echo $2` for (( c = 0 ; c < $FV ; c++ )) do # echo -n "`date --date="$START +$c day"... (12 Replies)
Discussion started by: PG3
12 Replies

3. Shell Programming and Scripting

grep with date

I have few files in one directory as below and I require the files that were created today... $ls -ltr -rw-r--r-- 1 abc abc 0 Dec 5 17:34 file4.txt -rw-r--r-- 1 abc abc 0 Dec 5 17:34 file5.txt -rw-r--r-- 1 abc abc 0 Dec 7 17:34 file6.txt -rw-r--r-- 1... (7 Replies)
Discussion started by: giridhar276
7 Replies

4. Shell Programming and Scripting

Calculating expiry date using date,sed,grep

Hi, I would greatly appreciate it if someone can help me with my problem. I have a crawler which collects spam URLs everyday & this data needs to be published in a blacklist. Here's the catch: The "Time To Live" (TTL) for each URL is 3 months (or whatever for that matter). If i see the... (5 Replies)
Discussion started by: r4v3n
5 Replies

5. Shell Programming and Scripting

Result of the grep is not storred correctly into the variable

I am facing a problem while storring the grep results into a variable. I need to count the occurence of the pattern \, in a file and store that in a variable. I have given the below command p=`grep -c '\\,' filename` But while echoing the variable, i am getting the total number of lines in... (2 Replies)
Discussion started by: renjithv
2 Replies

6. UNIX for Dummies Questions & Answers

Date command to obtain the last month is not working correctly..

Hello, I could not find the exactly same post here.. so I will explain what I did to get the last month using date command. I used date +%Y-%m -d "-1 months" to get the last month. However, the returned value of above command on 2009/10/31 was 2009 10 and not 2009 09.. and the... (9 Replies)
Discussion started by: tigersk
9 Replies

7. Shell Programming and Scripting

Grep/awk not getting the message correctly

I have a script which will take two file as the inputs and take the Value in file1 and search in file2 and give the output in Outputfile. #!/bin/sh #. ${HOME}/crossworlds/bin/CWSharedEnv.sh FILE1=$1 FILE2=$2 for Var in $(cat $FILE1);do echo $Var grep -i "$Var" $FILE2 done > Outputfile I... (2 Replies)
Discussion started by: SwapnaNaidu
2 Replies

8. UNIX for Dummies Questions & Answers

grep -A switch not working correctly with -m

egrep -A 7 -m 2 -h 'Date:|Time:' *.html this is showing only 2 line after the context of the 2nd found match. Is this a bug in grep? egrep -A 7 -m 2 -h 'Time:' *.html - this works correctly (2 Replies)
Discussion started by: zer0
2 Replies

9. Shell Programming and Scripting

Grep date from ls -l

I have a script which required the month and day as the input ex : ./script <Month> <date> from this I get the list of files to do further logics. The problem is when I assign these $1 and $2 to variables, and use grep command in the script ls -l |grep "$1 $2" it works fine for two... (1 Reply)
Discussion started by: GenMen
1 Replies

10. Shell Programming and Scripting

Is grep being used correctly?

My goal is to find files contain the "signal 11" string in a specific directory. I need the file details followed by the string. I wrote a script to test out and play with Shell command since it's my first time to write a Shell script. Let me cut the story short... this command line: if ... (2 Replies)
Discussion started by: sai0899
2 Replies
Login or Register to Ask a Question