LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   compound grep (https://www.linuxquestions.org/questions/linux-newbie-8/compound-grep-78930/)

j-me 08-04-2003 12:57 PM

compound grep
 
I am needing to grep a file to extract the date 'Aug 04'. I am using a script to automate this as I need it to scan the file and email the output.

<script>
works (but not completely). I get all Aug data but I also get anything that has Aug and 04 on the line:

file04 Aug 01 ....

#!/bin/sh
nowmonth=`date +%b`
nowday=`date +%d`
cat /home/input | grep $nowmonth | grep $nowday > mail -s .....



I really want to use:
cat /home/input | grep $nowmonth $nowday > mail -s

I try it but it gives me errors after the $nowmonth.
grep: 04: No such file or directory.

but it just does not seem to work right. Any ideas?

(using RH 8.0)

MartBrooks 08-04-2003 01:01 PM

Re: compound grep
 
Quote:

Originally posted by j-me

I really want to use:
cat /home/input | grep $nowmonth $nowday > mail -s

I try it but it gives me errors after the $nowmonth.
grep: 04: No such file or directory.

but it just does not seem to work right. Any ideas?

1) You don't need to use cat as grep will read a file directly
2) Quote the string you want to grep for.

So:

grep "$nowmonth $nowday" /home/input | mail foo@bar.com

j-me 08-04-2003 01:07 PM

awesome. thanks. guess i had always used cat to grep and didn't think of alternatives.

:) j-me

Bebo 08-04-2003 02:04 PM

An alternative to quoting is to make use of the regexps:

Code:

grep $nowmonth\ $nowday /home/input | mail foo@bar.com
Note the use of "\ " which is the regexp of space instead of quotes.


All times are GMT -5. The time now is 03:37 PM.