This is documentation for Mathematica 8, which was
based on an earlier version of the Wolfram Language.
View current documentation (Version 11.2)

Searching Files

FindList["file","text"]get a list of all the lines in the file that contain the specified text
FindList["file","text",n]get a list of the first n lines that contain the specified text
FindList["file",{"text1","text2",...}] get lines that contain any of the

Finding lines that contain specified text.

Here is a file containing some text.
This returns a list of all the lines in the file containing the text .
Click for copyable input
The text appears nowhere in the file.
Click for copyable input
By default, FindList scans successive lines of a file, and returns those lines which contain the text you specify. In general, however, you can get FindList to scan successive records, and return complete records which contain specified text. As in ReadList, the option RecordSeparators allows you to tell Mathematica what strings you want to consider as record separators. Note that by giving a pair of lists as the setting for RecordSeparators, you can specify different left and right separators. By doing this, you can make FindList search only for text which is between specific pairs of separators.
This finds all "sentences" ending with a period which contain And.
Click for copyable input
option name
default value
RecordSeparators{"\n"}separators for records
AnchoredSearchFalsewhether to require the text searched for to be at the beginning of a record
WordSeparators{" ","\t"}separators for words
WordSearchFalsewhether to require that the text searched for appear as a word
IgnoreCaseFalsewhether to treat lowercase and uppercase letters as equivalent

Options for FindList.

This finds only the occurrence of which is at the beginning of a line in the file.
Click for copyable input
In general, FindList finds text that appears anywhere inside a record. By setting the option WordSearch->True, however, you can tell FindList to require that the text it is looking for appears as a separate word in the record. The option WordSeparators specifies the list of separators for words.
The text does appear in the file, but not as a word. As a result, the FindList fails.
Click for copyable input
FindList[{"file1","file2",...},"text"] search for occurrences of the text in any of the

Searching in multiple files.

This searches for in two copies of .
Click for copyable input
It is often useful to call FindList on lists of files generated by functions such as FileNames.
FindList["!command",...]run an external command, and find text in its output

Finding text in the output from an external program.

This runs the external Unix command in a text-based interface.
Click for copyable input
This finds the time-of-day field in the date.
Click for copyable input
OpenRead["file"]open a file for reading
OpenRead["!command"]open a pipe for reading
Find[stream,text]find the next occurrence of text
Close[stream]close an input stream

Finding successive occurrences of text.

FindList works by making one pass through a particular file, looking for occurrences of the text you specify. Sometimes, however, you may want to search incrementally for successive occurrences of a piece of text. You can do this using Find.
In order to use Find, you first explicitly have to open an input stream using OpenRead. Then, every time you call Find on this stream, it will search for the text you specify, and make the current point in the file be just after the record it finds. As a result, you can call Find several times to find successive pieces of text.
This opens an input stream for .
Click for copyable input
This finds the first line containing And.
Click for copyable input
Calling Find again gives you the next line containing And.
Click for copyable input
This closes the input stream.
Click for copyable input
Once you have an input stream, you can mix calls to Find, Skip, and Read. If you ever call FindList or ReadList, Mathematica will immediately read to the end of the input stream.
This opens the input stream.
Click for copyable input
This finds the first line which contains , and leaves the current point in the file at the beginning of the next line.
Click for copyable input
Read can then read the word that appears at the beginning of the line.
Click for copyable input
This skips over the next three words.
Click for copyable input
Mathematica finds in the remaining text, and prints the entire record as output.
Click for copyable input
This closes the input stream.
Click for copyable input
StreamPosition[stream]find the position of the current point in an open stream
SetStreamPosition[stream,n]set the position of the current point
SetStreamPosition[stream,0]set the current point to the beginning of a stream
SetStreamPosition[stream,Infinity]set the current point to the end of a stream

Finding and setting the current point in a stream.

Functions like Read, Skip, and Find usually operate on streams in an entirely sequential fashion. Each time one of the functions is called, the current point in the stream moves on.
Sometimes, you may need to know where the current point in a stream is, and be able to reset it. On most computer systems, StreamPosition returns the position of the current point as an integer giving the number of bytes from the beginning of the stream.
This opens the stream.
Click for copyable input
When you first open the file, the current point is at the beginning, and StreamPosition returns .
Click for copyable input
This reads the first line in the file.
Click for copyable input
Now the current point has advanced.
Click for copyable input
This sets the stream position back.
Click for copyable input
Now Read returns the remainder of the first line.
Click for copyable input
This closes the stream.
Click for copyable input