Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cheap way to search a large text file for a string

Tags:

python

I need to search a pretty large text file for a particular string. Its a build log with about 5000 lines of text. Whats the best way to go about doing that? Using regex shouldn't cause any problems should it? I'll go ahead and read blocks of lines, and use the simple find.

like image 459
iman453 Avatar asked Oct 08 '10 19:10

iman453


People also ask

How do you search a file for a specific string of text?

You need to use the grep command. The grep command or egrep command searches the given input FILEs for lines containing a match or a text string.

How do I find large text files?

To be able to open such large CSV files, you need to download and use a third-party application. If all you want is to view such files, then Large Text File Viewer is the best choice for you. For actually editing them, you can try a feature-rich text editor like Emacs, or go for a premium tool like CSV Explorer.

How do you search for a specific word in a large text file in Java?

Use a method from Scanner object - FindWithinHorizon. Scanner will internally make a FileChannel to read the file. And for pattern matching it will end up using a Boyer-Moore algorithm for efficient string searching.


1 Answers

If it is "pretty large" file, then access the lines sequentially and don't read the whole file into memory:

with open('largeFile', 'r') as inF:     for line in inF:         if 'myString' in line:             # do_something 
like image 77
eumiro Avatar answered Sep 23 '22 03:09

eumiro