LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-05-2015, 06:12 AM   #1
hristo77
LQ Newbie
 
Registered: Nov 2015
Posts: 9

Rep: Reputation: Disabled
NEWBIE: grep using yesterdays date?


Hi,

Im trying to grep for yesterdays date like this

Code:
cat logfile_rman.log | grep '\-*' -A 5 | grep 'date +%Y%m%d -d yesterday' -A 500 | grep 'RMAN-' -C 1 | wc -l

Tried this as well:

exp_date=$(date +%Y%m%d -d yesterday)

cat logfile_rman.log | grep '\-*' -A 5 | grep '$exp_date' -A 500 | grep 'RMAN-' -C 1 | wc -l
The code works if I replace date with '20151104', I then get a wc of 20.

Whats wrong?

Regards
H
 
Old 11-05-2015, 06:42 AM   #2
HMW
Member
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Red Hat, CentOS
Posts: 773
Blog Entries: 3

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
Quote:
Originally Posted by hristo77 View Post
Hi,

Im trying to grep for yesterdays date like this

Code:
cat logfile_rman.log | grep '\-*' -A 5 | grep 'date +%Y%m%d -d yesterday' -A 500 | grep 'RMAN-' -C 1 | wc -l

Tried this as well:

exp_date=$(date +%Y%m%d -d yesterday)

cat logfile_rman.log | grep '\-*' -A 5 | grep '$exp_date' -A 500 | grep 'RMAN-' -C 1 | wc -l
The code works if I replace date with '20151104', I then get a wc of 20.

Whats wrong?

Regards
H
Hi!

Get rid of the single quotes in your grep.

Like this:
Code:
echo "foo 20151104" | grep $exp_date 
foo 20151104
As you can see, this works as expected. But not:
Code:
$ echo "foo 20151104" | grep '$exp_date'
^This returns nothing.

Edit:
To add to the confusion, you can use double quotes:
Code:
echo "foo 20151104" | grep "$exp_date" 
foo 20151104
Bash can be very confusing at times!


Best regards,
HMW

Last edited by HMW; 11-05-2015 at 06:44 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] date option for grep ? tabbygirl1990 Linux - Newbie 3 08-19-2014 03:08 PM
grep Word and Date from file T-Dub116 Linux - Newbie 8 12-11-2013 11:15 PM
[SOLVED] log file grep by date and year JJJCR Linux - General 10 01-14-2013 09:24 PM
grep a directory with date order ust Linux - Newbie 4 02-16-2009 02:17 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:56 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration