LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-04-2003, 12:57 PM   #1
j-me
Member
 
Registered: Jan 2003
Location: des moines, ia
Distribution: suse RH
Posts: 129

Rep: Reputation: 17
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)
 
Old 08-04-2003, 01:01 PM   #2
MartBrooks
Member
 
Registered: May 2002
Location: London
Distribution: Debian
Posts: 388

Rep: Reputation: 31
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
 
Old 08-04-2003, 01:07 PM   #3
j-me
Member
 
Registered: Jan 2003
Location: des moines, ia
Distribution: suse RH
Posts: 129

Original Poster
Rep: Reputation: 17
awesome. thanks. guess i had always used cat to grep and didn't think of alternatives.

j-me
 
Old 08-04-2003, 02:04 PM   #4
Bebo
Member
 
Registered: Jul 2003
Location: Göteborg
Distribution: Arch Linux (current)
Posts: 553

Rep: Reputation: 31
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.
 
  


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
grep ?? can grep us variables? DaFrEQ Linux - Software 4 09-14-2005 12:22 PM
c compound conditional steve_96 Programming 9 06-04-2005 07:17 PM
warning: deprecated use of label at end of compound statement abk4523 Programming 1 03-06-2005 09:50 AM
ps -ef|grep -v root|grep apache<<result maelstrombob Linux - Newbie 1 09-24-2003 11:38 AM
Compound Problems bselzler Linux - Networking 0 09-09-2001 09:30 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:22 AM.

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