LinuxQuestions.org
Register a domain and help support LQ
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 05-10-2011, 02:01 PM   #1
dhschryer
LQ Newbie
 
Registered: May 2011
Location: Ottawa Canada
Posts: 2

Rep: Reputation: 0
Execute series of grep commands in script ?


Very nubie ... green even ...

Executing series of grep commands to get counts in a large file. I'm appending to this file. Series of commands will be;

"greg -ir "May/0504" SentEmails.txt | wc -l >> Stats.txt
"greg -ir "May/0505" SentEmails.txt | wc -l >> Stats.txt
"greg -ir "May/0506" SentEmails.txt | wc -l >> Stats.txt
"greg -ir "May/0507" SentEmails.txt | wc -l >> Stats.txt

Can I somehow script this ?

D
 
Old 05-10-2011, 02:07 PM   #2
SL00b
Member
 
Registered: Feb 2011
Location: LA, US
Distribution: SLES
Posts: 375

Rep: Reputation: 112Reputation: 112
The short answer is, if it can be done on the command line, it can be scripted.

Here's a link to help you get started: http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html
 
Old 05-10-2011, 02:22 PM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Sure, as in so many cases, a simple for loop will do.

Code:
for i in 04 05 06 07 ; do
	grep -ic "may/05$i" SentEmails.txt >> Stats.txt
done
You don't need -r (recursive search) for a single file. But you can use -c to get a line count, instead of piping it through wc.

Just add whatever sequence of numbers or strings you need to the loop. Each one will be used in the variable ($i) in turn. You can even use brace expansion to generate the list:
Code:
#Bash version 4 has zero-padding:
for i in {01..31} ; do

#For previous versions use this:
for i in {0{1..9},{10..31}} ; do
Here are a few useful bash scripting references:
http://www.linuxcommand.org/index.php
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/index.html
http://www.gnu.org/software/bash/manual/bashref.html
http://wiki.bash-hackers.org/start

And please use [code][/code] tags around your code, to preserve formatting and to improve readability.
 
Old 05-13-2011, 11:31 AM   #4
dhschryer
LQ Newbie
 
Registered: May 2011
Location: Ottawa Canada
Posts: 2

Original Poster
Rep: Reputation: 0
Nice ... works great !

And if I wanted to count number of emails "by hour" for each day ?

Dave
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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] Script which will login to particular user and execute some commands. linux_beginer Linux - Newbie 6 03-20-2011 02:06 PM
hi ,i have to grep a series say 98782 from a file containing series how can i grep it sushil.cool Linux - Newbie 6 05-25-2010 07:27 AM
bash - how to launch a console window via script and execute commands in it? basd Programming 4 01-26-2008 09:51 PM
Run one script, but execute commands at the same time Micro420 Programming 8 07-18-2007 01:05 AM


All times are GMT -5. The time now is 12:17 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration