Speed Up Grep


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Speed Up Grep
# 1  
Old 02-02-2013
Speed Up Grep

Hi,

I have to grep string from 20 - 30 files each carries 200 - 300 MB size and append to the
file. How to speed the
grepping time.

Code:
cat catalina.out_2012_01_01 | grep "xxxxx"  >> backup.txt

PLZ, Suggest me,

Regards,
Nanthagopal A
# 2  
Old 02-02-2013
First of all do not use cat with grep

If you are searching for fixed strings try using fgrep instead which is much faster.
Code:
fgrep "xxxx" catalina.out_2012_01_01 > backup.txt

# 3  
Old 02-02-2013
Quote:
Originally Posted by bipinajith
First of all do not use cat with grep

If you are searching for fixed strings try using fgrep instead which is much faster.
Code:
fgrep "xxxx" catalina.out_2012_01_01 > backup.txt


I have change
Code:
grep

to
Code:
fgrep

and still same performance
# 4  
Old 02-02-2013
For multiple files (grep -F is the same as fgrep, you should use one of these)
This runs five searches at a time in parallel. So it is approximately 5 times faster than a single thread of grep, on modern multi-core cpus.
Code:
cnt=0;
for i in catalina.out* 
do
   cnt=$(( $cnt + 1 ))
   grep -F 'xxxxxx' $i >> backup.txt
   [ $(( $cnt % 5  ))  -eq 0 ]  && wait
done
wait

# 5  
Old 02-02-2013
Quote:
Originally Posted by jim mcnamara
For multiple files (grep -F is the same as fgrep, you should use one of these)
This runs five searches at a time in parallel. So it is approximately 5 times faster than a single thread of grep, on modern multi-core cpus.
Code:
cnt=0;
for i in catalina.out* 
do
   cnt=$(( $cnt + 1 ))
   grep -F 'xxxxxx' $i >> backup.txt
   [ $(( $cnt % 5  ))  -eq 0 ]  && wait
done
wait

Hi Jim,
Am I correct in assuming that you intended to have an & after backup.txt in this for loop?

Should the number of grep's to be run in parallel be tied to the number of CPU cores available to the process, or is grep I/O limited?
---------------------------
Added later: Are you sure that the output from grep is line-buffered? If not you could end up with output with parts of lines intermixed by running multiple greps writing to a single output file!

Last edited by Don Cragun; 02-02-2013 at 05:03 AM.. Reason: Additional question...
# 6  
Old 02-03-2013
You could try using the --mmap flag and process all files with 1 grep call (as there are only 20 - 30 files the command line length should be OK). This assumes more modern, GNU greps that support the mmap(2) system call to read files and that files don't shrink while grep is working:

Code:
grep -F --mmap -h "xxxxxx" catalina.out* >> backup.txt

Edit: use -F if your search string is fixed (ie not a regular expression).
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Can I speed up my grep command?

I've got at least 30,000 XML files that I'm using the grep command to get their filename. Can I use the head command to grab just the beginning 8 lines and compare that instead of parsing the whole document? It would speed things up! or maybe grep -m? (49 Replies)
Discussion started by: emc^24sho
49 Replies

2. Shell Programming and Scripting

How to speed up grep?

hi i'm greping the files with sepefic keyword, where the file is of too big. Assume there are 10 days log file each of more than 200mb. i've to grep all those files with a specific keywords. for example, 1. i'll grep for error message 2. after the i'll do one more grep for keyword... (4 Replies)
Discussion started by: vij_krr
4 Replies

3. Filesystems, Disks and Memory

data from blktrace: read speed V.S. write speed

I analysed disk performance with blktrace and get some data: read: 8,3 4 2141 2.882115217 3342 Q R 195732187 + 32 8,3 4 2142 2.882116411 3342 G R 195732187 + 32 8,3 4 2144 2.882117647 3342 I R 195732187 + 32 8,3 4 2145 ... (1 Reply)
Discussion started by: W.C.C
1 Replies

4. Shell Programming and Scripting

Speed up this script!

I have a script that processes a fair amount of data -- say, 25-50 megs per run. I'd like ideas on speeding it up. The code is actually just a preprocessor -- I'm using another language to do the heavy lifting. But as it happens, the preprocessing takes much more time than the final processing... (3 Replies)
Discussion started by: CRGreathouse
3 Replies

5. Shell Programming and Scripting

Optimizing for a Speed-up

How would one go about optimizing this current .sh program so it works at a more minimal time. Such as is there a better way to count what I need than what I have done or better way to match patterns in the file? Thanks, #declare variables to be used. help=-1 count=0 JanCount=0 FebCount=0... (3 Replies)
Discussion started by: switch
3 Replies

6. Filesystems, Disks and Memory

dmidecode, RAM speed = "Current Speed: Unknown"

Hello, I have a Supermicro server with a P4SCI mother board running Debian Sarge 3.1. This is the "dmidecode" output related to RAM info: RAM speed information is incomplete.. "Current Speed: Unknown", is there anyway/soft to get the speed of installed RAM modules? thanks!! Regards :)... (0 Replies)
Discussion started by: Santi
0 Replies

7. AIX

cpu speed

how do i determine the speed of a cpu on AIX 4.3.3 or 5.1? (5 Replies)
Discussion started by: csaunders
5 Replies

8. UNIX for Dummies Questions & Answers

Speed of mv vs. cp

Hi, Is mv (move) command quicker than cp (copy command)? I have large files and I want to know if mv actually copy the data to a new file then deletes the old or whether it just alters information the file system without physically moving data - Unfortuanately I don't have large files to test... (2 Replies)
Discussion started by: GMMike
2 Replies

9. UNIX for Dummies Questions & Answers

grep - speed of search

I grepped for a string from a directory of very large files. This took quite a long time (not a problem). When I grepped for a different string from the same files immediately after, the output was MUCH quicker. My question is, does anybody know why the second grep was so much quicker than the... (1 Reply)
Discussion started by: davirime
1 Replies

10. UNIX for Dummies Questions & Answers

Speed it up!

I wonder, are there any "tricks" to increase my server's access time in general? (4 Replies)
Discussion started by: pappous
4 Replies
Login or Register to Ask a Question