Searching in a file
In this recipe, we demonstrate how to search for certain words in a text file. You can find the code in the search.dart
script in the project searching_file
. As an example text file, we use taoprog.txt
.
How to do it...
The program is launched from the command-line in the bin
folder (or in Dart Editor with a Managed Launch with Script argum
ents -n search1 search2taoprog.txt
) as shown in the following screenshot:
In dart searc
h.dart -n search1 search2 taoprog.txt
, where search1
and search2
are words to be searched for, there can be one or more search words. For example, let's search for mysterious and machine, in which case, the output is as follows:
The flag –n
is optional; if included, we see a line number printed in front of the line.
The following is the code from the script:
import 'dart:io'; import 'package:args/args.dart'; const HOWTOUSE = 'usage: dart search.dart [-n] search-pattern file'; const LINENO = 'line-number'; ArgResults argResults; var searchTerms = "";...