File: sgrep.1

package info (click to toggle)
sgrep 1.94a-4
  • links: PTS
  • area: main
  • in suites: bullseye, buster, jessie, jessie-kfreebsd, sid, squeeze, stretch, wheezy
  • size: 880 kB
  • ctags: 1,074
  • sloc: ansic: 10,566; sh: 3,433; makefile: 134
file content (1160 lines) | stat: -rw-r--r-- 24,938 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
.if n .ds Q \&"
.if t .ds Q ``
.if n .ds U \&"
.if t .ds U ''
.TH "SGREP" 1 
.tr \&
.nr bi 0
.nr ll 0
.nr el 0
.de DS
..
.de DE
..
.de Pp
.ie \\n(ll>0 \{\
.ie \\n(bi=1 \{\
.nr bi 0
.if \\n(t\\n(ll=0 \{.IP \\(bu\}
.if \\n(t\\n(ll=1 \{.IP \\n+(e\\n(el.\}
.\}
.el .sp 
.\}
.el \{\
.ie \\nh=1 \{\
.LP
.nr h 0
.\}
.el .PP 
.\}
..
.SH NAME

.Pp
sgrep - search a file for a structured pattern
.SH SYNOPSIS

.Pp
\fIsgrep \fP
[\fB-aCcDdhiIlNnPqSsTtV\fP]
[\fB-g\fP \fIoption\fP] 
[\fB-O\fP \fIfilename\fP]
[\fB-o\fP "\fIformat\fP"]
[\fB-p\fP \fIpreprocessor\fP] 
[\fB-w\fP \fIchar list\fP] 
[\fB-x\fP \fIfilename\fP] 
[\fB-e\fP] 
\fIexpression\fP
[\fIfilename \fP ...]
.Pp
\fIsgrep\fP
[\fB-aCcDdhiIlNnPqSsTtV\fP]
[\fB-g\fP \fIoption\fP] 
[\fB-O\fP \fIfilename\fP] 
[\fB-o\fP "\fIformat\fP"]
[\fB-p\fP \fIpreprocessor\fP] 
[\fB-w\fP \fIchar list\fP] 
[\fB-x\fP \fIfilename\fP] 
\fB-f\fP \fIfilename\fP
[\fB-e \fP \fIexpression\fP] 
[\fIfilename\fP ...] 
.Pp
\fIsgrep\fP
[\fB-aCcDdhiIlNnPqSsTtV\fP]
[\fB-g\fP \fIoption\fP] 
[\fB-O\fP \fIfilename\fP] 
[\fB-o\fP "\fIformat\fP"]
[\fB-p\fP \fIpreprocessor\fP] 
[\fB-w\fP \fIchar list\fP] 
[\fB-x\fP \fIfilename\fP] 
\fB-f\fP \fIfilename\fP
\fB-F\fP \fIfilename\fP
[\fB-e \fP \fIexpression\fP] 
.Pp
\fIsgrep \fP
\fB-h\fP
.SH DESCRIPTION

.Pp
\fIsgrep (structured grep)\fP
is a tool for searching 
\fItext files\fP
and filtering 
\fItext streams\fP
using structural criteria.  The data model of sgrep is based on
\fIregions\fP,
which are non-empty substrings of text. 
\fIRegions \fP
are typically occurrences of constant strings or 
meaningful text elements, 
which are recognizable through some delimiting strings.
Regions 
can be arbitrarily long, arbitrarily 
overlapping, and arbitrarily nested.
.Pp
\fBsgrep\fP
uses patterns called
\fIregion expressions\fP
to express which
regions
of the input text are output to standard output. The selection of
regions 
is based on mutual  
\fIcontainment and ordering conditions \fP
of the regions, 
expressed by the region expression.
.Pp
\fIRegion expressions\fP
are read by default first from file
\fB$HOME/.sgreprc,\fP
or if it doesn't exist,
from file
\fB/usr/lib/sgreprc, \fP
and then from the command line. Different behavior
can be specified through command line options.
.Pp
Input 
\fIfiles \fP
are processed one by one (i.e., regions 
cannot extend over file boundaries), except if the
\fB-S\fP
flag is given, in which case
\fBsgrep\fP
takes the concatenation of the input 
files as its input text. 
If no input files are  given,
\fBsgrep\fP
reads the standard input.
Standard input can also be specified as an input file by giving 
hyphen '-' as a file name.
.Pp
The selected regions are output in increasing order of their start positions. 
If several output regions overlap, a minimal region that covers them all 
is output, by default, instead of outputting each of them separately.  
.Pp
.SH OPTIONS

.Pp
.nr ll +1
.nr t\n(ll 2
.if \n(ll>1 .RS
.IP "\fB-a\fP"
.nr bi 1
.Pp
Act as a filter: display the matching regions,
possibly formatted according to the output format, 
interleaved with the rest of the text.
(See the description of option -o below.)
.Pp
.IP "\fB-C \fP"
.nr bi 1
.Pp
Display copyright notice.
.IP "\fB-c \fP"
.nr bi 1
.Pp
Display only the count of the regions that match the expression.
.IP "\fB-D\fP"
.nr bi 1
.Pp
Display verbose progress output. 
\fBNOTE:\fP
This is used for debugging purposes only and may not function in
future versions of sgrep.
.IP "\fB-d \fP"
.nr bi 1
.Pp
Display each matching region once, even if the regions overlap or nest.
.IP "\fB-e\fP \fIexpression\fP"
.nr bi 1
.Pp
Search the input text for occurrences of 
\fIexpression\fP.
.IP "\fB-f\fP \fIfile\fP"
.nr bi 1
.Pp
Read the region expression from the named file. Filename
\fB- \fP
refers to stdin.
.IP "\fB-F\fP \fIfilename\fP"
.nr bi 1
.Pp
Read list of input files from \fIfilename\fP instead of command line
.IP "\fB-g\fP \fIoption\fP"
.nr bi 1
.Pp
Set scanner option. \fIoption\fP can be any of:
.nr ll +1
.nr t\n(ll 2
.if \n(ll>1 .RS
.IP "\fBsgml\fP"
.nr bi 1
.Pp
use SGML scanner
.IP "\fBhtml\fP"
.nr bi 1
.Pp
use HTML scanner (currently same as SGML scanner)
.IP "\fBxml\fP"
.nr bi 1
.Pp
use XML scanner
.IP "\fBsgml-debug\fP"
.nr bi 1
.Pp
show recognized SGML tokens
.IP "\fBinclude-entities\fP"
.nr bi 1
.Pp
automatically include system entities
.if \n(ll>1 .RE
.nr ll -1
.IP "\fB-h\fP"
.nr bi 1
.Pp
Display a short help.
.IP "\fB-i\fP"
.nr bi 1
.Pp
Ignore case distinctions in phrases.
.IP "\fB-I\fP"
.nr bi 1
.Pp
Switches to indexing mode, when given as first option
.IP "\fB-l\fP"
.nr bi 1
.Pp
Long output format: precede each output region by a 
line which indicates the
ordinal number of the region, the name of the file where the region starts,
the length of the region in bytes, the start and end positions of 
the region within the entire input text, the start position of the 
region within the file containing the start, and the end position 
of the region within the file containing the end.
.IP "\fB-N\fP"
.nr bi 1
.Pp
Do not add a newline after the last output region.
.IP "\fB-n\fP"
.nr bi 1
.Pp
Suppress reading \fB$HOME/.sgreprc\fP or 
\fB/usr/lib/sgreprc.\fP
.IP "\fB-O\fP file"
.nr bi 1
.Pp
Read the output format from file. See the description of output
formats below.
.IP "\fB-o\fP format"
.nr bi 1
.Pp
Set the output format.
The format is displayed for each output region 
with any occurrences of the following place holders substituted:
.nr ll +1
.nr t\n(ll 2
.if \n(ll>1 .RS
.IP "\fB%f\fP"
.nr bi 1
.Pp
name of the file containing the start of the region
.IP "\fB%s \fP"
.nr bi 1
.Pp
start position of the region
.IP "\fB%e \fP"
.nr bi 1
.Pp
end position of the region
.IP "\fB%l \fP"
.nr bi 1
.Pp
length of the region in bytes (i.e., \fB%e-%s+1\fP) 
.IP "\fB%i\fP"
.nr bi 1
.Pp
start position of the region in the file where the region begins 
.IP "\fB%j\fP"
.nr bi 1
.Pp
end position of the region in the file where the region ends
.IP "\fB%r\fP"
.nr bi 1
.Pp
text of the region. "%r" is the default output format.
.IP "\fB%n\fP"
.nr bi 1
.Pp
gets the ordinal number of the region
.if \n(ll>1 .RE
.nr ll -1
.IP "\fB-P\fP"
.nr bi 1
.Pp
Display the (preprocessed) region expression without executing it.
.IP "\fB-p\fP \fIpreprocessor\fP"
.nr bi 1
.Pp
Apply 
\fIpreprocessor \fP
to the region expression before evaluating it. 
.IP "\fB-S\fP"
.nr bi 1
.Pp
Stream mode. With this option sgrep considers 
it's input files as a continuous stream, so
that regions may extend across file boundaries.
.DS
.sp
.ft CR
.nf
    sgrep -S file_1 ... file_n
.DE
.fi 
.ec
.ft P
.sp

is similar to
.DS
.sp
.ft CR
.nf
    cat file_1 ... file_n | sgrep
.DE
.fi 
.ec
.ft P
.sp

except that the latter creates a temporary disk file of the input
stream.  Sgrep may use much more memory when run with the 
\fB-S\fP option, since then it cannot release its internal 
region lists between processing each file.
.IP "\fB-s\fP"
.nr bi 1
.Pp
Short output format (default): do not format the text of the 
output regions, and display overlapping parts of regions only once.
.IP "\fB-T\fP"
.nr bi 1
.Pp
Display statistics about the execution.
.IP "\fB-t\fP"
.nr bi 1
.Pp
Display time usage.
.IP "\fB-V\fP"
.nr bi 1
.Pp
Display version information.
.IP "\fB-v\fP"
.nr bi 1
.Pp
Verbose mode. Shows what is going on.
.IP "\fB-w\fP \fIchar list\fP"
.nr bi 1
.Pp
Set the list of characters used to recognize words.
.IP "\fB-x\fP \fIfilename\fP"
.nr bi 1
.Pp
Use given index file instead of scanner. Implies -S.
.IP "\fB--\fP"
.nr bi 1
.Pp
No more options.
.if \n(ll>1 .RE
.nr ll -1
.Pp
A list of options can be given also as the value of the 
environment variable
\fBSGREPOPT\fP.
.SH SYNTAX OF EXPRESSIONS

.Pp
.DS
.sp 
.ft CR
.nf
region_expr ->   basic_expr 
               | operator_expr

operator_expr -> region_expr ['not'] 'in' basic_expr
               | region_expr ['not'] 'containing' basic_expr
               | region_expr ['not'] 'equal' basic_expr
               | region_expr 'or' basic_expr
               | region_expr 'extracting' basic_expr
               | region_expr '..' basic_expr
               | region_expr '_.' basic_expr
               | region_expr '._' basic_expr
               | region_expr '__' basic_expr
               | region_expr 'quote' basic_expr
               | region_expr '_quote' basic_expr
               | region_expr 'quote_' basic_expr
               | region_expr '_quote_' basic_expr
               | 'concat' '(' region_expr ')'
               | 'inner' '(' region_expr ')'
               | 'outer' '(' region_expr ')'
               | 'join' '(' integer ',' region_expr ')'

basic_expr ->   phrase
              | 'start'
              | 'end'
              | 'chars'
              | constant_list
              | '(' region_expr ')'

phrase -> '"' char [ char ... ] '"'

constant_list -> '[' ']' | '[' regions ']'

regions ->   region 
           | region regions

region -> '(' integer ',' integer ')' 
.DE
.fi 
.ec
.ft P
.sp
.Pp
Note that region expressions are left-associative. This means, for example,
that an expression 
.DS
.sp
.ft CR
.nf
     '"<a>".."</a>" or "</b>"'
.DE
.fi 
.ec
.ft P
.sp

evaluates to the regions starting with
\f(CR"<a>"\fP and ending with
\f(CR"</a>"\fP,
or comprising only the string
\f(CR"</b>"\fP.
In order to obtain the regions that begin with
\f(CR"<a>"\fP
and end with either
\f(CR"</a>"\fP or
\f(CR"</b>"\fP,
one should indicate the proper order of evaluation using
parentheses:
.Pp
.DS
.sp
.ft CR
.nf
     "<a>".. ("</a>" or "</b>")
.DE
.fi 
.ec
.ft P
.sp
.Pp
.Pp
Expressions can also contain 
\fIcomments\fP,
which start with '#' and
extend to the end of the line. However, a '#'-sign in a phrase does not
begin a comment.
.Pp
.SH SEMANTICS OF EXPRESSIONS

.Pp
The value of an expression is a set of regions of input text
that satisfy the expression.
.Pp
Value \fBv(basic_expr)\fP
of a basic expression:
.Pp
.nr ll +1
.nr t\n(ll 2
.if \n(ll>1 .RS
.IP "\fBv(phrase):= \fP"
.nr bi 1
.Pp
the set of regions of input text whose text equals the text of the
phrase.
.IP "\fBv('start'):=\fP"
.nr bi 1
.Pp
a set consisting of single-character regions for the first position
of each input file. If the -S option is given, the value is a set
containing a single region that comprises the
first character in the input stream.
.IP "\fBv('end'):=\fP"
.nr bi 1
.Pp
a set consisting of single-character regions for the last position
of each input file. If the -S option is given, the value is a set
containing a single region that comprises the
last character in the input stream.
.IP "\fBv('chars'):= \fP"
.nr bi 1
.Pp
a set consisting of all single-character regions.
.IP "\fBv([ ]):= \fP"
.nr bi 1
.Pp
an empty set.
.IP "\fBv([(s_1,e_1) (s_1,e_2) ... (s_n,e_n)]):= \fP"
.nr bi 1
.Pp
a set consisting of regions 
\fBr_i \fP
for each 
\fBi = 1,...,n,\fP
where the start position of region 
\fBr_i \fP
is 
\fBs_i \fP
and its end position is 
\fBe_i. \fP
The positions have to be nonnegative integers, and the regions have
to be given in increasing order of their start positions; regions with
a common start positions have to be given in increasing order of their 
end positions. The positions are counted from the first character
of each input file, unless the -S option is given, in which case
the positions are counted starting from the beginning of the input
stream. The number of the first position in a file or a stream is
zero.
.IP "\fBv('('region_expr')'):= v(region_expr).\fP"
.nr bi 1
.Pp
.if \n(ll>1 .RE
.nr ll -1
.Pp
.Pp
Value \fBv(operator_expr)\fP
of operator expressions:
.nr ll +1
.nr t\n(ll 2
.if \n(ll>1 .RS
.IP "\fBv(region_expr 'in' basic_expr):=\fP"
.nr bi 1
.Pp
the set of the regions in \fBv(region_expr) \fP
that are contained in some region in \fBv(basic_expr)\fP.
A region \fBx\fP is contained in another region \fBy\fP if and
only if the start position of \fBx\fP is greater than the start
position of \fBy\fP and the end position of \fBx\fP is not greater
than the end position of \fBy\fP, or the  end position of \fBx\fP
is smaller than the end position of \fBy\fP and the start position of
\fBx\fP is not smaller than the start position of \fBy\fP.
.IP "\fBv(region_expr 'not' 'in' basic_expr):=\fP"
.nr bi 1
.Pp
the set of the regions in
\fBv(region_expr) \fP
that are not contained in any region in 
\fBv(basic_expr).\fP
.IP "\fBv(region_expr 'containing' basic_expr):=\fP"
.nr bi 1
.Pp
the set of the regions in 
\fBv(region_expr) \fP
that contain some region in 
\fBv(basic_expr).\fP
.IP "\fBv(region_expr 'not' 'containing' basic_expr):=\fP"
.nr bi 1
.Pp
the set of the regions in 
\fBv(region_expr) \fP
that do not contain any region in
\fBv(basic_expr).\fP
.IP "\fBv(region_expr 'equal' basic_expr):=\fP"
.nr bi 1
.Pp
The set of regions, which occur in both
\fBv(region_expr) \fP
and
\fBv(basic_expr).\fP
.IP "\fBv(region_expr 'not equal' basic_expr):=\fP"
.nr bi 1
.Pp
The set of regions, which occur in
\fBv(region_expr) \fP
but do not occur in
\fBv(basic_expr).\fP
.IP "\fBv(region_expr 'or' basic_expr):=\fP"
.nr bi 1
.Pp
the set of the regions that appear in
\fBv(region_expr) \fP
or in
\fBv(basic_expr) \fP
or in both.
.IP "\fBv(region_expr 'extracting' basic_expr):=\fP"
.nr bi 1
.Pp
the set of the non-empty regions that are formed of the 
regions in
\fBv(region_expr) \fP
by extracting an overlap with any region in
\fBv(basic_expr).\fP
For example, the value of 
.Pp
.DS
.sp
.ft CR
.nf
    '[(1,4) (3,6) (7,9)] extracting [(2,5) (4,7)]' 
.DE
.fi 
.ec
.ft P
.sp
.Pp
consists of the regions (1,1) and (8,9). 
.IP "\fBv(region_expr '..' basic_expr):\fP"
.nr bi 1
.Pp
The value of this expression consists of the regions that can be formed by
\fIpairing \fP
regions from 
\fBv(region_expr) \fP
with regions from 
\fBv(basic_expr).\fP
The pairing is defined as a generalization of the way how nested
parentheses are paired together "from inside out". 
For this we need to be able to compare the order of regions,
which may be overlapping and nested. This ordering is defined as follows.
.Pp
Let 
\fBx \fP
and 
\fBy \fP
be two regions. We say that region 
\fBx \fP
\fIprecedes \fP
region
\fBy \fP
if the end position of 
\fBx \fP
is smaller than the start position of 
\fBy.\fP
We say that region 
\fBx \fP
is 
\fIlater \fP
than region 
\fBy \fP
if the end position of 
\fBx \fP
is greater than the end position of 
\fBy, \fP
or if they end at the same position and the start of 
\fBx \fP
is greater than the start of 
\fBy. \fP
Region 
\fBx \fP
is 
\fIearlier \fP
than region 
\fBy \fP
if the start position of 
\fBx \fP
is smaller than the start position of 
\fBy, \fP
or if they start at the same position and the end position of 
\fBx \fP
is less than the end position of 
\fBy.\fP
Now a region 
\fBx \fP
from 
\fBv(region_expr) \fP
and a region 
\fBy \fP
from
\fBv(basic_expr) \fP
are paired in expression 
\fBv(region_expr '..' basic_expr) \fP
if and only if
.nr ll +1
.nr el +1
.nr t\n(ll 1
.nr e\n(el 0 1
.af e\n(el \*(f\n(el
.if \n(ll>1 .RS
.nr bi 1
.Pp
\fBx \fP
precedes 
\fBy,\fP
.nr bi 1
.Pp
\fBx \fP
is not paired with any region  from 
\fBv(basic_expr) \fP
which is earlier than 
\fBy,\fP
and 
.nr bi 1
.Pp
\fBy \fP
is not paired with any region from
\fBv(region_expr) \fP
which is later than 
\fBx. \fP
.if \n(ll>1 .RE
.nr el -1
.nr ll -1

The pairing of regions 
\fBx \fP
and 
\fBy \fP
forms a region that extends from the start position of 
\fBx \fP
to the end position of 
\fBy.\fP
.IP "\fBv(region_expr '._' basic_expr):\fP"
.nr bi 1
.Pp
The pairing of the regions from
\fBv(region_expr) \fP
and the regions from 
\fBv(basic_expr) \fP
is defined similarly to 
\fBv(region_expr '..' basic_expr) above,\fP
except that the pairing of regions 
\fBx \fP
and 
\fBy \fP
now forms a region which extends from the start position of 
\fBx \fP
to the position immediately preceding the start of 
\fBy.\fP
.IP "\fBv(region_expr '_.' basic_expr):=\fP"
.nr bi 1
.Pp
The pairing of the regions from 
\fBv(region_expr) \fP
and the regions from 
\fBv(basic_expr) \fP
is defined similarly to 
\fBv(region_expr '..' basic_expr) above, except that\fP
the pairing of regions 
\fBx \fP
and 
\fBy \fP
now forms a region which extends from the position 
immediately following the end position of 
\fBx \fP
to the end position of 
\fBy.\fP
.IP "\fBv(region_expr '__' basic_expr):=\fP"
.nr bi 1
.Pp
The pairing of the regions from 
\fBv(region_expr) \fP
and the regions from 
\fBv(basic_expr) \fP
is defined similarly to 
\fBv(region_expr '..' basic_expr)\fP above, except that now
the pairing of regions 
\fBx\fP and \fBy\fP
forms a region which extends from the text 
position immediately following the end of 
\fBx\fP
to the text position immediately preceding the start of 
\fBy\fP.
Possibly resulting empty regions are excluded from the result.
.Pp
.IP "\fBv(region_expr 'quote' basic_expr):\fP"
.nr bi 1
.Pp
The value of this expression consists of 
the regions that extend from the start position of a "\fIleft-quote region\fP" in 
\fBv(region_expr)\fP to the end position of a corresponding 
"\fIright-quote region\fP" in \fBv(basic_expr)\fP.
The regions in the result are non-nesting and non-overlapping.
The left-quote regions and the right-quote
regions are defined as follows:
.nr ll +1
.nr t\n(ll 0
.if \n(ll>1 .RS
.nr bi 1
.Pp
The earliest region (see above) in \fBv(region_expr)\fP is 
a \fIpossible left-quote region\fP.
.nr bi 1
.Pp
For each possible left-quote region \fBx\fP, the earliest region in
\fBv(basic_expr)\fP preceded by \fBx\fP is its right-quote region.
.nr bi 1
.Pp
For each  right-quote region \fBy\fP in \fBv(basic_expr)\fP, the
earliest region in \fBv(region_expr)\fP preceded by \fBy\fP is a
possible left-quote region.
.if \n(ll>1 .RE
.nr ll -1

The below example query finds C-style non-nesting comments:
.DS
.sp
.ft CR
.nf
        "/*" quote "*/"
.DE
.fi 
.ec
.ft P
.sp
.Pp
The below example query finds strings between quotation marks:
.DS
.sp
.ft CR
.nf
        "\\"" quote "\\""
.DE
.fi 
.ec
.ft P
.sp

(Notice the difference to expression \fB"\\"" .. "\\""\fP, 
which would evaluate to any substring of input text that starts with 
a quotation mark and ends with the next quotation mark.)
.Pp
The variants \fB_quote\fP, \fBquote_\fP and \fB_quote_\fP
are analogical to the operators
\fB_.\fP, \fB._\fP and \fB__\fP, in the sense that 
the "quote regions" originating from the expression on the side of the
underscore \fB_\fP are excluded from the result regions.
(In the case of \fB_quote_\fP any possibly resulting empty regions
are excluded from the result.)
.Pp
.IP "\fBv('concat' '(' region_expr ')' ):=\fP"
.nr bi 1
.Pp
the set of the longest regions of input text that are covered by
the regions in
\fBv(region_expr).\fP
.IP "\fBv('inner' '(' region_expr ')' ):=\fP"
.nr bi 1
.Pp
the set of regions in
\fBv(region_expr) \fP
that do not contain any other region in 
\fBv(region_expr).\fP
Note that for any region expression \fBA\fP, the expression 
\fBinner(A)\fP is equivalent to \fB(A not containing A)\fP.
.IP "\fBv('outer' '(' region_expr ')' ):=\fP"
.nr bi 1
.Pp
the set of regions in 
\fBv(region_expr) \fP
that are not contained in any other region in 
\fBv(region_expr).\fP
Note that for any region expression \fBA\fP, the expression
\fBouter(A)\fP is equivalent to \fB(A not in A)\fP.  
.IP "\fBv('join' '(' n ',' region_expr ')' ):\fP"
.nr bi 1
.Pp
The value of this expression is formed by 
processing the regions of v(region_expr) in increasing order of their
start positions (and in increasing order of end positions for
regions with a common start). Each region 
\fBr\fP
produces a result region beginning  at the start of r
and extending to the end of the (n-1)th region after r.
The operation is useful only with non-nesting regions. Especially,
when applied to 'chars', it can be used to express nearness
conditions. For example,
.Pp
.DS
.sp
.ft CR
.nf
    '"/*" quote "*/" in join(10,chars)' 
.DE
.fi 
.ec
.ft P
.sp
.Pp
selects comments  "/*  ... */" which are at most 10 characters long. 
.if \n(ll>1 .RE
.nr ll -1
.Pp
.SH EXAMPLES OF REGION EXPRESSIONS

.Pp
Count the number of occurrences of string "sort" in file eval.c:
.DS
.sp
.ft CR
.nf
    sgrep -c '"sort"' eval.c
.DE
.fi 
.ec
.ft P
.sp
.Pp
Show all blocks delimited by braces in file eval.c:
.DS
.sp
.ft CR
.nf
    sgrep '"{" .. "}"' eval.c
.DE
.fi 
.ec
.ft P
.sp
.Pp
Show the outermost blocks that contain "sort" or "nest":
.Pp
.DS
.sp
.ft CR
.nf
    sgrep 'outer("{" .. "}" containing ("sort" or "nest"))'\\
            eval.c
.DE
.fi 
.ec
.ft P
.sp
.Pp
Show all lines containing "sort" but no "nest" in files with
an extension .c, preceded by the name of the file:
.Pp
.DS
.sp
.ft CR
.nf
    sgrep -o "%f:%r" '"\\n" _. "\\n" containing "sort" \\
                      not containing "nest"' *.c
.DE
.fi 
.ec
.ft P
.sp
.Pp
(Notice that this query would omit the first line, since it has no
preceding new-line character '\\n',  and also the
last one, if not terminated by a new-line. For a correct way to
express text lines, see the definition of the LINE macro below.)
.Pp
Show the beginning of conditional statements,
consisting of "if" followed by a condition in parentheses,
in files *.c. The query has
to disregard "if"s appearing within comments "/* ... */" or on
compiler control lines beginning with '#':
.Pp
.DS
.sp
.ft CR
.nf
    sgrep '"if" not in ("/*" quote "*/" or ("\\n#" .. "\\n"))  \\
                        .. ("(" ..  ")")' *.c
.DE
.fi 
.ec
.ft P
.sp
.Pp
Show the if-statements containing string "access" in their condition
part appearing in the main
function of the program in source files *.c:
.Pp
.DS
.sp
.ft CR
.nf
    sgrep '"if" not in ("/*" quote "*/" or ("\\n#" .. "\\n"))  \\
             .. ("(" ..  ")") containing "access" \\
                              in ("main(" .. ("{" .. "}")) \\
            .. ("{" .. "}" or ";")'  *.c
.DE
.fi 
.ec
.ft P
.sp
.Pp
We see that complicated conditions can become rather
illegible. The use of carefully designed 
\fImacros \fP
can make expressing queries much easier. 
For example, one could give the 
below m4 macro processor definitions in a file, say, c.macros:
.Pp
.DS
.sp
.ft CR
.nf
    define(BLOCK,( "{" .. "}" ))
    define(COMMENT,( "/*" quote "*/" ))
    changecom(%)
    define(CTRLINE,( "#" in start or "\\n#" 
                      _. ("\\n" or end) ))
    define(IF_COND,( "if" not in (COMMENT or CTRLINE) 
                      .. ("(" .. ")")))
.DE
.fi 
.ec
.ft P
.sp
.Pp
Then the above query could be written more intuitively as
.Pp
.DS
.sp
.ft CR
.nf
    sgrep -p m4 -f c.macros -e 'IF_COND containing "access"\\
           in ( "main(" ..  BLOCK ) .. (BLOCK or  ";")' *.c
.DE
.fi 
.ec
.ft P
.sp
.Pp
.SH OPTIMIZATION

.Pp
\fBsgrep\fP
performs common subexpression elimination on the query
expression, so that recurring
sub-expressions are evaluated only once. For example, in
expression
.Pp
.DS
.sp
.ft CR
.nf
    '(" " or "\\n" or "\\t") .. (" " or "\\n" or "\\t")'
.DE
.fi 
.ec
.ft P
.sp
.Pp
the sub-expression
.Pp
.DS
.sp
.ft CR
.nf
    '(" " or "\\n" or "\\t")'
.DE
.fi 
.ec
.ft P
.sp
.Pp
is evaluated only one.
.SH DIAGNOSTICS

.Pp
Exit status is 0 if any matching regions are found, 1 if none, 2  for
syntax  errors  or  inaccessible files (even if matching regions were
found).
.Pp
.SH ENVIRONMENT

.Pp
One's own default options for sgrep can be given as a value of the
environment variable
\fISGREPOPT\fP.
For example, executing 
.DS
.sp
.ft CR
.nf
    setenv  SGREPOPT  '-p m4 -o %r\\n'
.DE
.fi 
.ec
.ft P
.sp

makes sgrep to apply m4 preprocessor to the expression and
display each output region as such followed by a line feed. 
.Pp
.SH FILES

.Pp
\fBSgrep\fP
tries to read the contents of the files 
\fI$HOME/.sgreprc\fP
and 
\fI/usr/lib/sgreprc\fP.
Generally useful macro definitions may be placed in  these files.
Using m4 (or some other) macro processor, for example the following definitions
could go in one of these files:
.Pp
.DS
.sp
.ft CR
.nf
    define(BLANK,( " " or "\\t" or "\\n"))
    define(LEND,( "\\n" or end ))
    define(LINE,( start .. LEND or ("\\n" _. LEND) ))
    define(NUMERAL,( "1" or "2" or "3" or "4" or "5" or
                     "6" or "7" or "8" or "9" or "0" ))
.DE
.fi 
.ec
.ft P
.sp
.Pp
.Pp
.SH FUTURE EXTENSIONS

.Pp
.nr ll +1
.nr t\n(ll 0
.if \n(ll>1 .RS
.nr bi 1
.Pp
Regular expressions (The most important missing feature)
.nr bi 1
.Pp
Built-in macro preprocessor
.nr bi 1
.Pp
More operations
.nr bi 1
.Pp
Indexing for large static texts     
.if \n(ll>1 .RE
.nr ll -1
.Pp
.Pp
.SH AUTHORS

.Pp
Jani Jaakkola and Pekka Kilpelainen, University of Helsinki,
Department of Computer Science, 1995.
.Pp
.SH BUGS

.Pp
\fBSgrep\fP
may use lots
of memory, when evaluating complex queries on big files. 
When sgrep reads its input text from a pipe, it 
copies it to a temporary file.
sgrep does not have regular expressions in search patters.
.Pp
.SH SEE ALSO

.Pp
\fBawk(1), ed(1),  grep(1)\fP
.Pp
sgrep home page at
\f(CRhttp://www.cs.helsinki.fi/~jjaakkol/sgrep.html\fP
.Pp
.Pp