Projekt dgrep
Zielsetzung
Es wir ein Kommandozeilenprogramm erstellt, das einen String, definiert mit einem regulären Ausdruck, in Dateien, evt. in mehreren Dateibäumen, sucht.
Mit zahlreichen Optionen kann die Suche eingegrenzt und die Ausgabe spezifiziert werden.
Das Programm hat eine Erklärfunktion (Aufruf mit Option "--help" oder "-h").
Beschreibung
Usage: dgrep [<options>] <pattern> <file1> [<file2>] ..
Searches <pattern> in <file1> <file2>...
<fileN>: a filename pattern or a directory, e.g. "/opt" "opt/*.txt" "*.doc"
Examples:
dgrep -r "version.*2"
dgrep -V2 -i -r "happy|lucky" "*.txt" "--excluded=test.*" "--excluded-dirs=.git|.config"
<option>:
-c, --count Show only the count of lines with hits (per file)
-i, --ignore-case The search is case insensitive
-v, --invert-match Show lines that do not contain the pattern
--process-binaries Normally binary files will not be processed
-h, --help Show the command description
-H, --hits Show only the number of hits (per file): a line can contain many hits
-l, --list Show the filename only, not the matching lines
-r, --recursive Searches in all sub directories too
-w, --word The patter describes a word: a hit will be delimited by word boundaries
-x, --excluded A regular expression for files to skip, e.g. ".*.(bak|sic)"
-X, --excluded-dirs A regular expression for directory to skip, e.g. "test|.git|.config
-f, --format Describes the format of a found line
The format is a string with placeholders.
Example: The name of the file is "/doc/data.txt", the search pattern is "(\w+).* (\d+\.\d\d) "
the found line is "apples: 20 kg 20.00 EUR"
%f% full name of the file, e.g. "/doc/data.txt"
%p% the path of the file, e.g. "/doc"
%n% the node, e.g. "data.txt"
%F% the filename, e.g. "data"
%e% the extension, e.g. ".txt"
%#% the line number
%h% the hit, e.g. ": 20 "
%c% the count of hit lines. Only meaningful together with option "--count"
%l% the line with the hit, e.g. "apples: 20 kg 20.00 EUR"
%1% the first group, e.g. "apple"
%2% the 2.nd group, e.g. "20.00"
...
%9% the 9.th group, e.g. "" (not found)
A meaningful format of the above example: "%f%: product: %1% price: %2%"
-F, --format-context Describes the format of a context line (see above-context...)
The format is a string with placeholders like the option --format.
-a, --above-context That number of lines above the hit line will be showed
(defaults to "0")
-C, --context That number of lines above and below the hit line will be showed
(defaults to "0")
-b, --below-context That number of lines below the hit line will be showed
(defaults to "0")
--break-lines Stops the search in a file if that number of matching lines is reached
--exit-lines Stops the search if that number of matching lines is reached (over all files)
--exit-files Stops the search if that number of files with matching lines is reached
-V, --verbose-level 0: no additional info 1: summary 2: detail 3: loop 4: fine
(defaults to "0")
Kenntnisse
- Umgang mit Verzeichnissen
- Unittests
- Generator
Projekt erstellen
- IdeaIC starten
- Menü:
File-New- <Project> - Linke Spalte:
Dart - Dart-SDK:
- Windows: c:\dart-sdk
- Linux: ~/dart-sdk
- Es erscheint eine Listbox, dort
Console-Application a command-line application sampleanklicken Next-Button- Project name:
dgrep - Project loction:
- Linux:
~/dev/dgrep - Windows:
c:\dev\dgrep
- Linux:
Finish-Button
Damit ist das Projekt names dgrep erstellt.
Projekt erkunden
- Linke Spalte:
Projectsanklicken - Es wird ein Ordner namens
dgrepsichtbar - auf das Symbol ">" klicken, dann klappt das Verzeichnis aus
- Im Unterverzeichnis
binliegt die Datei mit dem Hauptprogramm,dgrep.dart. - Herunterladen der Datei Zip-Datei und diese im Projektverzeichnis (
c:\dev\dgrepoder~/dev/dgrep) entpacken.
dgrep.dart
Wir tragen ein:
import 'package:dgrep/search_engine.dart';
void main(List<String> arguments) {
SearchEngine.execute(arguments);
}
package:dgrep/search_engine.dartWir importieren die KlasseSearchEngine.SearchEngine.execute(arguments);Die KlasseSearchEnginebesitzt eine statische Methodeexecute(), die als Parameter die Liste der Programmargument erhält und die Suche erledigt.
helper.dart
Durch das obige Entpacken der Zip-Datei gibt es die Datei lib/helper.dart.
Die helper.dart enthält nur einzelne Funktionen, die in dgrep-helper.dart beschrieben werden.
search_engine.dart
Durch das obige Entpacken der Zip-Datei gibt es die Datei lib/search_engine.dart. Es folgt eine Besprechung von Ausschnitten der Datei.
- Im Unterverzeichnis
lib liegt eine zweite Datei dsuche.dart, die ergänzenden Code enthält. In diesem Projekt brauchen wir diese Datei nicht. also löschen: Rechte Maustaste - Delete
- ziemlich unten gibt es die Datei
pubspec.yaml Dort werden Pakete eingetragen.
Programmcode in dsuche.dart
Kopiere folgenden Code in die Datei bin\dsuche.dart
import 'dart:io';
/// Durchsuchen einer Datei nach einem regulären Ausdruck.
/// [args]: Programm-Argumente, z.B. [r'https?://', 'rechnung.txt']
void main(List<String> args) {
if (args.length < 2){
print('Gebrauch: dsuche <suchmuster> <date>\nBeispiel: dsuche "https?://" rechnung.txt');
} else {
final lines = File(args[1]).readAsLinesSync();
RegExp regExp = RegExp(args[0], caseSensitive: false);
for (var line in lines){
if (regExp.firstMatch(line) != null){
print(line);
}
}
}
}
Programm in der Entwicklungsumgebung starten
- In der Toolbar den grünen Hammer
Add configuration klicken
- Es geht eine Dialogbox auf: Oben links auf das
+ klicken
- In der Liste
Dart Command Line App
- Name:
dsuche
- Dart file: Hier auf das Ordnersymbol in dem Textfeld klicken, dort Ordner
dsuche aufklappen, im Unterordner bin die Datei dsuche.dart anklicken.
- Program arguments:
- Windows:
https?:// c:\dev\links.txt
- Linux:
https?:// ~/dev/links.txt
- Button
Ok
- Im Entwicklungsordner (Windows: c:\dev, Linux: ~/dev/) eine Datei links.html anlegen:
Dies ist eine Testdatei
Suchen geht mit https://google.com
Spezialwissen findet sich auf http://wikipedia.de
Hab Spaß mit Programmieren.
Programm compilieren
Windows
- Eingabeaufforderung starten
gdev dsuche
dcompile dsuche
Danach gibt es die Datei c:\dev\bin\dsuche.exe
Dieses Programm kann in der Eingabeaufforderung gestartet werden.
gdev
dsuche https?:// links.txt