Documentation for Greple and its modules.
This is the multi-page printable view of this section. Click here to print.
Documentation
- 1: Manuals
- 1.1: greple
- 1.2: greple@v8
- 1.3: greple -Mdebug
- 1.4: greple -Mcolors
- 1.5: greple -Mfind
- 1.6: greple -Mdig
- 1.7: greple -Mselect
- 1.8: greple -Mxp
- 1.9: greple -Mgit
- 1.10: greple -Mframe
- 1.11: greple -Mupdate
- 1.12: greple -Msubst
- 1.13: greple -Msubst-desumasu
- 1.14: greple -Mtype
- 1.15: greple -Mmsdoc
- 1.16: greple -Mwordle
- 1.17: greple -Maozora
- 1.18: greple -Mcm
- 1.19: greple -Mdaemon3
- 1.20: greple -Mical
- 1.21: greple -Mjq
- 1.22: greple -Mmecab
- 1.23: greple -Mppi
- 1.24: greple -Mpw
- 1.25: greple -Msccc2
- 2: Tutorial
- 2.1: はじめに
- 2.2: 単純な検索
- 2.3: 複数キーワードによる検索
- 2.4: 複数キーワード検索 OR
- 2.5: 複数キーワード検索 NOT
- 2.6: 複数キーワード検索 MUST
- 2.7: 複数キーワード検索 単独オプション
- 2.8: 検索範囲指定 --inside/--outside
- 2.9: 検索範囲指定 --include/--exclude
- 2.10: 検索ブロック指定
- 2.11: 拡張モジュールの使用法
- 2.12: 初期設定ファイル .greplerc
- 2.13: カラーオプション
- 2.14: カラーオプション(続き)
- 2.15: カラーマップの切り替え
- 2.16: 入出力フィルター
- 2.17: 関数インタフェース
- 2.18: 関数オプション
- 2.19: モジュールの作り方 (1)
- 2.20: モジュールの作り方 (2)
- 3: Articles
1 - Manuals
Greple man pages.
1.1 - greple
NAME
greple - extensible grep with lexical expression and region control
VERSION
Version 9.00_02
SYNOPSIS
greple [-Mmodule] [ -options ] pattern [ file… ]
PATTERN
pattern 'and +must -not ?optional &function'
-x, --le pattern lexical expression (same as bare pattern)
-e, --and pattern pattern match across line boundary
-r, --must pattern pattern cannot be compromised
-t, --may pattern pattern may be exist
-v, --not pattern pattern not to be matched
--re pattern regular expression
--fe pattern fixed expression
-f, --file file file contains search pattern
--select index select indexed pattern from -f file
MATCH
-i ignore case
--need=[+-]n required positive match count
--allow=[+-]n acceptable negative match count
--matchcount=n[,m] required match count for each block
STYLE
-l list filename only
-c print count of matched block only
-n print line number
-H, -h do or do not display filenames
-o print only the matching part
--all print entire data
-m, --max=n[,m] max count of blocks to be shown
-A,-B,-C [n] after/before/both match context
--join delete newline in the matched part
--joinby=string replace newline in the matched text by string
--nonewline do not add newline character at block end
--filestyle=style how filename printed (once, separate, line)
--linestyle=style how line number printed (separate, line)
--separate set filestyle and linestyle both "separate"
--format LABEL=... define line number and file name format
--frame-top top frame
--frame-middle middle frame
--frame-bottom bottom frame
FILE
--glob=glob glob target files
--chdir=dir change directory before search
--readlist get filenames from stdin
COLOR
--color=when use terminal color (auto, always, never)
--nocolor same as --color=never
--colormap=color R, G, B, C, M, Y etc.
--colorsub=... shortcut for --colormap="sub{...}"
--colorful use default multiple colors
--colorindex=flags color index method: Ascend/Descend/Block/Random
--ansicolor=s ANSI color 16, 256 or 24bit
--[no]256 same as --ansicolor 256 or 16
--regioncolor use different color for inside/outside regions
--uniqcolor use different color for unique string
--uniqsub=func preprocess function before check uniqueness
--random use random color each time
--face set/unset visual effects
BLOCK
-p, --paragraph paragraph mode
--border=pattern border pattern
--block=pattern block of records
--blockend=s block end mark (Default: "--")
--join-blocks join back-to-back consecutive blocks
REGION
--inside=pattern select matches inside of pattern
--outside=pattern select matches outside of pattern
--include=pattern reduce matches to the area
--exclude=pattern reduce matches to outside of the area
--strict strict mode for --inside/outside --block
CHARACTER CODE
--icode=name file encoding
--ocode=name output encoding
FILTER
--if,--of=filter input/output filter command
--pf=filter post process filter command
--noif disable default input filter
RUNTIME FUNCTION
--print=func print function
--continue continue after print function
--callback=func callback function for matched string
--begin=func call function before search
--end=func call function after search
--prologue=func call function before command execution
--epilogue=func call function after command execution
OTHER
--usage[=expand] show this message
--exit=n command exit status
--norc skip reading startup file
--man display command or module manual page
--show display module file
--path show module file path
--persist same as --error=retry
--error=action action after read error
--warn=type run time error control
--alert [name=#] set alert parameter (size/time)
-d flags display info (f:file d:dir c:color m:misc s:stat)
INSTALL
CPANMINUS
$ cpanm App::Greple
DESCRIPTION
MULTIPLE KEYWORDS
AND
greple can take multiple search patterns by -e option, but
unlike egrep(1) command, they are searched in AND context. For
example, next command print lines those contain all of foo
and
bar
and baz
.
greple -e foo -e bar -e baz ...
Each word can appear in any order and any place in the string. So this command find all of following lines.
foo bar baz
baz bar foo
the foo, bar and baz
If you want to use OR syntax, use regular expression.
greple -e foo -e bar -e baz -e 'yabba|dabba|doo'
This command will print lines those contains all of foo
, bar
and
baz
and one or more of yabba
, dabba
or doo
.
NOT
Use option -v to specify keyword which should not found in the data
record. Next example will show lines those contain both foo
and
bar
but none of yabba
, dabba
or doo
.
greple -e foo -e bar -v yabba -v dabba -v doo
greple -e foo -e bar -v 'yabba|dabba|doo'
MAY
When you are focusing on multiple words, there may be words those are not necessary but would be of interest if there were.
Use option –may or -t (tentative) to specify that kind of words. They will be a subject of search, and highlighted if exist, but are optional.
Next command print all lines including foo
and bar
, and
highlight baz
as well.
greple -e foo -e bar -t baz
MUST
Option –must or -r is another way to specify optional keyword. If required keyword exists, all other positive match keyword becomes optional. Next command is equivalent to the above example.
greple -r foo -r bar -e baz
LEXICAL EXPRESSION
greple takes the first argument as a search pattern specified by –le option. In –le pattern, you can set multiple keywords in a single parameter. Each keyword is separated by spaces, and the first character describe the type.
none And pattern : --and -e
+ Required pattern : --must -r
- Negative match pattern : --not -v
? Optional pattern : --may -t
Just like internet search engine, you can simply provide foo bar baz
to search lines including all words.
greple 'foo bar baz'
Next command show lines which include foo
, but does not include
bar
, and highlight baz
if exists.
greple 'foo -bar ?baz'
FLEXIBLE BLOCKS
Default data block greple search and print is a line. Using
–paragraph (or -p in short) option, series of text separated by
empty line is taken as a record block. So next command will print
whole paragraph which contains the word foo
, bar
and baz
.
greple -p 'foo bar baz'
Block also can be defined by pattern. Next command treat the data as a series of 10-line unit.
greple -n --border='(.*\n){1,10}'
You can also define arbitrary complex blocks by writing script.
greple --block '&your_original_function' ...
MATCH AREA CONTROL
Using option –inside and –outside, you can specify the text area to be matched. Next commands search only in mail header and body area respectively. In these cases, data block is not changed, so print lines which contains the pattern in the specified area.
greple --inside '\A(.+\n)+' pattern
greple --outside '\A(.+\n)+' pattern
Option –inside/–outside can be used repeatedly to enhance the area to be matched. There are similar option –include/–exclude, but they are used to trim down the area.
These four options also take user defined function and any complex region can be used.
LINE ACROSS MATCH
greple search a given pattern across line boundaries. This is especially useful to handle Asian multi-byte text, more specifically Japanese. Japanese text can be separated by newline almost any place in the text. So the search pattern may spread out onto multiple lines.
As for ascii word list, space character in the pattern matches any
kind of space including newline. Next example will search the word
sequence of foo
, bar
and baz
, even they spread out to
multiple lines.
greple -e 'foo bar baz'
Option -e is necessary because space is taken as a token separator in the bare or –le pattern.
MODULE AND CUSTOMIZATION
User can define default and original options in ~/.greplerc
. Next
example enables colored output always, and define new option using
macro processing.
option default --color=always
define :re1 complex-regex-1
define :re2 complex-regex-2
define :re3 complex-regex-3
option --newopt --inside :re1 --exclude :re2 --re :re3
Specific set of function and option interface can be implemented as module. Modules are invoked by -M option immediately after command name.
For example, greple does not have recursive search option, but it can be implemented by –readlist option which accept target file list from standard input. Using find module, it can be written like this:
greple -Mfind . -type f -- pattern
Also dig module implements more complex search. It can be used as simple as this:
greple -Mdig pattern --dig .
but this command is finally translated into following option list.
greple -Mfind . ( -name .git -o -name .svn -o -name RCS ) -prune -o
-type f ! -name .* ! -name *,v ! -name *~
! -iname *.jpg ! -iname *.jpeg ! -iname *.gif ! -iname *.png
! -iname *.tar ! -iname *.tbz ! -iname *.tgz ! -iname *.pdf
-print -- pattern
INCLUDED MODUES
This release include some sample modules. Read document in each modules for detail. You can read the document by –man option or perldoc command.
greple -Mdig --man
perldoc App::Greple::dig
When it does not work, use perldoc App::Greple::dig
.
colors
Color variation module. See App::Greple::colors.
find
Module to use find(1) command to help recursive search. See App::Greple::find.
dig
Module for recursive search using find module. Defines –dig, –git and –git-r options. See App::Greple::dig.
pgp
Module to search pgp files. See App::Greple::pgp.
select
Module to select files. See App::Greple::select.
perl
Sample module to search from perl source files. See App::Greple::perl.
Other modules are available at CPAN, or git repository https://github.com/kaz-utashiro/.
OPTIONS
PATTERNS
If no specific option is given, greple takes the first argument as a search pattern specified by –le option. All of these patterns can be specified multiple times.
Command itself is written in Perl, and any kind of Perl style regular expression can be used in patterns. See perlre(1) for detail.
Note that multiple line modifier (m
) is set when executed, so put
(?-m)
at the beginning of regex if you want to explicitly disable
it.
Order of capture group in the pattern is not guaranteed. Please avoid
to use direct index, and use relative or named capture group instead.
For example, if you want to search repeated characters, use
(\w)\g{-1}
or (?<c>\w)\g{c}
rather than
(\w)\1
.
-e pattern, –and=pattern
Specify the positive match pattern. Next command print lines contains all of
foo
,bar
andbaz
.greple -e foo -e bar -e baz
-t pattern, –may=pattern
Specify the optional (tentative) match pattern. Next command print lines contains
foo
andbar
, and highlightbaz
if exists.greple -e foo -e bar -t baz
-r pattern, –must=pattern
Specify the required match pattern. If one or more required pattern exist, other positive match pattern becomes optional.
greple -r foo -r bar -e baz
Because -t promote all other -e patterns required, next command do the same thing. Mixing -r, -e and -t is not recommended, though.
greple -r foo -e bar -t baz
-v pattern, –not=pattern
Specify the negative match pattern. Because it does not affect to the bare pattern argument, you can narrow down the search result like this.
greple foo file greple foo file -v bar greple foo file -v bar -v baz
In the above pattern options, space characters are treated specially.
They are replaced by the pattern which matches any number of white
spaces including newline. So the pattern can expand to multiple
lines. Next commands search the series of word foo
bar
baz
even if they are separated by newlines.
greple -e 'foo bar baz'
This is done by converting pattern foo bar baz
to
foo\s+bar\+baz
, so that word separator can match one or more white
spaces.
As for Asian wide characters, pattern is cooked as zero or more white
spaces can be allowed between any characters. So Japanese string
pattern 日本語
will be converted to 日\s*本\s*語
.
If you don’t want these conversion, use –re option.
-x pattern, –le=pattern
Treat the pattern string as a collection of tokens separated by spaces. Each token is interpreted by the first character. Token start with
-
means negative pattern,?
means optional, and+
does required.Next example print lines which contain
foo
andyabba
, and none ofbar
anddabba
, with highlightingbaz
anddoo
if they exist.greple --le='foo -bar ?baz yabba -dabba ?doo'
This is the summary of start character for –le option:
+ Required pattern - Negative match pattern ? Optional pattern & Function call (see next section)
-x [+?-]&function, –le=[+?-]&function
If the pattern start with ampersand (
&
), it is treated as a function, and the function is called instead of searching pattern. Function call interface is same as the one for block/region options.If you have a definition of odd_line function in you
.greplerc
, which is described in this manual later, you can print odd number lines like this:greple -n '&odd_line' file
Required (
+
), optional (?
) and negative (-
) mark can be used for function pattern.CALLBACK FUNCTION: Region list returned by function can have two extra elements besides start/end position. Third element is index. Fourth element is a callback function pointer which will be called to produce string to be shown in command output. Callback function is called with four arguments (start position, end position, index, matched string) and expected to return replacement string.
–re=pattern
Specify regular expression. No special treatment for space and wide characters.
–fe=pattern
Specify the fixed string pattern, like fgrep.
-i, –ignore-case
Ignore case.
–need=n
–allow=n
Option to compromise matching condition. Option –need specifies the required match count, and –allow the number of negative condition to be overlooked.
greple --need=2 --allow=1 'foo bar baz -yabba -dabba -doo'
Above command prints the line which contains two or more from
foo
,bar
andbaz
, and does not include more than one ofyabba
,dabba
ordoo
.Using option –need=1, greple produces same result as grep command.
grep -e foo -e bar -e baz greple -e foo -e bar -e baz --need=1
When the count n is negative value, it is subtracted from default value.
If the option –need=0 is specified and no pattern was found, entire data is printed. This is true even for required pattern.
–matchcount=count –mc=…
–matchcount=min,max –mc=…
When option –matchcount is specified, only blocks which have given match count will be shown. Minimum and maximum number can be given, connecting by comma, and they can be omitted. Next commands print lines including semicolons; 3 or more, exactly 3, and 3 or less, respectively.
greple --matchcount=3, ';' file greple --matchcount=3 ';' file greple --matchcount=,3 ';' file
In fact, min and max can repeat to represent multiple range. Missing, negative or zero max means infinite. Next command find match count 0 to 10, 20 to 30, and 40-or-greater.
greple --matchcount=,10,20,30,40
-f file, –file=file
Specify the file which contains search pattern. When file contains multiple lines, patterns are mixed together by OR context.
Blank line and the line starting with sharp (#) character is ignored. Two slashes (//) and following string are taken as a comment and removed with preceding spaces.
When multiple files specified, each file produces individual pattern.
If the file name is followed by
[index]
string, it is treated as specified by –select option. Next two commands are equivalent.greple -f pattern_file'[1,5:7]' greple -f pattern_file --select 1,5:7
See App::Greple::subst module.
–select=index
When you want to choose specific pattern in the pattern file provided by -f option, use –select option. index is number list separated by comma (,) character and each number is interpreted by Getopt::EX::Numbers module. Take a look at the module document for detail.
Next command use 1st and 5,6,7th pattern in the file.
greple -f pattern_file --select 1,5:7
STYLES
-l
List filename only.
-c, –count
Print count of matched block.
-n, –line-number
Show line number.
-h, –no-filename
Do not display filename.
-H
Display filename always.
-o, –only-matching
Print matched string only. Newline character is printed after matched string if it does not end with newline. Use –no-newline option if you don’t need extra newline.
–all
Print entire file. This option does not affect to seach behavior or block treatment. Just print all contents.
-m n[,m], –max-count=n[,m]
Set the maximum count of blocks to be shown to n.
Actually n and m are simply passed to perl splice function as offset and length. Works like this:
greple -m 10 # get first 10 blocks greple -m 0,-10 # get last 10 blocks greple -m 0,10 # remove first 10 blocks greple -m -10 # remove last 10 blocks greple -m 10,10 # remove 10 blocks from 10th (10-19)
This option does not affect to search performance and command exit status.
Note that grep command also has same option, but it’s behavior is different when invoked to multiple files. greple produces given number of output for each file, while grep takes it as a total number of output.
-m *, –max-count=*
In fact, n and m can repeat as many as possible. Next example removes first 10 blocks (by
0,10
), then get first 10 blocks from the result (by10
). Consequently, get 10 blocks from 10th (10-19).greple -m 0,10,10
Next command get first 20 (by
20,
) and get last 10 (by,-10
), producing same result. Empty string behaves like absence for length and zero for offset.greple -m 20,,,-10
-A[n], –after-context[=n]
-B[n], –before-context[=n]
-C[n], –context[=n]
Print n-blocks before/after matched string. The value n can be omitted and the default is 2. When used with –paragraph or –block option, n means number of paragraph or block.
Actually, these options expand the area of logical operation. It means
greple -C1 'foo bar baz'
matches following text.
foo bar baz
Moreover
greple -C1 'foo baz'
also matches this text, because matching blocks around
foo
andbar
overlaps each other and makes single block.–join
–joinby=string
Convert newline character found in matched string to empty or specified string. Using –join with -o (only-matching) option, you can collect searching sentence list in one per line form. This is sometimes useful for Japanese text processing. For example, next command prints the list of KATAKANA words, including those spread across multiple lines.
greple -ho --join '\p{InKatakana}+(\n\p{InKatakana}+)*'
Space separated word sequence can be processed with –joinby option. Next example prints all
for *something*
pattern in pod documents within Perl script.greple -Mperl --pod -ioe '\bfor \w+' --joinby ' '
–[no]newline
Since greple can handle arbitrary blocks other than normal text lines, they sometimes do not end with newline character. Option -o makes similar situation. In that case, extra newline is appended at the end of block to be shown. Option –no-newline disables this behavior.
–filestyle=[
line
,once
,separate
], –fsDefault style is line, and greple prints filename at the beginning of each line. Style once prints the filename only once at the first time. Style separate prints filename in the separate line before each line or block.
–linestyle=[
line
,separate
], –lsDefault style is line, and greple prints line numbers at the beginning of each line. Style separate prints line number in the separate line before each line or block.
–separate
Shortcut for –filestyle=separate –linestyle=separate. This is convenient to use block mode search and visiting each location from supporting tool, such as Emacs.
–format LABEL=format
Define the format string of line number (LINE) and file name (FILE) to be displayed. Default is:
--format LINE='%d:' --format FILE='%s:'
Format string is passed to
sprintf
function. Tab character can be expressed as\t
.Next example will show line numbers in five digits with tab space:
--format LINE='%05d\t'
–frame-top=string
–frame-middle=string
–frame-bottom=string
Print surrounding frames before and after each block.
top
frame is printed at the beginning,bottom
frame at the end,middle
frame between blocks.
FILES
–glob=pattern
Get files matches to specified pattern and use them as a target files. Using –chdir and –glob makes easy to use greple for fixed common job.
–chdir=directory
Change directory before processing files. When multiple directories are specified in –chdir option, by using wildcard form or repeating option, –glob file expansion will be done for every directories.
greple --chdir '/usr/man/man?' --glob '*.[0-9]' ...
–readlist
Get filenames from standard input. Read standard input and use each line as a filename for searching. You can feed the output from other command like find(1) for greple with this option. Next example searches string from files modified within 7 days:
find . -mtime -7 -print | greple --readlist pattern
Using find module, this can be done like:
greple -Mfind . -mtime -7 -- pattern
COLORS
–color=[
auto
,always
,never
], –nocolorUse terminal color capability to emphasize the matched text. Default is
auto
: effective when STDOUT is a terminal and option -o is not given, not otherwise. Option valuealways
andnever
will work as expected.Option –nocolor is alias for –color=never.
When color output is disabled, ANSI terminal sequence is not produced, but functional colormap, such as
--cm sub{...}
, still works.–colormap=spec, –cm=…
Specify color map. Because this option is mostly implemented by Getopt::EX::Colormap module, consult its document for detail and up-to-date specification.
Color specification is combination of single uppercase character representing basic colors, and (usually brighter) alternative colors in lowercase:
R r Red G g Green B b Blue C c Cyan M m Magenta Y y Yellow K k Black W w White
or RGB value and 24 grey levels if using ANSI 256 color terminal:
(255,255,255) : 24bit decimal RGB colors #000000 .. #FFFFFF : 24bit hex RGB colors #000 .. #FFF : 12bit hex RGB 4096 colors 000 .. 555 : 6x6x6 RGB 216 colors L00 .. L25 : Black (L00), 24 grey levels, White (L25)
Beginning # can be omitted in 24bit RGB notation. When values are all same in 24bit or 12bit RGB, it is converted to 24 grey level, otherwise 6x6x6 216 color.
or color names enclosed by angle bracket:
<red> <blue> <green> <cyan> <magenta> <yellow> <aliceblue> <honeydue> <hotpink> <mooccasin> <medium_aqua_marine>
with other special effects:
N None Z 0 Zero (reset) D 1 Double strike (boldface) P 2 Pale (dark) I 3 Italic U 4 Underline F 5 Flash (blink: slow) Q 6 Quick (blink: rapid) S 7 Stand out (reverse video) H 8 Hide (concealed) X 9 Cross out E Erase Line ; No effect / Toggle foreground/background ^ Reset to foreground
If the spec includes
/
, left side is considered as foreground color and right side as background. If multiple colors are given in same spec, all indicators are produced in the order of their presence. As a result, the last one takes effect.Effect characters are case insensitive, and can be found anywhere and in any order in color spec string. Character
;
does nothing and can be used just for readability, likeSD;K/544
.Example:
RGB 6x6x6 12bit 24bit color name === ======= ========= ============= ================== B 005 #00F (0,0,255) <blue> /M /505 /#F0F /(255,0,255) /<magenta> K/W 000/555 #000/#FFF 000000/FFFFFF <black>/<white> R/G 500/050 #F00/#0F0 FF0000/00FF00 <red>/<green> W/w L03/L20 #333/#ccc 303030/c6c6c6 <dimgrey>/<lightgrey>
Multiple colors can be specified separating by white space or comma, or by repeating options. Those colors will be applied for each pattern keywords. Next command will show word
foo
in red,bar
in green andbaz
in blue.greple --colormap='R G B' 'foo bar baz' greple --cm R -e foo --cm G -e bar --cm B -e baz
Coloring capability is implemented in Getopt::EX::Colormap module.
–colormap=field=spec,…
Another form of colormap option to specify the color for fields:
FILE File name LINE Line number TEXT Unmatched normal text BLOCKEND Block end mark PROGRESS Progress status with -dnf option
In current release,
BLOCKEND
mark is colored withE
effect recently implemented in Getopt::EX module, which allows to fill up the line with background color. This effect uses irregular escape sequence, and you may need to defineLESSANSIENDCHARS
environment as “mK” to see the result with less command.–colormap=
&func
–colormap=
sub{...}
You can also set the name of perl subroutine name or definition to be called handling matched words. Target word is passed as variable
$_
, and the return value of the subroutine will be displayed.Next command convert all words in C comment to upper case.
greple --all '/\*(?s:.*?)\*/' --cm 'sub{uc}'
You can quote matched string instead of coloring (this emulates deprecated option –quote):
greple --cm 'sub{"<".$_.">"}' ...
It is possible to use this definition with field names. Next example print line numbers in seven digits.
greple -n --cm 'LINE=sub{s/(\d+)/sprintf("%07d",$1)/e;$_}'
Experimentally, function can be combined with other normal color specifications. Also the form
&func;
can be repeated.greple --cm 'BF/544;sub{uc}' greple --cm 'R;&func1;&func2;&func3'
When color for ‘TEXT’ field is specified, whole text including matched part is passed to the function, exceptionally. It is not recommended to use user defined function for ‘TEXT’ field.
–colorsub=
...
, –cs=…–colorsub or –cs is a shortcut for subroutine colormap. It simply enclose the argument by
sub{ ... }
expression. Sogreple -cm 'sub{uc}'
can be written as simple as this.
greple -cs uc
You can not use this option for labeled color.
–[no]colorful
Shortcut for –colormap=’
RD GD BD CD MD YD
’ in ANSI 16 colors mode, and –colormap=’D/544 D/454 D/445 D/455 D/454 D/554
’ and other combination of 3, 4, 5 for 256 colors mode. Enabled by default.When single pattern is specified, first color in colormap is used for the pattern. If multiple patterns and multiple colors are specified, each pattern is colored with corresponding color cyclically.
Option –regioncolor, –uniqcolor and –colorindex change this behavior.
–colorindex=spec, –ci=spec
Specify color index method by combination of spec characters. A (ascend) and D (descend) can be mixed with B (block) and/or S (shuffle) like –ci=ABS. R (random) can be too but it does not make sense. When S is used alone, colormap is shuffled with normal behavior.
A (Ascending)
Apply different color sequentially according to the order of appearance.
D (Descending)
Apply different color sequentially according to the reverse order of appearance.
B (Block)
Reset sequential index on every block.
S (Shuffle)
Shuffle indexed color.
R (Random)
Use random color index every time.
N (Normal)
Reset to normal behavior. Because the last option takes effect, –ci=N can be used to reset the behavior set by previous options.
–random
Shortcut for –colorindex=R.
–ansicolor=[
16
,256
,24bit
]If set as
16
, use ANSI 16 colors as a default color set, otherwise ANSI 256 colors. When set as24bit
, 6 hex digits notation produces 24bit color sequence. Default is256
.–[no]256
Shortcut for –ansicolor=
256
or16
.–[no]regioncolor, –[no]rc
Use different colors for each –inside/outside region.
Disabled by default, but automatically enabled when only single search pattern is specified. Use –no-regioncolor to cancel automatic action.
–[no]uniqcolor, –[no]uc
Use different colors for different string matched. Disabled by default.
Next example prints all words start by
color
and display them all in different colors.greple --uniqcolor 'colou?r\w*'
When used with option -i, color is selected still in case-sensitive fashion. If you want case-insensitive color selection, use next –uniqsub option.
–uniqsub=function, –us=function
Above option –uniqcolor set same color for same literal string. Option –uniqsub specify the preprocessor code applied before comparison. function get matched string by
$_
and returns the result. For example, next command will choose unique colors for each word by their length.greple --uniqcolor --uniqsub 'sub{length}' '\w+' file
If you want case-insensitive color selection, do like this.
greple -i pattern --uc --uniqsub 'sub{lc}'
Next command read the output from
git blame
command and set unique color for each entire line by their commit ids.git blame ... | greple .+ --uc --us='sub{s/\s.*//r}' --face=E-D
–face=[-+]effect
Set or unset specified effect for all indexed color specs. Use
+
(optional) to set, and-
to unset. Effect is a single character expressing S (Stand-out), U (Underline), D (Double-struck), F (Flash) and such.Next example remove D (double-struck) effect.
greple --face -D
Multiple effects can be set/unset at once.
greple --face SF-D
BLOCKS
-p, –paragraph
Print a paragraph which contains the pattern. Each paragraph is delimited by two or more successive newlines by default. Be aware that an empty line is not a paragraph delimiter if which contains space characters. Example:
greple -np 'setuid script' /usr/man/catl/perl.l greple -pe '^struct sockaddr' /usr/include/sys/socket.h
It changes the unit of context specified by -A, -B, -C options. Space gap between paragraphs are also treated as block unit. Thus, option -pC2 will print with previous and next paragraph, while -pC1 will print with just surrounding spaces.
You can create original paragraph pattern by –border option.
–border=pattern
Specify record block border pattern. Pattern match is done in the context of multiple line mode.
Default block is a single line and use
/^/m
as a pattern. Paragraph mode uses/(?:\A|\R)\K\R+/
, which means continuous newlines at the beginning of text or following another newline (\R
means more general linebreaks including\r\n
; consult perlrebackslash for detail).Next command treat the data as a series of 10-line unit.
greple -n --border='(.*\n){1,10}'
Contrary to the next –block option, –border never produce disjoint records.
If you want to treat entire file as a single block, setting border to start or end of whole data is efficient way. Next commands works same.
greple --border '\A' # beginning of file greple --border '\z' # end of file
–block=pattern
–block=&sub
Specify the record block to display. Default block is a single line.
Empty blocks are ignored. When blocks are not continuous, the match occurred outside blocks are ignored.
If multiple block options are given, overlapping blocks are merged into a single block.
Please be aware that this option is sometimes quite time consuming, because it finds all blocks before processing.
–blockend=string
Change the end mark displayed after -pABC or –block options. Default value is “–”.
–join-blocks
Join consecutive blocks together. Logical operation is done for each individual blocks, but if the results are back-to-back connected, make them single block for final output.
REGIONS
–inside=pattern
–outside=pattern
Option –inside and –outside limit the text area to be matched. For simple example, if you want to find string
and
not in the wordcommand
, it can be done like this.greple --outside=command and
The block can be larger and expand to multiple lines. Next command searches from C source, excluding comment part.
greple --outside '(?s)/\*.*?\*/'
Next command searches only from POD part of the perl script.
greple --inside='(?s)^=.*?(^=cut|\Z)'
When multiple inside and outside regions are specified, those regions are mixed up in union way.
In multiple color environment, and if single keyword is specified, matches in each –inside/outside region is printed in different color. Forcing this operation with multiple keywords, use –regioncolor option.
–inside=&function
–outside=&function
If the pattern name begins by ampersand (&) character, it is treated as a name of subroutine which returns a list of blocks. Using this option, user can use arbitrary function to determine from what part of the text they want to search. User defined function can be defined in
.greplerc
file or by module option.–include=pattern
–exclude=pattern
–include=&function
–exclude=&function
–include/exclude option behave exactly same as –inside/outside when used alone.
When used in combination, –include/exclude are mixed in AND manner, while –inside/outside are in OR.
Thus, in the next example, first line prints all matches, and second does none.
greple --inside PATTERN --outside PATTERN greple --include PATTERN --exclude PATTERN
You can make up desired matches using –inside/outside option, then remove unnecessary part by –include/exclude
–strict
Limit the match area strictly.
By default, –block, –inside/outside, –include/exclude option allows partial match within the specified area. For instance,
greple --inside and command
matches pattern
command
because the part of matched string is included in specified inside-area. Partial match fails when option –strict provided, and longer string never matches within shorter area.Interestingly enough, above example
greple --include PATTERN --exclude PATTERN
produces output, as a matter of fact. Think of the situation searching, say,
' PATTERN '
with this condition. Matched area includes surrounding spaces, and satisfies both conditions partially. This match does not occur when option –strict is given, either.
CHARACTER CODE
–icode=code
Target file is assumed to be encoded in utf8 by default. Use this option to set specific encoding. When handling Japanese text, you may choose from 7bit-jis (jis), euc-jp or shiftjis (sjis). Multiple code can be supplied using multiple option or combined code names with space or comma, then file encoding is guessed from those code sets. Use encoding name
guess
for automatic recognition from default code list which is euc-jp and 7bit-jis. Following commands are all equivalent.greple --icode=guess ... greple --icode=euc-jp,7bit-jis ... greple --icode=euc-jp --icode=7bit-jis ...
Default code set are always included suspect code list. If you have just one code adding to suspect list, put + mark before the code name. Next example does automatic code detection from euc-kr, ascii, utf8 and UTF-16/32.
greple --icode=+euc-kr ...
If the string “binary” is given as encoding name, no character encoding is expected and all files are processed as binary data.
–ocode=code
Specify output code. Default is utf8.
FILTER
–if=filter, –if=EXP:filter
You can specify filter command which is applied to each file before search. If only one filter command is specified, it is applied to all files. If filter information include colon, first field will be perl expression to check the filename saved in variable $_. If it successes, next filter command is pushed.
greple --if=rev perg greple --if='/\.tar$/:tar tvf -'
If the command doesn’t accept standard input as processing data, you may be able to use special device:
greple --if='nm /dev/stdin' crypt /usr/lib/lib*
Filters for compressed and gzipped file is set by default unless –noif option is given. Default action is like this:
greple --if='s/\.Z$//:zcat' --if='s/\.g?z$//:gunzip -c'
File with
.gpg
suffix is filtered by gpg command. In that case, pass-phrase is asked for each file. If you want to input pass-phrase only once to find from multiple files, use -Mpgp module.If the filter start with
&
, perl subroutine is called instead of external command. You can define the subroutine in.greplerc
or modules. Greple simply call the subroutine, so it should be responsible for process control.–noif
Disable default input filter. Which means compressed files will not be decompressed automatically.
–of=filter
–of=&func
Specify output filter which process the output of greple command. Filter command can be specified in multiple times, and they are invoked for each file to be processed. So next command reset the line number for each file.
greple --of 'cat -n' string file1 file2 ...
If the filter start with
&
, perl subroutine is called instead of external command. You can define the subroutine in.greplerc
or modules.Output filter command is executed only when matched string exists to avoid invoking many unnecessary processes. No effect for option -l and -c.
–pf=filter
–pf=&func
Similar to –of filter but invoked just once and takes care of entire output from greple command.
RUNTIME FUNCTIONS
–print=function
–print=sub{…}
Specify user defined function executed before data print. Text to be printed is replaced by the result of the function. Arbitrary function can be defined in
.greplerc
file or module. Matched data is placed in variable$_
. Filename is passed by&FILELABEL
key, as described later.It is possible to use multiple –print options. In that case, second function will get the result of the first function. The command will print the final result of the last function.
–continue
When –print option is given, greple will immediately print the result returned from print function and finish the cycle. Option –continue forces to continue normal printing process after print function called. So please be sure that all data being consistent.
–callback=function(…)
Callback function is called before printing every matched pattern with four labeled parameters: start, end, index and match, which corresponds to start and end position in the text, pattern index, and the matched string. Matched string in the text is replaced by returned string from the function.
–begin=function(…)
–begin=function=…
Option –begin specify the function executed at the beginning of each file processing. This function have to be called from main package. So if you define the function in the module package, use the full package name or export properly.
If the function dies with a message starting with a word “SKIP” (
/^SKIP/i
), that file is simply skipped. So you can control if the file is to be processed using the file name or content. To see the message, use –warn begin=1 option.For example, using next function, only perl related files will be processed.
sub is_perl { my %arg = @_; my $name = delete $arg{&FILELABEL} or die; $name =~ /\.(?:pm|pl|PL|pod)$/ or /\A#!.*\bperl/ or die "skip $name\n"; } 1; __DATA__ option default --filestyle=once --format FILE='\n%s:\n' autoload -Mdig --dig option --perl $<move> --begin &__PACKAGE__::is_perl --dig .
–end=function(…)
–end=function=…
Option –end is almost same as –begin, except that the function is called after the file processing.
–prologue=function(…)
–prologue=function=…
–epilogue=function(…)
–epilogue=function=…
Option –prologue and –epilogue specify functions called before and after processing. During the execution, file is not opened and therefore, file name is not given to those functions.
-Mmodule::function(…)
-Mmodule::function=…
Function can be given with module option, following module name. In this form, the function will be called with module package name. So you don’t have to export it. Because it is called only once at the beginning of command execution, before starting file processing,
FILELABEL
parameter is not given exceptionally.
For these run-time functions, optional argument list can be set in the
form of key
or key=value
, connected by comma. These arguments
will be passed to the function in key => value list. Sole key will
have the value one. Also processing file name is passed with the key
of FILELABEL
constant. As a result, the option in the next form:
--begin function(key1,key2=val2)
--begin function=key1,key2=val2
will be transformed into following function call:
function(&FILELABEL => "filename", key1 => 1, key2 => "val2")
As described earlier, FILELABEL
parameter is not given to the
function specified with module option. So
-Mmodule::function(key1,key2=val2)
-Mmodule::function=key1,key2=val2
simply becomes:
function(key1 => 1, key2 => "val2")
The function can be defined in .greplerc
or modules. Assign the
arguments into hash, then you can access argument list as member of
the hash. It’s safe to delete FILELABEL key if you expect random
parameter is given. Content of the target file can be accessed by
$_
. Ampersand (&
) is required to avoid the hash key is
interpreted as a bare word.
sub function {
my %arg = @_;
my $filename = delete $arg{&FILELABEL};
$arg{key1}; # 1
$arg{key2}; # "val2"
$_; # contents
}
OTHERS
–usage[=expand]
Greple print usage and exit with option –usage, or no valid parameter is not specified. In this case, module option is displayed with help information if available. If you want to see how they are expanded, supply something not empty to –usage option, like:
greple -Mmodule --usage=expand
–exit=number
When greple executed normally, it exit with status 0 or 1 depending on something matched or not. Sometimes we want to get status 0 even if nothing matched. This option set the status code for normal execution. It still exits with non-zero status when error occurred.
–man, –doc
Show manual page. Display module’s manual page when used with -M option.
–show, –less
Show module file contents. Use with -M option.
–path
Show module file path. Use with -M option.
–norc
Do not read startup file:
~/.greplerc
. This option have to be placed before any other options including -M module options. SettingGREPLE_NORC
environment have same effect.–conceal type=val
Use following –warn option in reverse context. This option remains for backward compatibility and will be deprecated in the near future.
–persist
Same as –error=retry. It may be deprecated in the future.
–error=action
As greple tries to read data as a character string, sometimes fails to convert them into internal representation, and the file is skipped without processing by default. This works fine to skip binary data. (skip)
Also sometimes encounters code mapping error due to character encoding. In this case, reading the file as a binary data helps to produce meaningful output. (retry)
This option specifies the action when data read error occurred.
skip
Skip the file. Default.
retry
Retry reading the file as a binary data.
fatal
Abort the operation.
ignore
Ignore error and continue to read anyway.
You may occasionally want to find text in binary data. Next command will work like string(1) command.
greple -o --re '(?a)\w{4,}' --error=retry --uc /bin/*
If you want read all files as binary data, use –icode=binary instead.
-w, –warn type=[
0
,1
]Control runtime message mainly about file operation related to –error option. Repeatable. Value is optional and 1 is assumed when omitted. So -wall option is same as -wall=1 and enables all messages, and -wall=0 disables all.
Types are:
read
(Default 0) Errors occurred during file read. Mainly unicode related errors when reading binary or ambiguous text file.
skip
(Default 1) File skip message.
retry
(Default 0) File retry message.
begin
(Default 0) When –begin function died with
/^SKIP/i
message, the file is skipped without any notice. Enables this to see the dying message.all
Set same value for all types.
–alert [
size
=#,time
=# ]Set alert parameter for large file. Greple scans whole file content to know line borders, and it takes several seconds or more if it contains large number of lines.
By default, if the target file contains more than 512 * 1024 characters (size), 2 seconds timer will start (time). Alert message is shown when the timer expired.
To disable this alert, set the size as zero:
--alert size=0
-Mdebug, -dx
Debug option is described in App::Greple::debug module.
ENVIRONMENT and STARTUP FILE
GREPLEOPTS
Environment variable GREPLEOPTS is used as a default options. They are inserted before command line options.
GREPLE_NORC
If set non-empty string, startup file
~/.greplerc
is not processed.DEBUG_GETOPT
Enable Getopt::Long debug option.
DEBUG_GETOPTEX
Enable Getopt::EX debug option.
NO_COLOR
If true, all coloring capability with ANSI terminal sequence is disabled. See https://no-color.org/.
Before starting execution, greple reads the file named .greplerc
on user’s home directory. Following directives can be used.
option name string
Argument name of option directive is user defined option name. The rest are processed by
shellwords
routine defined in Text::ParseWords module. Be sure that this module sometimes requires escape backslashes.Any kind of string can be used for option name but it is not combined with other options.
option --fromcode --outside='(?s)\/\*.*?\*\/' option --fromcomment --inside='(?s)\/\*.*?\*\/'
If the option named default is defined, it will be used as a default option.
For the purpose to include following arguments within replaced strings, two special notations can be used in option definition. String
$<n>
is replaced by the _n_th argument after the substituted option, where n is number start from one. String$<shift>
is replaced by following command line argument and the argument is removed from option list.For example, when
option --line --le &line=$<shift>
is defined, command
greple --line 10,20-30,40
will be evaluated as this:
greple --le &line=10,20-30,40
expand name string
Define local option name. Command expand is almost same as command option in terms of its function. However, option defined by this command is expanded in, and only in, the process of definition, while option definition is expanded when command arguments are processed.
This is similar to string macro defined by following define command. But macro expansion is done by simple string replacement, so you have to use expand to define option composed by multiple arguments.
define name string
Define macro. This is similar to option, but argument is not processed by shellwords and treated just a simple text, so meta-characters can be included without escape. Macro expansion is done for option definition and other macro definition. Macro is not evaluated in command line option. Use option directive if you want to use in command line,
define (#kana) \p{InKatakana} option --kanalist --nocolor -o --join --re '(#kana)+(\n(#kana)+)*' help --kanalist List up Katakana string
help name
If help directive is used for same option name, it will be printed in usage message. If the help message is
ignore
, corresponding line won’t show up in the usage.builtin spec variable
Define built-in option which should be processed by option parser. Arguments are assumed to be Getopt::Long style spec, and variable is string start with
$
,@
or%
. They will be replaced by a reference to the object which the string represent.See pgp module for example.
autoload module options …
Define module which should be loaded automatically when specified option is found in the command arguments.
For example,
autoload -Mdig --dig --git
replaces option “
--dig
” to “-Mdig --dig
”, so that dig module is loaded before processing--dig
option.
Environment variable substitution is done for string specified by
option
and define
directives. Use Perl syntax $ENV{NAME} for
this purpose. You can use this to make a portable module.
When greple found __PERL__
line in .greplerc
file, the rest
of the file is evaluated as a Perl program. You can define your own
subroutines which can be used by –inside/outside,
–include/exclude, –block options.
For those subroutines, file content will be provided by global
variable $_
. Expected response from the subroutine is the list of
array references, which is made up by start and end offset pairs.
For example, suppose that the following function is defined in your
.greplerc
file. Start and end offset for each pattern match can be
taken as array element $-[0]
and $+[0]
.
__PERL__
sub odd_line {
my @list;
my $i;
while (/.*\n/g) {
push(@list, [ $-[0], $+[0] ]) if ++$i % 2;
}
@list;
}
You can use next command to search pattern included in odd number lines.
% greple --inside '&odd_line' pattern files...
MODULE
You can expand the greple command using module. Module files are
placed at App/Greple/
directory in Perl library, and therefor has
App::Greple::module package name.
In the command line, module have to be specified preceding any other options in the form of -Mmodule. However, it also can be specified at the beginning of option expansion.
If the package name is declared properly, __DATA__
section in the
module file will be interpreted same as .greplerc
file content. So
you can declare the module specific options there. Functions declared
in the module can be used from those options, it makes highly
expandable option/programming interaction possible.
Using -M without module argument will print available module list. Option –man will display module document when used with -M option. Use –show option to see the module itself. Option –path will print the path of module file.
See this sample module code. This sample defines options to search from pod, comment and other segment in Perl script. Those capability can be implemented both in function and macro.
package App::Greple::perl;
use Exporter 'import';
our @EXPORT = qw(pod comment podcomment);
our %EXPORT_TAGS = ( );
our @EXPORT_OK = qw();
use App::Greple::Common;
use App::Greple::Regions;
my $pod_re = qr{^=\w+(?s:.*?)(?:\Z|^=cut\s*\n)}m;
my $comment_re = qr{^(?:[ \t]*#.*\n)+}m;
sub pod {
match_regions(pattern => $pod_re);
}
sub comment {
match_regions(pattern => $comment_re);
}
sub podcomment {
match_regions(pattern => qr/$pod_re|$comment_re/);
}
1;
__DATA__
define :comment: ^(\s*#.*\n)+
define :pod: ^=(?s:.*?)(?:\Z|^=cut\s*\n)
#option --pod --inside :pod:
#option --comment --inside :comment:
#option --code --outside :pod:|:comment:
option --pod --inside '&pod'
option --comment --inside '&comment'
option --code --outside '&podcomment'
You can use the module like this:
greple -Mperl --pod default greple
greple -Mperl --colorful --code --comment --pod default greple
If special subroutine initialize()
and finalize()
are defined in
the module, they are called at the beginning with
Getopt::EX::Module object as a first argument. Second argument is
the reference to @ARGV
, and you can modify actual @ARGV
using
it. See App::Greple::find module as an example.
Calling sequence is like this. See Getopt::EX::Module for detail.
1) Call initialize()
2) Call function given in -Mmod::func() style
3) Call finalize()
HISTORY
Most capability of greple is derived from mg command, which has been developing from early 1990’s by the same author. Because modern standard grep family command becomes to have similar capabilities, it is a time to clean up entire functionalities, totally remodel the option interfaces, and change the command name. (2013.11)
SEE ALSO
App::Greple, https://github.com/kaz-utashiro/greple
Getopt::EX, https://github.com/kaz-utashiro/Getopt-EX
AUTHOR
Kazumasa Utashiro
LICENSE
Copyright 1991-2022 Kazumasa Utashiro
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
1.2 - greple@v8
NAME
greple - extensible grep with lexical expression and region control
VERSION
Version 8.60
SYNOPSIS
greple [-Mmodule] [ -options ] pattern [ file… ]
PATTERN
pattern 'and +must -not ?alternative &function'
-x, --le pattern lexical expression (same as bare pattern)
-e, --and pattern pattern match across line boundary
-r, --must pattern pattern cannot be compromised
-v, --not pattern pattern not to be matched
--or pattern alternative pattern group
--re pattern regular expression
--fe pattern fixed expression
-f, --file file file contains search pattern
--select index select indexed pattern from -f file
MATCH
-i ignore case
--need=[+-]n required positive match count
--allow=[+-]n acceptable negative match count
--matchcount=n[,m] required match count for each block
STYLE
-l list filename only
-c print count of matched block only
-n print line number
-H, -h do or do not display filenames
-o print only the matching part
--all print entire data
-m, --max=n[,m] max count of blocks to be shown
-A,-B,-C [n] after/before/both match context
--join delete newline in the matched part
--joinby=string replace newline in the matched text by string
--nonewline do not add newline character at block end
--filestyle=style how filename printed (once, separate, line)
--linestyle=style how line number printed (separate, line)
--separate set filestyle and linestyle both "separate"
--format LABEL=... define line number and file name format
--frame-top top frame
--frame-middle middle frame
--frame-bottom bottom frame
FILE
--glob=glob glob target files
--chdir=dir change directory before search
--readlist get filenames from stdin
COLOR
--color=when use terminal color (auto, always, never)
--nocolor same as --color=never
--colormap=color R, G, B, C, M, Y etc.
--colorful use default multiple colors
--colorindex=flags color index method: Ascend/Descend/Block/Random
--ansicolor=s ANSI color 16, 256 or 24bit
--[no]256 same as --ansicolor 256 or 16
--regioncolor use different color for inside/outside regions
--uniqcolor use different color for unique string
--uniqsub=func preprocess function before check uniqueness
--random use random color each time
--face set/unset visual effects
BLOCK
-p, --paragraph paragraph mode
--border=pattern border pattern
--block=pattern block of records
--blockend=s block end mark (Default: "--")
--join-blocks join back-to-back consecutive blocks
REGION
--inside=pattern select matches inside of pattern
--outside=pattern select matches outside of pattern
--include=pattern reduce matches to the area
--exclude=pattern reduce matches to outside of the area
--strict strict mode for --inside/outside --block
CHARACTER CODE
--icode=name file encoding
--ocode=name output encoding
FILTER
--if,--of=filter input/output filter command
--pf=filter post process filter command
--noif disable default input filter
RUNTIME FUNCTION
--print=func print function
--continue continue after print function
--callback=func callback function for matched string
--begin=func call function before search
--end=func call function after search
--prologue=func call function before command execution
--epilogue=func call function after command execution
OTHER
--usage[=expand] show this message
--exit=n command exit status
--norc skip reading startup file
--man display command or module manual page
--show display module file
--path show module file path
--persist same as --error=retry
--error=action action after read error
--warn=type run time error control
--alert [name=#] set alert parameter (size/time)
-d flags display info (f:file d:dir c:color m:misc s:stat)
INSTALL
CPANMINUS
$ cpanm App::Greple
DESCRIPTION
MULTIPLE KEYWORDS
greple has almost same function as Unix command egrep(1) but
search is done in a manner similar to Internet search engine. For
example, next command print lines those contain all of foo
and
bar
and baz
.
greple 'foo bar baz' ...
Each word can appear in any order and any place in the string. So this command find all of following lines.
foo bar baz
baz bar foo
the foo, bar and baz
If you want to use OR syntax, prepend question mark (?
) on each
token, or use regular expression.
greple 'foo bar baz ?yabba ?dabba ?doo'
greple 'foo bar baz yabba|dabba|doo'
This command will print lines those contains all of foo
, bar
and
baz
and one or more of yabba
, dabba
or doo
.
NOT operator can be specified by prefixing the token by minus sign
(-
). Next example will show lines those contain both foo
and
bar
but none of yabba
, dabba
or doo
.
greple 'foo bar -yabba -dabba -doo'
This can be written as this using -e and -v option.
greple -e foo -e bar -v yabba -v dabba -v doo
greple -e foo -e bar -v 'yabba|dabba|doo'
If +
is placed to positive matching pattern, that pattern is marked
as required, and required match count is automatically set to the
number of required patterns. So
greple '+foo bar baz'
commands implicitly set the option --need 1
, and consequently print
all lines including foo
. In other words, it makes other patterns
optional, but they are highlighted if exist. If you want to search
lines which includes foo
and either or both of bar
and baz
,
use like this:
greple '+foo bar baz' --need 2
greple '+foo bar baz' --need +1
greple 'foo bar|baz'
FLEXIBLE BLOCKS
Default data block greple search and print is a line. Using
–paragraph (or -p in short) option, series of text separated by
empty line is taken as a record block. So next command will print
whole paragraph which contains the word foo
, bar
and baz
.
greple -p 'foo bar baz'
Block also can be defined by pattern. Next command treat the data as a series of 10-line unit.
greple -n --border='(.*\n){1,10}'
You can also define arbitrary complex blocks by writing script.
greple --block '&your_original_function' ...
MATCH AREA CONTROL
Using option –inside and –outside, you can specify the text area to be matched. Next commands search only in mail header and body area respectively. In these cases, data block is not changed, so print lines which contains the pattern in the specified area.
greple --inside '\A(.+\n)+' pattern
greple --outside '\A(.+\n)+' pattern
Option –inside/–outside can be used repeatedly to enhance the area to be matched. There are similar option –include/–exclude, but they are used to trim down the area.
These four options also take user defined function and any complex region can be used.
LINE ACROSS MATCH
greple search a given pattern across line boundaries. This is especially useful to handle Asian multi-byte text, more specifically Japanese. Japanese text can be separated by newline almost any place in the text. So the search pattern may spread out onto multiple lines.
As for ascii word list, space character in the pattern matches any
kind of space including newline. Next example will search the word
sequence of foo
, bar
and baz
, even they spread out to
multiple lines.
greple -e 'foo bar baz'
Option -e is necessary because space is taken as a token separator in the bare or –le pattern.
MODULE AND CUSTOMIZATION
User can define default and original options in ~/.greplerc
. Next
example enables colored output always, and define new option using
macro processing.
option default --color=always
define :re1 complex-regex-1
define :re2 complex-regex-2
define :re3 complex-regex-3
option --newopt --inside :re1 --exclude :re2 --re :re3
Specific set of function and option interface can be implemented as module. Modules are invoked by -M option immediately after command name.
For example, greple does not have recursive search option, but it can be implemented by –readlist option which accept target file list from standard input. Using find module, it can be written like this:
greple -Mfind . -type f -- pattern
Also dig module implements more complex search. It can be used as simple as this:
greple -Mdig pattern --dig .
but this command is finally translated into following option list.
greple -Mfind . ( -name .git -o -name .svn -o -name RCS ) -prune -o
-type f ! -name .* ! -name *,v ! -name *~
! -iname *.jpg ! -iname *.jpeg ! -iname *.gif ! -iname *.png
! -iname *.tar ! -iname *.tbz ! -iname *.tgz ! -iname *.pdf
-print -- pattern
INCLUDED MODUES
This release include some sample modules. Read document in each modules for detail. You can read the document by –man option or perldoc command.
greple -Mdig --man
perldoc App::Greple::dig
When it does not work, use perldoc App::Greple::dig
.
colors
Color variation module. See App::Greple::colors.
find
Module to use find(1) command to help recursive search. See App::Greple::find.
dig
Module for recursive search using find module. Defines –dig, –git and –git-r options. See App::Greple::dig.
pgp
Module to search pgp files. See App::Greple::pgp.
select
Module to select files. See App::Greple::select.
perl
Sample module to search from perl source files. See App::Greple::perl.
Other modules are available at CPAN, or git repository https://github.com/kaz-utashiro/.
OPTIONS
PATTERNS
If no specific option is given, greple takes the first argument as a search pattern specified by –le option. All of these patterns can be specified multiple times.
Command itself is written in Perl, and any kind of Perl style regular expression can be used in patterns. See perlre(1) for detail.
Note that multiple line modifier (m
) is set when executed, so put
(?-m)
at the beginning of regex if you want to explicitly disable
it.
Order of capture group in the pattern is not guaranteed. Please avoid
to use direct index, and use relative or named capture group instead.
For example, if you want to search repeated characters, use
(\w)\g{-1}
or (?<c>\w)\g{c}
rather than
(\w)\1
.
-x pattern, –le=pattern
Treat the string as a collection of tokens separated by spaces. Each token is interpreted by the first character. Token start with
-
means negative pattern,?
means alternative, and+
does required.Next example print lines which contains
foo
andbar
, and one or more ofyabba
anddabba
, and none ofbaz
anddoo
.greple --le='foo bar -baz ?yabba ?dabba -doo'
Multiple
?
preceded tokens are treated all mixed together. That means?A|B ?C|D
is equivalent to?A|B|C|D
. If you want to mean(A or B)
and(C or D)
, use AND syntax instead:A|B C|D
.This is the summary of start character for –le option:
+ Required pattern - Negative match pattern ? Alternative pattern & Function call (see next section)
-x=[+-]&function, –le=[+-]&function
If the pattern start with ampersand (
&
), it is treated as a function, and the function is called instead of searching pattern. Function call interface is same as the one for block/region options.If you have a definition of odd_line function in you
.greplerc
, which is described in this manual later, you can print odd number lines like this:greple -n '&odd_line' file
Required (
+
) and negative (-
) mark can be used for function pattern.This version experimentally support callback function for each pattern. Region list returned by function can have two extra element besides start/end position. Third element is index. Fourth element is callback function pointer which is called to produce string to be shown in command output. Callback function takes four argument (start position, end position, index, matched string) and returns replacement string.
-e pattern, –and=pattern
Specify positive match token. Next two commands are equivalent.
greple 'foo bar baz' greple -e foo -e bar -e baz
First character is not interpreted, so next commands will search the pattern
-baz
.greple -e -baz
Space characters are treated specially by -e and -v options. They are replaced by the pattern which matches any number of white spaces including newline. So the pattern can be expand to multiple lines. Next commands search the series of word
foo
,bar
andbaz
even if they are separated by newlines.greple -e 'foo bar baz'
-r pattern, –must=pattern
Specify required match token. Next two commands are equivalent.
greple '+foo bar baz' greple -r foo -e bar -e baz
-v pattern, –not=pattern
Specify negative match token. Because it does not affect to the bare pattern argument, you can narrow down the search result like this.
greple foo file greple foo file -v bar greple foo file -v bar -v baz
–or=pattern
Specify logical-or match token group. Same as
?
marked token in –le option. Next commands are all equivalent.greple --le 'foo bar ?yabba ?dabba' greple --and foo --and bar --or yabba --or dabba greple -e foo -e bar -e 'yabba|dabba'
Option –or group and each –le pattern makes individual pattern. So
greple --le '?foo ?yabba' --le '?bar ?dabba' --or baz --or doo
is same as:
greple -e 'foo|yabba' -e 'bar|dabba' -e 'baz|doo'
–re=pattern
Specify regular expression. No special treatment for space and wide characters.
–fe=pattern
Specify fixed string pattern, like fgrep.
-i, –ignore-case
Ignore case.
–need=n
–allow=n
Option to compromise matching condition. Option –need specifies the required match count, and –allow the number of negative condition to be overlooked.
greple --need=2 --allow=1 'foo bar baz -yabba -dabba -doo'
Above command prints the line which contains two or more from
foo
,bar
andbaz
, and does not include more than one ofyabba
,dabba
ordoo
.Using option –need=1, greple produces same result as grep command.
grep -e foo -e bar -e baz greple -e foo -e bar -e baz --need=1
When the count n is negative value, it is subtracted from default value.
If the option –need=0 is specified and no pattern was found, entire data is printed. This is true even for required pattern.
–matchcount=count|min,max, –mc=…
When option –matchcount is specified, only blocks which have given match count will be shown. Minimum and maximum number can be given, connecting by comma, and they can be omitted. Next commands print lines including semicolons; 3 or more, exactly 3, and 3 or less, respectively.
greple --matchcount=3, ';' file greple --matchcount=3 ';' file greple --matchcount=,3 ';' file
In fact, min and max can repeat to represent multiple range. Missing, negative or zero max means infinite. Next command find match count 0 to 10, 20 to 30, and 40-or-greater.
greple --matchcount=,10,20,30,40
-f file, –file=file
Specify the file which contains search pattern. When file contains multiple lines, patterns are mixed together by OR context.
Blank line and the line starting with sharp (#) character is ignored. Two slashes (//) and following string are taken as a comment and removed with preceding spaces.
When multiple files specified, each file produces individual pattern.
If the file name is followed by
[index]
string, it is treated as specified by –select option. Next two commands are equivalent.greple -f pattern_file'[1,5:7]' greple -f pattern_file --select 1,5:7
See App::Greple::subst module.
–select=index
When you want to choose specific pattern in the pattern file provided by -f option, use –select option. index is number list separated by comma (,) character and each number is interpreted by Getopt::EX::Numbers module. Take a look at the module document for detail.
Next command use 1st and 5,6,7th pattern in the file.
greple -f pattern_file --select 1,5:7
STYLES
-l
List filename only.
-c, –count
Print count of matched block.
-n, –line-number
Show line number.
-h, –no-filename
Do not display filename.
-H
Display filename always.
-o, –only-matching
Print matched string only. Newline character is printed after matched string if it does not end with newline. Use –no-newline option if you don’t need extra newline.
–all
Print entire file. This option does not affect to seach behavior or block treatment. Just print all contents.
-m n[,m], –max-count=n[,m]
Set the maximum count of blocks to be shown to n.
Actually n and m are simply passed to perl splice function as offset and length. Works like this:
greple -m 10 # get first 10 blocks greple -m 0,-10 # get last 10 blocks greple -m 0,10 # remove first 10 blocks greple -m -10 # remove last 10 blocks greple -m 10,10 # remove 10 blocks from 10th (10-19)
This option does not affect to search performance and command exit status.
Note that grep command also has same option, but it’s behavior is different when invoked to multiple files. greple produces given number of output for each file, while grep takes it as a total number of output.
-m *, –max-count=*
In fact, n and m can repeat as many as possible. Next example removes first 10 blocks (by
0,10
), then get first 10 blocks from the result (by10
). Consequently, get 10 blocks from 10th (10-19).greple -m 0,10,10
Next command get first 20 (by
20,
) and get last 10 (by,-10
), producing same result. Empty string behaves like absence for length and zero for offset.greple -m 20,,,-10
-A[n], –after-context[=n]
-B[n], –before-context[=n]
-C[n], –context[=n]
Print n-blocks before/after matched string. The value n can be omitted and the default is 2. When used with –paragraph or –block option, n means number of paragraph or block.
Actually, these options expand the area of logical operation. It means
greple -C1 'foo bar baz'
matches following text.
foo bar baz
Moreover
greple -C1 'foo baz'
also matches this text, because matching blocks around
foo
andbar
overlaps each other and makes single block.–join
–joinby=string
Convert newline character found in matched string to empty or specified string. Using –join with -o (only-matching) option, you can collect searching sentence list in one per line form. This is sometimes useful for Japanese text processing. For example, next command prints the list of KATAKANA words, including those spread across multiple lines.
greple -ho --join '\p{InKatakana}+(\n\p{InKatakana}+)*'
Space separated word sequence can be processed with –joinby option. Next example prints all
for *something*
pattern in pod documents within Perl script.greple -Mperl --pod -ioe '\bfor \w+' --joinby ' '
–[no]newline
Since greple can handle arbitrary blocks other than normal text lines, they sometimes do not end with newline character. Option -o makes similar situation. In that case, extra newline is appended at the end of block to be shown. Option –no-newline disables this behavior.
–filestyle=[
line
,once
,separate
], –fsDefault style is line, and greple prints filename at the beginning of each line. Style once prints the filename only once at the first time. Style separate prints filename in the separate line before each line or block.
–linestyle=[
line
,separate
], –lsDefault style is line, and greple prints line numbers at the beginning of each line. Style separate prints line number in the separate line before each line or block.
–separate
Shortcut for –filestyle=separate –linestyle=separate. This is convenient to use block mode search and visiting each location from supporting tool, such as Emacs.
–format LABEL=format
Define the format string of line number (LINE) and file name (FILE) to be displayed. Default is:
--format LINE='%d:' --format FILE='%s:'
Format string is passed to
sprintf
function. Tab character can be expressed as\t
.Next example will show line numbers in five digits with tab space:
--format LINE='%05d\t'
–frame-top=string
–frame-middle=string
–frame-bottom=string
Print surrounding frames before and after each block.
top
frame is printed at the beginning,bottom
frame at the end,middle
frame between blocks.
FILES
–glob=pattern
Get files matches to specified pattern and use them as a target files. Using –chdir and –glob makes easy to use greple for fixed common job.
–chdir=directory
Change directory before processing files. When multiple directories are specified in –chdir option, by using wildcard form or repeating option, –glob file expansion will be done for every directories.
greple --chdir '/usr/man/man?' --glob '*.[0-9]' ...
–readlist
Get filenames from standard input. Read standard input and use each line as a filename for searching. You can feed the output from other command like find(1) for greple with this option. Next example searches string from files modified within 7 days:
find . -mtime -7 -print | greple --readlist pattern
Using find module, this can be done like:
greple -Mfind . -mtime -7 -- pattern
COLORS
–color=[
auto
,always
,never
], –nocolorUse terminal color capability to emphasize the matched text. Default is
auto
: effective when STDOUT is a terminal and option -o is not given, not otherwise. Option valuealways
andnever
will work as expected.Option –nocolor is alias for –color=never.
When color output is disabled, ANSI terminal sequence is not produced, but functional colormap, such as
--cm sub{...}
, still works.–colormap=spec
Specify color map. Because this option is mostly implemented by Getopt::EX::Colormap module, consult its document for detail and up-to-date specification.
Color specification is combination of single uppercase character representing basic colors, and (usually brighter) alternative colors in lowercase:
R r Red G g Green B b Blue C c Cyan M m Magenta Y y Yellow K k Black W w White
or RGB value and 24 grey levels if using ANSI 256 color terminal:
(255,255,255) : 24bit decimal RGB colors #000000 .. #FFFFFF : 24bit hex RGB colors #000 .. #FFF : 12bit hex RGB 4096 colors 000 .. 555 : 6x6x6 RGB 216 colors L00 .. L25 : Black (L00), 24 grey levels, White (L25)
Beginning # can be omitted in 24bit RGB notation. When values are all same in 24bit or 12bit RGB, it is converted to 24 grey level, otherwise 6x6x6 216 color.
or color names enclosed by angle bracket:
<red> <blue> <green> <cyan> <magenta> <yellow> <aliceblue> <honeydue> <hotpink> <mooccasin> <medium_aqua_marine>
with other special effects:
N None Z 0 Zero (reset) D 1 Double strike (boldface) P 2 Pale (dark) I 3 Italic U 4 Underline F 5 Flash (blink: slow) Q 6 Quick (blink: rapid) S 7 Stand out (reverse video) H 8 Hide (concealed) X 9 Cross out E Erase Line ; No effect / Toggle foreground/background ^ Reset to foreground
If the spec includes
/
, left side is considered as foreground color and right side as background. If multiple colors are given in same spec, all indicators are produced in the order of their presence. As a result, the last one takes effect.Effect characters are case insensitive, and can be found anywhere and in any order in color spec string. Character
;
does nothing and can be used just for readability, likeSD;K/544
.Example:
RGB 6x6x6 12bit 24bit color name === ======= ========= ============= ================== B 005 #00F (0,0,255) <blue> /M /505 /#F0F /(255,0,255) /<magenta> K/W 000/555 #000/#FFF 000000/FFFFFF <black>/<white> R/G 500/050 #F00/#0F0 FF0000/00FF00 <red>/<green> W/w L03/L20 #333/#ccc 303030/c6c6c6 <dimgrey>/<lightgrey>
Multiple colors can be specified separating by white space or comma, or by repeating options. Those colors will be applied for each pattern keywords. Next command will show word
foo
in red,bar
in green andbaz
in blue.greple --colormap='R G B' 'foo bar baz' greple --cm R -e foo --cm G -e bar --cm B -e baz
Coloring capability is implemented in Getopt::EX::Colormap module.
–colormap=field=spec,…
Another form of colormap option to specify the color for fields:
FILE File name LINE Line number TEXT Unmatched normal text BLOCKEND Block end mark PROGRESS Progress status with -dnf option
In current release,
BLOCKEND
mark is colored withE
effect recently implemented in Getopt::EX module, which allows to fill up the line with background color. This effect uses irregular escape sequence, and you may need to defineLESSANSIENDCHARS
environment as “mK” to see the result with less command.–colormap=
&func
–colormap=
sub{...}
You can also set the name of perl subroutine name or definition to be called handling matched words. Target word is passed as variable
$_
, and the return value of the subroutine will be displayed.Next command convert all words in C comment to upper case.
greple --all '/\*(?s:.*?)\*/' --cm 'sub{uc}'
You can quote matched string instead of coloring (this emulates deprecated option –quote):
greple --cm 'sub{"<".$_.">"}' ...
It is possible to use this definition with field names. Next example print line numbers in seven digits.
greple -n --cm 'LINE=sub{s/(\d+)/sprintf("%07d",$1)/e;$_}'
Experimentally, function can be combined with other normal color specifications. Also the form
&func;
can be repeated.greple --cm 'BF/544;sub{uc}' greple --cm 'R;&func1;&func2;&func3'
When color for ‘TEXT’ field is specified, whole text including matched part is passed to the function, exceptionally. It is not recommended to use user defined function for ‘TEXT’ field.
–[no]colorful
Shortcut for –colormap=’
RD GD BD CD MD YD
’ in ANSI 16 colors mode, and –colormap=’D/544 D/454 D/445 D/455 D/454 D/554
’ and other combination of 3, 4, 5 for 256 colors mode. Enabled by default.When single pattern is specified, first color in colormap is used for the pattern. If multiple patterns and multiple colors are specified, each pattern is colored with corresponding color cyclically.
Option –regioncolor, –uniqcolor and –colorindex change this behavior.
–colorindex=spec, –ci=spec
Specify color index method by combination of spec characters. A (ascend) and D (descend) can be mixed with B (block) and/or S (shuffle) like –ci=ABS. R (random) can be too but it does not make sense. When S is used alone, colormap is shuffled with normal behavior.
A (Ascending)
Apply different color sequentially according to the order of appearance.
D (Descending)
Apply different color sequentially according to the reverse order of appearance.
B (Block)
Reset sequential index on every block.
S (Shuffle)
Shuffle indexed color.
R (Random)
Use random color index every time.
N (Normal)
Reset to normal behavior. Because the last option takes effect, –ci=N can be used to reset the behavior set by previous options.
–random
Shortcut for –colorindex=R.
–ansicolor=[
16
,256
,24bit
]If set as
16
, use ANSI 16 colors as a default color set, otherwise ANSI 256 colors. When set as24bit
, 6 hex digits notation produces 24bit color sequence. Default is256
.–[no]256
Shortcut for –ansicolor=
256
or16
.–[no]regioncolor, –[no]rc
Use different colors for each –inside/outside region.
Disabled by default, but automatically enabled when only single search pattern is specified. Use –no-regioncolor to cancel automatic action.
–[no]uniqcolor, –[no]uc
Use different colors for different string matched. Disabled by default.
Next example prints all words start by
color
and display them all in different colors.greple --uniqcolor 'colou?r\w*'
When used with option -i, color is selected still in case-sensitive fashion. If you want case-insensitive color selection, use next –uniqsub option.
–uniqsub=function, –us=function
Above option –uniqcolor set same color for same literal string. Option –uniqsub specify the preprocessor code applied before comparison. function get matched string by
$_
and returns the result. For example, next command will choose unique colors for each word by their length.greple --uniqcolor --uniqsub 'sub{length}' '\w+' file
If you want case-insensitive color selection, do like this.
greple -i pattern --uc --uniqsub 'sub{lc}'
Next command read the output from
git blame
command and set unique color for each entire line by their commit ids.git blame ... | greple .+ --uc --us='sub{s/\s.*//r}' --face=E-D
–face=[-+]effect
Set or unset specified effect for all indexed color specs. Use
+
(optional) to set, and-
to unset. Effect is a single character expressing S (Stand-out), U (Underline), D (Double-struck), F (Flash) and such.Next example remove D (double-struck) effect.
greple --face -D
Multiple effects can be set/unset at once.
greple --face SF-D
BLOCKS
-p, –paragraph
Print a paragraph which contains the pattern. Each paragraph is delimited by two or more successive newlines by default. Be aware that an empty line is not a paragraph delimiter if which contains space characters. Example:
greple -np 'setuid script' /usr/man/catl/perl.l greple -pe '^struct sockaddr' /usr/include/sys/socket.h
It changes the unit of context specified by -A, -B, -C options. Space gap between paragraphs are also treated as block unit. Thus, option -pC2 will print with previous and next paragraph, while -pC1 will print with just surrounding spaces.
You can create original paragraph pattern by –border option.
–border=pattern
Specify record block border pattern. Pattern match is done in the context of multiple line mode.
Default block is a single line and use
/^/m
as a pattern. Paragraph mode uses/(?:\A|\R)\K\R+/
, which means continuous newlines at the beginning of text or following another newline (\R
means more general linebreaks including\r\n
; consult perlrebackslash for detail).Next command treat the data as a series of 10-line unit.
greple -n --border='(.*\n){1,10}'
Contrary to the next –block option, –border never produce disjoint records.
If you want to treat entire file as a single block, setting border to start or end of whole data is efficient way. Next commands works same.
greple --border '\A' # beginning of file greple --border '\z' # end of file
–block=pattern
–block=&sub
Specify the record block to display. Default block is a single line.
Empty blocks are ignored. When blocks are not continuous, the match occurred outside blocks are ignored.
If multiple block options are given, overlapping blocks are merged into a single block.
Please be aware that this option is sometimes quite time consuming, because it finds all blocks before processing.
–blockend=string
Change the end mark displayed after -pABC or –block options. Default value is “–”.
–join-blocks
Join consecutive blocks together. Logical operation is done for each individual blocks, but if the results are back-to-back connected, make them single block for final output.
REGIONS
–inside=pattern
–outside=pattern
Option –inside and –outside limit the text area to be matched. For simple example, if you want to find string
and
not in the wordcommand
, it can be done like this.greple --outside=command and
The block can be larger and expand to multiple lines. Next command searches from C source, excluding comment part.
greple --outside '(?s)/\*.*?\*/'
Next command searches only from POD part of the perl script.
greple --inside='(?s)^=.*?(^=cut|\Z)'
When multiple inside and outside regions are specified, those regions are mixed up in union way.
In multiple color environment, and if single keyword is specified, matches in each –inside/outside region is printed in different color. Forcing this operation with multiple keywords, use –regioncolor option.
–inside=&function
–outside=&function
If the pattern name begins by ampersand (&) character, it is treated as a name of subroutine which returns a list of blocks. Using this option, user can use arbitrary function to determine from what part of the text they want to search. User defined function can be defined in
.greplerc
file or by module option.–include=pattern
–exclude=pattern
–include=&function
–exclude=&function
–include/exclude option behave exactly same as –inside/outside when used alone.
When used in combination, –include/exclude are mixed in AND manner, while –inside/outside are in OR.
Thus, in the next example, first line prints all matches, and second does none.
greple --inside PATTERN --outside PATTERN greple --include PATTERN --exclude PATTERN
You can make up desired matches using –inside/outside option, then remove unnecessary part by –include/exclude
–strict
Limit the match area strictly.
By default, –block, –inside/outside, –include/exclude option allows partial match within the specified area. For instance,
greple --inside and command
matches pattern
command
because the part of matched string is included in specified inside-area. Partial match fails when option –strict provided, and longer string never matches within shorter area.Interestingly enough, above example
greple --include PATTERN --exclude PATTERN
produces output, as a matter of fact. Think of the situation searching, say,
' PATTERN '
with this condition. Matched area includes surrounding spaces, and satisfies both conditions partially. This match does not occur when option –strict is given, either.
CHARACTER CODE
–icode=code
Target file is assumed to be encoded in utf8 by default. Use this option to set specific encoding. When handling Japanese text, you may choose from 7bit-jis (jis), euc-jp or shiftjis (sjis). Multiple code can be supplied using multiple option or combined code names with space or comma, then file encoding is guessed from those code sets. Use encoding name
guess
for automatic recognition from default code list which is euc-jp and 7bit-jis. Following commands are all equivalent.greple --icode=guess ... greple --icode=euc-jp,7bit-jis ... greple --icode=euc-jp --icode=7bit-jis ...
Default code set are always included suspect code list. If you have just one code adding to suspect list, put + mark before the code name. Next example does automatic code detection from euc-kr, ascii, utf8 and UTF-16/32.
greple --icode=+euc-kr ...
If the string “binary” is given as encoding name, no character encoding is expected and all files are processed as binary data.
–ocode=code
Specify output code. Default is utf8.
FILTER
–if=filter, –if=EXP:filter
You can specify filter command which is applied to each file before search. If only one filter command is specified, it is applied to all files. If filter information include colon, first field will be perl expression to check the filename saved in variable $_. If it successes, next filter command is pushed.
greple --if=rev perg greple --if='/\.tar$/:tar tvf -'
If the command doesn’t accept standard input as processing data, you may be able to use special device:
greple --if='nm /dev/stdin' crypt /usr/lib/lib*
Filters for compressed and gzipped file is set by default unless –noif option is given. Default action is like this:
greple --if='s/\.Z$//:zcat' --if='s/\.g?z$//:gunzip -c'
File with
.gpg
suffix is filtered by gpg command. In that case, pass-phrase is asked for each file. If you want to input pass-phrase only once to find from multiple files, use -Mpgp module.If the filter start with
&
, perl subroutine is called instead of external command. You can define the subroutine in.greplerc
or modules. Greple simply call the subroutine, so it should be responsible for process control.–noif
Disable default input filter. Which means compressed files will not be decompressed automatically.
–of=filter
–of=&func
Specify output filter which process the output of greple command. Filter command can be specified in multiple times, and they are invoked for each file to be processed. So next command reset the line number for each file.
greple --of 'cat -n' string file1 file2 ...
If the filter start with
&
, perl subroutine is called instead of external command. You can define the subroutine in.greplerc
or modules.Output filter command is executed only when matched string exists to avoid invoking many unnecessary processes. No effect for option -l and -c.
–pf=filter
–pf=&func
Similar to –of filter but invoked just once and takes care of entire output from greple command.
RUNTIME FUNCTIONS
–print=function
–print=sub{…}
Specify user defined function executed before data print. Text to be printed is replaced by the result of the function. Arbitrary function can be defined in
.greplerc
file or module. Matched data is placed in variable$_
. Filename is passed by&FILELABEL
key, as described later.It is possible to use multiple –print options. In that case, second function will get the result of the first function. The command will print the final result of the last function.
–continue
When –print option is given, greple will immediately print the result returned from print function and finish the cycle. Option –continue forces to continue normal printing process after print function called. So please be sure that all data being consistent.
–callback=function(…)
Callback function is called before printing every matched pattern with four labeled parameters: start, end, index and match, which corresponds to start and end position in the text, pattern index, and the matched string. Matched string in the text is replaced by returned string from the function.
–begin=function(…)
–begin=function=…
Option –begin specify the function executed at the beginning of each file processing. This function have to be called from main package. So if you define the function in the module package, use the full package name or export properly.
If the function dies with a message starting with a word “SKIP” (
/^SKIP/i
), that file is simply skipped. So you can control if the file is to be processed using the file name or content. To see the message, use –warn begin=1 option.For example, using next function, only perl related files will be processed.
sub is_perl { my %arg = @_; my $name = delete $arg{&FILELABEL} or die; $name =~ /\.(?:pm|pl|PL|pod)$/ or /\A#!.*\bperl/ or die "skip $name\n"; } 1; __DATA__ option default --filestyle=once --format FILE='\n%s:\n' autoload -Mdig --dig option --perl $<move> --begin &__PACKAGE__::is_perl --dig .
–end=function(…)
–end=function=…
Option –end is almost same as –begin, except that the function is called after the file processing.
–prologue=function(…)
–prologue=function=…
–epilogue=function(…)
–epilogue=function=…
Option –prologue and –epilogue specify functions called before and after processing. During the execution, file is not opened and therefore, file name is not given to those functions.
-Mmodule::function(…)
-Mmodule::function=…
Function can be given with module option, following module name. In this form, the function will be called with module package name. So you don’t have to export it. Because it is called only once at the beginning of command execution, before starting file processing,
FILELABEL
parameter is not given exceptionally.
For these run-time functions, optional argument list can be set in the
form of key
or key=value
, connected by comma. These arguments
will be passed to the function in key => value list. Sole key will
have the value one. Also processing file name is passed with the key
of FILELABEL
constant. As a result, the option in the next form:
--begin function(key1,key2=val2)
--begin function=key1,key2=val2
will be transformed into following function call:
function(&FILELABEL => "filename", key1 => 1, key2 => "val2")
As described earlier, FILELABEL
parameter is not given to the
function specified with module option. So
-Mmodule::function(key1,key2=val2)
-Mmodule::function=key1,key2=val2
simply becomes:
function(key1 => 1, key2 => "val2")
The function can be defined in .greplerc
or modules. Assign the
arguments into hash, then you can access argument list as member of
the hash. It’s safe to delete FILELABEL key if you expect random
parameter is given. Content of the target file can be accessed by
$_
. Ampersand (&
) is required to avoid the hash key is
interpreted as a bare word.
sub function {
my %arg = @_;
my $filename = delete $arg{&FILELABEL};
$arg{key1}; # 1
$arg{key2}; # "val2"
$_; # contents
}
OTHERS
–usage[=expand]
Greple print usage and exit with option –usage, or no valid parameter is not specified. In this case, module option is displayed with help information if available. If you want to see how they are expanded, supply something not empty to –usage option, like:
greple -Mmodule --usage=expand
–exit=number
When greple executed normally, it exit with status 0 or 1 depending on something matched or not. Sometimes we want to get status 0 even if nothing matched. This option set the status code for normal execution. It still exits with non-zero status when error occurred.
–man, –doc
Show manual page. Display module’s manual page when used with -M option.
–show, –less
Show module file contents. Use with -M option.
–path
Show module file path. Use with -M option.
–norc
Do not read startup file:
~/.greplerc
. This option have to be placed before any other options including -M module options. SettingGREPLE_NORC
environment have same effect.–conceal type=val
Use following –warn option in reverse context. This option remains for backward compatibility and will be deprecated in the near future.
–persist
Same as –error=retry. It may be deprecated in the future.
–error=action
As greple tries to read data as a character string, sometimes fails to convert them into internal representation, and the file is skipped without processing by default. This works fine to skip binary data. (skip)
Also sometimes encounters code mapping error due to character encoding. In this case, reading the file as a binary data helps to produce meaningful output. (retry)
This option specifies the action when data read error occurred.
skip
Skip the file. Default.
retry
Retry reading the file as a binary data.
fatal
Abort the operation.
ignore
Ignore error and continue to read anyway.
You may occasionally want to find text in binary data. Next command will work like string(1) command.
greple -o --re '(?a)\w{4,}' --error=retry --uc /bin/*
If you want read all files as binary data, use –icode=binary instead.
-w, –warn type=[
0
,1
]Control runtime message mainly about file operation related to –error option. Repeatable. Value is optional and 1 is assumed when omitted. So -wall option is same as -wall=1 and enables all messages, and -wall=0 disables all.
Types are:
read
(Default 0) Errors occurred during file read. Mainly unicode related errors when reading binary or ambiguous text file.
skip
(Default 1) File skip message.
retry
(Default 0) File retry message.
begin
(Default 0) When –begin function died with
/^SKIP/i
message, the file is skipped without any notice. Enables this to see the dying message.all
Set same value for all types.
–alert [
size
=#,time
=# ]Set alert parameter for large file. Greple scans whole file content to know line borders, and it takes several seconds or more if it contains large number of lines.
By default, if the target file contains more than 512 * 1024 characters (size), 2 seconds timer will start (time). Alert message is shown when the timer expired.
To disable this alert, set the size as zero:
--alert size=0
-Mdebug, -dx
Debug option is described in App::Greple::debug module.
ENVIRONMENT and STARTUP FILE
GREPLEOPTS
Environment variable GREPLEOPTS is used as a default options. They are inserted before command line options.
GREPLE_NORC
If set non-empty string, startup file
~/.greplerc
is not processed.DEBUG_GETOPT
Enable Getopt::Long debug option.
DEBUG_GETOPTEX
Enable Getopt::EX debug option.
NO_COLOR
If true, all coloring capability with ANSI terminal sequence is disabled. See https://no-color.org/.
Before starting execution, greple reads the file named .greplerc
on user’s home directory. Following directives can be used.
option name string
Argument name of option directive is user defined option name. The rest are processed by
shellwords
routine defined in Text::ParseWords module. Be sure that this module sometimes requires escape backslashes.Any kind of string can be used for option name but it is not combined with other options.
option --fromcode --outside='(?s)\/\*.*?\*\/' option --fromcomment --inside='(?s)\/\*.*?\*\/'
If the option named default is defined, it will be used as a default option.
For the purpose to include following arguments within replaced strings, two special notations can be used in option definition. String
$<n>
is replaced by the _n_th argument after the substituted option, where n is number start from one. String$<shift>
is replaced by following command line argument and the argument is removed from option list.For example, when
option --line --le &line=$<shift>
is defined, command
greple --line 10,20-30,40
will be evaluated as this:
greple --le &line=10,20-30,40
expand name string
Define local option name. Command expand is almost same as command option in terms of its function. However, option defined by this command is expanded in, and only in, the process of definition, while option definition is expanded when command arguments are processed.
This is similar to string macro defined by following define command. But macro expansion is done by simple string replacement, so you have to use expand to define option composed by multiple arguments.
define name string
Define macro. This is similar to option, but argument is not processed by shellwords and treated just a simple text, so meta-characters can be included without escape. Macro expansion is done for option definition and other macro definition. Macro is not evaluated in command line option. Use option directive if you want to use in command line,
define (#kana) \p{InKatakana} option --kanalist --nocolor -o --join --re '(#kana)+(\n(#kana)+)*' help --kanalist List up Katakana string
help name
If help directive is used for same option name, it will be printed in usage message. If the help message is
ignore
, corresponding line won’t show up in the usage.builtin spec variable
Define built-in option which should be processed by option parser. Arguments are assumed to be Getopt::Long style spec, and variable is string start with
$
,@
or%
. They will be replaced by a reference to the object which the string represent.See pgp module for example.
autoload module options …
Define module which should be loaded automatically when specified option is found in the command arguments.
For example,
autoload -Mdig --dig --git
replaces option “
--dig
” to “-Mdig --dig
”, so that dig module is loaded before processing--dig
option.
Environment variable substitution is done for string specified by
option
and define
directives. Use Perl syntax $ENV{NAME} for
this purpose. You can use this to make a portable module.
When greple found __PERL__
line in .greplerc
file, the rest
of the file is evaluated as a Perl program. You can define your own
subroutines which can be used by –inside/outside,
–include/exclude, –block options.
For those subroutines, file content will be provided by global
variable $_
. Expected response from the subroutine is the list of
array references, which is made up by start and end offset pairs.
For example, suppose that the following function is defined in your
.greplerc
file. Start and end offset for each pattern match can be
taken as array element $-[0]
and $+[0]
.
__PERL__
sub odd_line {
my @list;
my $i;
while (/.*\n/g) {
push(@list, [ $-[0], $+[0] ]) if ++$i % 2;
}
@list;
}
You can use next command to search pattern included in odd number lines.
% greple --inside '&odd_line' pattern files...
MODULE
You can expand the greple command using module. Module files are
placed at App/Greple/
directory in Perl library, and therefor has
App::Greple::module package name.
In the command line, module have to be specified preceding any other options in the form of -Mmodule. However, it also can be specified at the beginning of option expansion.
If the package name is declared properly, __DATA__
section in the
module file will be interpreted same as .greplerc
file content. So
you can declare the module specific options there. Functions declared
in the module can be used from those options, it makes highly
expandable option/programming interaction possible.
Using -M without module argument will print available module list. Option –man will display module document when used with -M option. Use –show option to see the module itself. Option –path will print the path of module file.
See this sample module code. This sample defines options to search from pod, comment and other segment in Perl script. Those capability can be implemented both in function and macro.
package App::Greple::perl;
use Exporter 'import';
our @EXPORT = qw(pod comment podcomment);
our %EXPORT_TAGS = ( );
our @EXPORT_OK = qw();
use App::Greple::Common;
use App::Greple::Regions;
my $pod_re = qr{^=\w+(?s:.*?)(?:\Z|^=cut\s*\n)}m;
my $comment_re = qr{^(?:[ \t]*#.*\n)+}m;
sub pod {
match_regions(pattern => $pod_re);
}
sub comment {
match_regions(pattern => $comment_re);
}
sub podcomment {
match_regions(pattern => qr/$pod_re|$comment_re/);
}
1;
__DATA__
define :comment: ^(\s*#.*\n)+
define :pod: ^=(?s:.*?)(?:\Z|^=cut\s*\n)
#option --pod --inside :pod:
#option --comment --inside :comment:
#option --code --outside :pod:|:comment:
option --pod --inside '&pod'
option --comment --inside '&comment'
option --code --outside '&podcomment'
You can use the module like this:
greple -Mperl --pod default greple
greple -Mperl --colorful --code --comment --pod default greple
If special subroutine initialize()
and finalize()
are defined in
the module, they are called at the beginning with
Getopt::EX::Module object as a first argument. Second argument is
the reference to @ARGV
, and you can modify actual @ARGV
using
it. See App::Greple::find module as an example.
Calling sequence is like this. See Getopt::EX::Module for detail.
1) Call initialize()
2) Call function given in -Mmod::func() style
3) Call finalize()
HISTORY
Most capability of greple is derived from mg command, which has been developing from early 1990’s by the same author. Because modern standard grep family command becomes to have similar capabilities, it is a time to clean up entire functionalities, totally remodel the option interfaces, and change the command name. (2013.11)
SEE ALSO
App::Greple, https://github.com/kaz-utashiro/greple
Getopt::EX, https://github.com/kaz-utashiro/Getopt-EX
AUTHOR
Kazumasa Utashiro
LICENSE
Copyright 1991-2022 Kazumasa Utashiro
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
1.3 - greple -Mdebug
NAME
debug - Greple module for debug control
SYNOPSIS
greple -dmc
greple -Mdebug
greple -Mdebug::on(getoptex)
greple -Mdebug::on=getoptex
DESCRIPTION
Enable debug mode for specified target. Currently, following targets are available.
getoptex Getopt::EX
getopt Getopt::Long
color -dc Color information
directory -dd Change directory information
file -df Show search file names
number -dn Show number of processing files
match -dm Match pattern
option -do Show command option processing
process -dp Exec ps command before exit
stat -ds Show statistic information
grep -dg Show grep internal state
filter -dF Show filter informaiton
unused -du Show unused 1-char option name
When used without function call, default target is enabled; currently
getoptex
and option
.
$ greple -Mdebug
Specify required target with on
function like:
$ greple -Mdebug::on(color,match,option)
$ greple -Mdebug::on=color,match,option
Calling debug::on=all
enables all targets, except unused
and
number
.
Target name marked with -dx
can be enabled in that form. Following
commands are all equivalent.
$ greple -Mdebug::on=color,match,option
$ greple -dc -dm -do
$ greple -dcmo
EXAMPLE
Next command will show how the module option is processed in Getopt::EX module.
$ greple -Mdebug::on=getoptex,option -Mdig something --dig .
1.4 - greple -Mcolors
NAME
colors - Greple module for various colormap
SYNOPSIS
greple -Mcolors
OPTIONS
–light
For light terminal. Same as a default.
–dark
For dark terminal.
–grey24
–grey24-bg
24 level grey scales.
–solarized
–solarized-fg
–solarized-bg
Solarized colors. –solarized is same as –solarized-fg.
–git-color-blame
Colorize git-blame(1) command output.
SEE ALSO
1.5 - greple -Mfind
NAME
find - Greple module to use find command
SYNOPSIS
greple -Mfind find-options – greple-options …
DESCRIPTION
Provide find command option with ending --
.
Greple will invoke find command with provided options and read its output from STDIN, with option –readlist. So
greple -Mfind . -type f -- pattern
is equivalent to:
find . -type f | greple --readlist pattern
If the first argument start with !
, it is taken as a command name
and executed in place of find. You can search git managed files
like this:
greple -Mfind !git ls-files -- pattern
SEE ALSO
1.6 - greple -Mdig
NAME
dig - Greple module for recursive search
SYNOPSIS
greple -Mdig pattern –dig directories …
greple -Mdig pattern –git …
DESCRIPTION
Option –dig searches all files under specified directories. Since it takes all following arguments, place at the end of all options.
It is possible to specify AND condition after directories, in find option format. Next command will search all C source files under the current directory.
$ greple -Mdig pattern --dig . -name *.c
$ greple -Mdig pattern --dig . ( -name *.c -o -name *.h )
If more compilicated file filtering is required, combine with -Mselect module.
You can use –dig option without module declaration by setting it
as autoload module in your ~/.greplerc
.
autoload -Mdig --dig --git
Use –git-r to search submodules recursively.
OPTIONS
–dig directories find-option
Specify at the end of greple options, because all the rest is taken as option for find(1) command.
–git ls-files-option
Search files under git control. Specify at the end of greple options, because all the rest is taken as option for git-ls-files(1) command.
–git-r ls-files-option
Short cut for –git –recurse-submodules.
SEE ALSO
1.7 - greple -Mselect
NAME
select - Greple module to select files
SYNOPSIS
greple -Mdig -Mselect … –dig .
FILENAME
--suffix file suffixes
--select-name regex match for file name
--select-path regex match for file path
--x-suffix exclusive version of --suffix
--x-select-name exclusive version of --select-name
--x-select-path exclusive version of --select-path
DATA
--shebang included in #! line
--select-data regex match for file data
--x-shebang exclusive version of --shebang
--x-select-data exclusive version of --select-data
DESCRIPTION
Greple’s -Mselect module allows to filter files using their name and content data. It is usually supposed to be used along with -Mfind or -Mdig module.
For example, next command scan files end with .pl
and .pm
under
current directory.
greple -Mdig -Mselect --suffix=pl,pm foobar --dig .
This is almost equivalent to the next command using –dig option with extra conditional expression for find command.
greple -Mdig foobar --dig . -name '*.p[lm]'
The problems is that the above command does not search perl command
script without suffixes. Next command looks for both files looking at
#!
(shebang) line.
greple -Mdig -Mselect --suffix=pl,pm --shebang perl foobar --dig .
Generic option –select-name, –select-path and –select-data take regular expression and works for arbitrary use.
ORDER and DEFAULT
Besides normal inclusive rules, there is exclusive rules which start with –x- option name.
As for the order of rules, all exclusive rules are checked first, then inclusive rules are applied.
When no rules are matched, default action is taken. If no inclusive rule exists, it is selected. Otherwise discarded.
OPTIONS
FILENAME
–suffix=xx,yy,zz …
Specify one or more file name suffixes connecting by comma (
,
). They will be converted to/\.(xx|yy|zz)$/
expression and compared to the file name.–select-name=regex
Specify regular expression and it is compared to the file name. Next command search Makefiles under any directory.
greple -Mselect --select-name '^Makefile.*'
–select-path=regex
Specify regular expression and it is compared to the file path.
–x-suffix=xx,yy,zz …
–x-select-name=regex
–x-select-path=regex
These are reverse version of corresponding options. File is not selected when matched.
DATA
–shebang=aa,bb,cc
This option test if a given string is included in the first line of the file start with
#!
(aka shebang) mark. Multiple names can be specified connecting by command (,
). Given string is converted to the next regular expression:/\A #! .*\b (aa|bb|cc)/x
–select-data=regex
Specify regular expression and it is compared to the file content data.
–x-shebang=aa,bb,cc
–x-select-data=regex
These are reverse version of corresponding options. File is not selected when matched.
1.8 - greple -Mxp
NAME
App::Greple::xp - extended pattern module
VERSION
Version 0.04
SYNOPSIS
greple -Mxp
DESCRIPTION
This module provides functions those can be used by greple pattern and region options.
OPTIONS
–le-pattern file
–inside-pattern file
–outside-pattern file
–include-pattern file
–exclude-pattern file
Read file contents and use each lines as a pattern for options.
–le-string file
–inside-string file
–outside-string file
–include-string file
–exclude-string file
Almost same as *-pattern option but each line is concidered as a fixed string rather than regular expression.
COMMENT
You can insert comment lines in pattern file. As for fixed string file, there is no way to write comment.
Lines start with hash mark (#
) is ignored as a comment line.
String after double slash (//
) is also ignored with preceding
spaces.
WILD CARD
Because file parameter is globbed, you can use wild card to give multiple files. If nothing matched to the wild card, this option is simply ignored with no message.
$ greple -Mxp --exclude-pattern '*.exclude' ...
SEE ALSO
https://github.com/kaz-utashiro/greple
https://github.com/kaz-utashiro/greple-xp
AUTHOR
Kazumasa Utashiro
LICENSE
Copyright 2019- Kazumasa Utashiro.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
1.9 - greple -Mgit
NAME
git - Greple git module
SYNOPSIS
greple -Mgit ...
DESCRIPTION
App::Greple::git is a greple module to handle git output.
OPTIONS
–color-blame-line, –color-blame
–color-blame-label
Read git-blame(1) output and apply unique color for each commit id. Option –color-blame and –color-blame-line colorize whole line, while –color-blame-label does only labels.
Set
$HOME/.gitconfig
like this:[pager] blame = greple -Mgit --color-blame-line | env LESSANSIENDCHARS=mK less -cR
ENVIRONMENT
LESS
LESSANSIENDCHARS
Since greple produces ANSI Erase Line terminal sequence, it is convenient to set less command understand them.
LESS=-cR LESSANSIENDCHARS=mK
INSTALL
CPANMINUS
$ cpanm App::Greple::git
SEE ALSO
App::sdif: git diff support
AUTHOR
Kazumasa Utashiro
LICENSE
Copyright 2021-2022 Kazumasa Utashiro.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
1.10 - greple -Mframe
NAME
App::Greple::frame - Greple frame output module
SYNOPSIS
greple -Mframe –frame …
DESCRIPTION
Greple -Mframe module provide a capability to put surrounding frames for each blocks.
top
, middle
and bottom
frames are printed for blocks.
By default –join-blocks option is enabled to collect consecutive lines into a single block. If you don’t like this, override it by –no-join-blocks option.
OPTIONS
–frame
Set frame and fold long lines with frame-friendly prefix string. Folding width is taken from the terminal. Or you can specify the width by calling set function with module option.
–set-frame-width=#
Set frame width. You have to put this option before –frame option. See set function in “FUNCTION” section.
FUNCTION
set(width=n)
Set terminal width to n. Use like this:
greple -Mframe::set(width=80) ... greple -Mframe::set=width=80 ...
If non-digit character is found in the value part, it is considered as a Reverse Polish Notation, starting terminal width pushed on the stack. RPN
2/3-
meansterminal-width / 2 - 3
.You can use like this:
greple -Mframe::set=width=2/3- --frame --uc '(\w+::)+\w+' --git | ansicolumn -PC2
SEE ALSO
AUTHOR
Kazumasa Utashiro
LICENSE
Copyright 2022 Kazumasa Utashiro.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
1.11 - greple -Mupdate
NAME
update - Greple module to update file content
SYNOPSIS
greple -Mupdate
Options:
--update replace file content
--with-backup make backup files
--diff produce diff output
--U# specify unified diff context length
VERSION
Version 0.03
DESCRIPTION
This greple module substitute the target file content by command output. For example, next command replace all words in the file to uppercase.
greple -Mupdate '\w+' --cm 'sub{uc}' --update file
Above is a very simple example but you can implement arbitrarily complex function in conjunction with other various greple options.
You can check how the file will be edited by –diff option.
greple -Mupdate '\w+' --cm 'sub{uc}' --diff file
Command sdif or cdif would be useful to see the difference visually.
greple -Mupdate '\w+' --cm 'sub{uc}' --diff file | cdif
This module has been spun off from App::Greple::subst module. Consult it for more practical use case.
OPTIONS
–update
–update::update
Update the target file by command output. Entire file content is produced and any color effects are canceled. Without this option, greple behaves as normal operation, that means only matched lines are printed.
File is not touched as far as its content does not change.
–with-backup[=suffix]
Backup original file with
.bak
suffix. If optional parameter is given, it is used as a suffix string. If the file exists,.bak_1
,.bak_2
… are used.–diff
–update::diff
Option -diff produce diff output of original and converted text. Option -U# can be used to specify context length.
INSTALL
CPANMINUS
$ cpanm App::Greple::update
GITHUB
$ cpanm https://github.com/kaz-utashiro/greple-update.git
SEE ALSO
App::Greple, https://github.com/kaz-utashiro/greple
App::Greple::update, https://github.com/kaz-utashiro/greple-update
App::Greple::subst, https://github.com/kaz-utashiro/greple-subst
AUTHOR
Kazumasa Utashiro
LICENSE
Copyright 2022 Kazumasa Utashiro.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
1.12 - greple -Msubst
NAME
subst - Greple module for text search and substitution
VERSION
Version 2.3104
SYNOPSIS
greple -Msubst –dict dictionary [ options ]
Dictionary:
--dict dictionary file
--dictdata dictionary data
Check:
--check=[ng,ok,any,outstand,all,none]
--select=N
--linefold
--stat
--with-stat
--stat-style=[default,dict]
--stat-item={match,expect,number,ok,ng,dict}=[0,1]
--subst
--[no-]warn-overlap
--[no-]warn-include
File Update:
--diff
--diffcmd command
--create
--replace
--overwrite
DESCRIPTION
This greple module supports check and substitution of text files based on dictionary data.
Dictionary file is given by –dict option and each line contains matching pattern and expected string pairs.
greple -Msubst --dict DICT
If the dictionary file contains following data:
colou?r color
cent(er|re) center
above command finds the first pattern which does not match the second string, that is “colour” and “centre” in this case.
Field //
in dictionary data is ignored, so this file can be written
like this:
colou?r // color
cent(er|re) // center
You can use same file by greple’s -f option and string after
//
is ignored as a comment in that case.
greple -f DICT ...
Option –dictdata can be used to provide dictionary data in command line.
greple --dictdata $'colou?r color\ncent(er|re) center\n'
Dictionary entry starting with a sharp sign (#
) is a comment and
ignored.
Overlapped pattern
When the matched string is same or shorter than previously matched string by another pattern, it is simply ignored (–no-warn-include by default). So, if you have to declare conflicted patterns, place the longer pattern earlier.
If the matched string overlaps with previously matched string, it is warned (–warn-overlap by default) and ignored.
Terminal color
This version uses Getopt::EX::termcolor module. It sets option –light-screen or –dark-screen depending on the terminal on which the command run, or TERM_BGCOLOR environment variable.
Some terminals (eg: “Apple_Terminal” or “iTerm”) are detected automatically and no action is required. Otherwise set TERM_BGCOLOR environment to #000000 (black) to #FFFFFF (white) digit depending on terminal background color.
OPTIONS
–dict=file
Specify dictionary file.
–dictdata=data
Specify dictionary data by text.
–check=
outstand
|ng
|ok
|any
|all
|none
Option –check takes argument from
ng
,ok
,any
,outstand
,all
andnone
.With default value
outstand
, command will show information about both expected and unexpected words only when unexpected word was found in the same file.With value
ng
, command will show information about unexpected words. With valueok
, you will get information about expected words. Both with valueany
.Value
all
andnone
make sense only when used with –stat option, and display information about never matched pattern.–select=N
Select _N_th entry from the dictionary. Argument is interpreted by Getopt::EX::Numbers module. Range can be defined like –select=
1:3,7:9
. You can get numbers by –stat option.–linefold
If the target data is folded in the middle of text, use –linefold option. It creates regex patterns which matches string spread across lines. Substituted text does not include newline, though. Because it confuses regex behavior somewhat, avoid to use if possible.
–stat
–with-stat
Print statistical information. Works with –check option.
Option –with-stat print statistics after normal output, while –stat print only statistics.
–stat-style=
default
|dict
Using –stat-style=dict option with –stat and –check=any, you can get dictionary style output for your working document.
–stat-item item=[0,1]
Specify which item is shown up in stat information. Default values are:
match=1 expect=1 number=1 ng=1 ok=1 dict=0
If you don’t need to see pattern field, use like this:
--stat-item match=0
Multiple parameters can be set at once:
--stat-item match=number=0,ng=1,ok=1
–subst
Substitute unexpected matched pattern to expected string. Newline character in the matched string is ignored. Pattern without replacement string is not changed.
–[no-]warn-overlap
Warn overlapped pattern. Default on.
–[no-]warn-include
Warn included pattern. Default off.
FILE UPDATE OPTIONS
–diff
–diffcmd=command
Option –diff produce diff output of original and converted text.
Specify diff command name used by –diff option. Default is “diff -u”.
–create
Create new file and write the result. Suffix “.new” is appended to original filename.
–replace
Replace the target file by converted result. Original file is renamed to backup name with “.bak” suffix.
–overwrite
Overwrite the target file by converted result with no backup.
DICTIONARY
This module includes example dictionaries. They are installed share directory and accessed by –exdict option.
greple -Msubst --exdict jtca-katakana-guide-3.dict
–exdict dictionary
Use dictionary flie in the distribution as a dictionary file.
–exdictdir
Show dictionary directory.
–exdict jtca-katakana-guide-3.dict
–jtca-katakana-guide
Created from following guideline document.
外来語(カタカナ)表記ガイドライン 第3版 制定:2015年8月 発行:2015年9月 一般財団法人テクニカルコミュニケーター協会 Japan Technical Communicators Association https://www.jtca.org/standardization/katakana_guide_3_20171222.pdf
–jtca
Customized –jtca-katakana-guide. Original dictionary is automatically generated from published data. This dictionary is customized for practical use.
–exdict jtf-style-guide-3.dict
–jtf-style-guide
Created from following guideline document.
JTF日本語標準スタイルガイド(翻訳用) 第3.0版 2019年8月20日 一般社団法人 日本翻訳連盟(JTF) 翻訳品質委員会 https://www.jtf.jp/jp/style_guide/pdf/jtf_style_guide.pdf
–jtf
Customized –jtf-style-guide. Original dictionary is automatically generated from published data. This dictionary is customized for practical use.
–exdict sccc2.dict
–sccc2
Dictionary used for “C/C++ セキュアコーディング 第2版” published in 2014.
https://www.jpcert.or.jp/securecoding_book_2nd.html
–exdict ms-style-guide.dict
–ms-style-guide
Dictionary generated from Microsoft localization style guide.
https://www.microsoft.com/ja-jp/language/styleguides
Data is generated from this article:
https://www.atmarkit.co.jp/news/200807/25/microsoft.html
–microsoft
Customized –ms-style-guide. Original dictionary is automatically generated from published data. This dictionary is customized for practical use.
Amendment dictionary can be found here. Please raise an issue or send a pull-request if you have request to update.
JAPANESE
This module is originaly made for Japanese text editing support.
KATAKANA
Japanese KATAKANA word have a lot of variants to describe same word, so unification is important but it’s quite tiresome work. In the next example,
イ[エー]ハトー?([ヴブボ]ォ?) // イーハトーヴォ
left pattern matches all following words.
イエハトブ
イーハトヴ
イーハトーヴ
イーハトーヴォ
イーハトーボ
イーハトーブ
This module helps to detect and correct them.
INSTALL
CPANMINUS
$ cpanm App::Greple::subst
SEE ALSO
https://github.com/kaz-utashiro/greple
https://github.com/kaz-utashiro/greple-subst
https://github.com/kaz-utashiro/greple-update
https://www.jtca.org/standardization/katakana_guide_3_20171222.pdf
https://www.jtf.jp/jp/style_guide/styleguide_top.html, https://www.jtf.jp/jp/style_guide/pdf/jtf_style_guide.pdf
https://www.microsoft.com/ja-jp/language/styleguides, https://www.atmarkit.co.jp/news/200807/25/microsoft.html
文化庁 国語施策・日本語教育 国語施策情報 内閣告示・内閣訓令 外来語の表記
https://qiita.com/kaz-utashiro/items/85add653a71a7e01c415
AUTHOR
Kazumasa Utashiro
LICENSE
Copyright 2017-2022 Kazumasa Utashiro.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
1.13 - greple -Msubst-desumasu
NAME
App::Greple::subst::desumasu - Japanese DESU/MASU dictionary for App::Greple::subst
SYNOPSIS
greple -Msubst::desumasu --dearu --subst --all file
greple -Msubst::desumasu --dearu --diff file
greple -Msubst::desumasu --dearu --replace file
DESCRIPTION
greple -Msubst module based on desumasu-converter.
This is a simple checker/converter module for Japanese writing style so called DUSU/MASU (ですます調: 敬体) and DEARU (である調: 常体). This is not my own idea and the dictionary is based on https://github.com/kssfilo/desumasu-converter.
See article https://kanasys.com/tech/722 for detail.
OPTIONS
–dearu
–dearu-n
–dearu-N
Convert DESU/MASU to DEARU style.
DESU (です) and MASU (ます) sometimes followed by NE (ね) in frank situation, and that NE (ね) is removed from converted result by default. Option with -n keep that NE (ね), and option with -N igonore them.
–desumasu
–desumasu-n
–desumasu-N
Convert DEARU to DESU/MASU style.
Use them with greple -Msubst options.
–subst –all –no-color
Print converted text.
–diff
Produce diff output of original and converted text. Use cdif command in App::sdif to visualize the difference.
–create
–replace
–overwrite
To update the file, use these options. Option –create make new file with
.new
suffix. Option –replace update the target file with backup, while option –overwrite does without backup.
See App::Greple::subst for other options.
INSTALL
CPANMINUS
From CPAN:
cpanm App::Greple::subst::desumasu
From GIT repository:
cpanm https://github.com/kaz-utashiro/greple-subst-desumasu.git
SEE ALSO
App::Greple, App::Greple::subst
https://github.com/kssfilo/desumasu-converter, https://kanasys.com/tech/722
AUTHOR
Kazumasa Utashiro
LICENSE
Copyright 2021-2022 Kazumasa Utashiro.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
1.14 - greple -Mtype
NAME
App::Greple::type - file type filter module for greple
SYNOPSIS
greple -Mdig -Mtype --type-xxxx ... --dig .
DESCRIPTION
This module filters search target files by given rule. It is convenient to use with other greple module which support recursive or multi-file search such as -Mfind, -Mdig or -Mgit.
For example, option for Perl is defined as this:
option --type-perl \
--suffix=pl,PL,pm,pod,t,psgi \
--shebang=perl
Using this option, only files those name end with –suffix option
or files which contains string perl
in the first #!
(shebang)
line will be searched.
Option –suffix and –shebang are defined in App::Greple::select module.
SHORT NAME
Calling module as -Mtype::config(short) or -Mtype::config=short introduce short name for rule options. When short name mode is activated, all –type-xxxx options can be used as –xxxx as well.
OPTIONS
option --type-actionscript --suffix=as,mxml
option --type-ada --suffix=ada,adb,ads
option --type-asm --suffix=asm,s
option --type-asp --suffix=asp
option --type-aspx --suffix=master,ascx,asmx,aspx,svc
option --type-batch --suffix=bat,cmd
option --type-cc --suffix=c,h,xs
option --type-cfmx --suffix=cfc,cfm,cfml
option --type-clojure --suffix=clj
option --type-cmake --suffix=cmake --select-name=^CMakeLists.txt$
option --type-coffeescript --suffix=coffee
option --type-cpp --suffix=cpp,cc,cxx,m,hpp,hh,h,hxx,c++,h++
option --type-csharp --suffix=cs
option --type-css --suffix=css
option --type-dart --suffix=dart
option --type-delphi --suffix=pas,int,dfm,nfm,dof,dpk,dproj,groupproj,bdsgroup,bdsproj
option --type-elisp --suffix=el
option --type-elixir --suffix=ex,exs
option --type-erlang --suffix=erl,hrl
option --type-fortran --suffix=f,f77,f90,f95,f03,for,ftn,fpp
option --type-go --suffix=go
option --type-groovy --suffix=groovy,gtmpl,gpp,grunit,gradle
option --type-haskell --suffix=hs,lhs
option --type-hh --suffix=h
option --type-html --suffix=htm,html
option --type-java --suffix=java,properties
option --type-js --suffix=js
option --type-json --suffix=json
option --type-jsp --suffix=jsp,jspx,jhtm,jhtml
option --type-less --suffix=less
option --type-lisp --suffix=lisp,lsp
option --type-lua --suffix=lua --shebng=lua
option --type-markdown --suffix=md
option --type-md --type-markdown
option --type-make --suffix=mak,mk --select-name=^(GNUmakefile|Makefile|makefile)$
option --type-matlab --suffix=m
option --type-objc --suffix=m,h
option --type-objcpp --suffix=mm,h
option --type-ocaml --suffix=ml,mli
option --type-parrot --suffix=pir,pasm,pmc,ops,pod,pg,tg
option --type-perl --suffix=pl,PL,pm,pod,t,psgi --shebang=perl
option --type-perltest --suffix=t
option --type-php --suffix=php,phpt,php3,php4,php5,phtml --shebang=php
option --type-plone --suffix=pt,cpt,metadata,cpy,py
option --type-python --suffix=py --shebang=python
option --type-rake --select-name=^Rakefile$
option --type-rr --suffix=R
option --type-ruby --suffix=rb,rhtml,rjs,rxml,erb,rake,spec \
--select-name=^Rakefile$ --shebang=ruby
option --type-rust --suffix=rs
option --type-sass --suffix=sass,scss
option --type-scala --suffix=scala
option --type-scheme --suffix=scm,ss
option --type-shell --suffix=sh,bash,csh,tcsh,ksh,zsh,fish \
--shebang=sh,bash,csh,tcsh,ksh,zsh,fish
option --type-smalltalk --suffix=st
option --type-sql --suffix=sql,ctl
option --type-tcl --suffix=tcl,itcl,itk
option --type-tex --suffix=tex,cls,sty
option --type-tt --suffix=tt,tt2,ttml
option --type-vb --suffix=bas,cls,frm,ctl,vb,resx
option --type-verilog --suffix=v,vh,sv
option --type-vim --suffix=vim
option --type-xml --suffix=xml,dtd,xsl,xslt,ent --select-data='\A.*<[?]xml'
option --type-yaml --suffix=yaml,yml
BACKGROUND
This module is inspired by App::Gre command, and original matching rule is taken from it.
Filename matching can be done with -Mfind module, but to know file type from its content, different mechanism was required. So I made the –begin function can die to stop the file processing, and introduced new -Mselect module.
SEE ALSO
App::Greple, App::Greple::select
AUTHOR
Kazumasa Utashiro
LICENSE
Copyright 2021-2022 Kazumasa Utashiro.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
1.15 - greple -Mmsdoc
NAME
msdoc - Greple module for access MS office docx/pptx/xlsx documents
VERSION
Version 1.06
SYNOPSIS
greple -Mmsdoc pattern example.docx
DESCRIPTION
This module makes it possible to search string in Microsoft docx/pptx/xlsx file.
Microsoft document consists of multiple files archived in zip format. String information is stored in “word/document.xml”, “ppt/slides/*.xml” or “xl/sharedStrings.xml”. This module extracts these data and replaces the search target.
By default, text part from XML data is extracted. This process is done by very simple method and may include redundant information.
Strings are simply connected into paragraph for .docx and .pptx document. For .xlsx document, single space is inserted between them. Use –separator option to change this behavior.
After every paragraph, single newline is inserted for .pptx and .xlsx file, and double newlines for .docx file. Use –space option to change.
OPTIONS
–dump
Simply print all converted data. Additional pattern can be specified, and they will be highlighted inside whole text.
$ greple -Mmsdoc --dump -e foo -e bar buz.docx
–space=n
Specify number of newlines inserted after every paragraph. Any non-negative integer is allowed including zero.
–separator=string
Specify the separator string placed between each component strings.
–indent
Extract indented XML document, not a plain text.
–indent-mark=string
Set indentation string. Default is
|
.
INSTALL
CPANMINUS
cpanm App::Greple::msdoc
SEE ALSO
App::Greple, https://github.com/kaz-utashiro/greple
App::Greple::msdoc, https://github.com/kaz-utashiro/greple-msdoc
App::optex::textconv, https://github.com/kaz-utashiro/optex-textconv
AUTHOR
Kazumasa Utashiro
LICENSE
Copyright 2018-2022 Kazumasa Utashiro.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
1.16 - greple -Mwordle
NAME
App::Greple::wordle - wordle module for greple
SYNOPSIS
greple -Mwordle
DESCRIPTION
App::Greple::wordle is a greple module which implements wordle game. Correctness is checked by regular expression.
Rule is almost same as the original game but answer is different. Use –compat option to get compatible answer.
OPTIONS
–series=#, -s#
–compat
Choose different series of answer. Default 1. Series zero is same as the original game and option –compat is a short cut for –series=0. If it is not zero, original answer word set is shuffled by pseudo random numbers using series number as an initial seed.
–index=#, -n#
Specify index. Default index is calculated from days from 2021/06/19. If the value is negative and you can get yesterday’s question by giving -1.
Answer for option -s0n0 is
cigar
.–[no-]result
Show result when succeeded. Default true.
–random
Generate random index every time.
–trial=#, -x=#
Set trial count. Default 6.
COMMANDS
Five letter word is processed as an answer. Some other input is taken as a command.
h, hint
List possible words.
u, uniq
List possible words made of unique characters.
=chars
If start with equal (
=
), list words which include all of chars.!chars
If start with exclamation mark (
!
), list words which does not include any of chars.regex
Any other string include non-alphabetical character is taken as a regular expression to filter words.
!!
Get word list produced by the last command execution.
These commands can be connected in series. For example, next command
show possible words start with letter z
.
hint ^z
Next shows all words which does not incude any letter of audio
and
rents
, and made of unique characters.
!audio !rents u
EXAMPLE
1: solid # try word "solid"
2: panic # try word "panic"
3: hint # show hint
3: !solid !panic =eft uniq # search word exclude(solidpanic) include(eft)
3: wheft # try word "wheft"
4: hint # show hint
4: datum # try word "datum"
5: tardy # try word "tardy"
BUGS
Wrong position character is colored yellow always, even if it is colored green in other position.
INSTALL
CPANMINUS
$ cpanm App::Greple::wordle
or
$ curl -sL http://cpanmin.us | perl - App::Greple::wordle
SEE ALSO
App::Greple::wordle, https://github.com/kaz-utashiro/greple-wordle
App::Greple, https://github.com/kaz-utashiro/greple
https://qiita.com/kaz-utashiro/items/ba6696187f2ce902aa39
https://github.com/alex1770/wordle
AUTHOR
Kazumasa Utashiro
LICENSE
Copyright 2022 Kazumasa Utashiro.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
1.17 - greple -Maozora
NAME
aozora - Greple module for aozora-bunko proofreading
SYNOPSIS
greple -Maozora [ options ]
VERSION
Version 0.01
DESCRIPTION
http://www.aozora.gr.jp/KOSAKU/MANUAL_4.html
OCR入力では、平仮名の「へぺべ」と片仮名の「ヘペベ」がしばしば入れ替わりますが、画面やプリントアウトの校正では、この誤りをみつけることは困難です。
けれど、正規表現に対応したエディタを使えば、半角の「[]」で片仮名の「ヘペベ」を囲った
[ヘペベ]
を検索語にして、片仮名の「ヘ」「ペ」「ベ」を、まとめてチェックできます。
正規表現では、通常とは異なり、「[ ]」で挟まれた一つ一つの文字すべてが検索対象として指定されます。
「[青空文庫]」で正規表現の検索を行うと、「青空文庫」という連続した四文字ではなく、「青」「空」「文」「庫」のそれぞれが拾われます。
見つけ出したい文字列のパターンをどう表現するかといった正規表現の詳細は、解説本やインターネットの記述を参考にしてください。対応するエディタについては、インターネットで調べてください。
点検グループで用いている正規表現を、参考までにリストアップしておきます。 青空文庫の校正を進める上で、これらが何をあらわしているかを理解する必要はありません。ただ、簡単なものからでも試してもらえれば、作業の効率と精度をあげるのに役立つはずです。
–katakana-he
片仮名ヘペベをチェックする。
[ヘペベ]
–hiragana-he
平仮名へぺべをチェックする。
(多数ヒットしすぎてチェックしづらいときは、次項の正規表現で、誤って入った平仮名へぺべをチェックしてください。)
[へぺべ]
–check-he
平仮名とカタカナの「へべぺ」「ヘベペ」を両方チェックする。 それぞれ異なる色で表示される。
–suspicious-he
片仮名文字列に接する平仮名へぺべをみて、読み取り誤りをチェックする。
[ァ-ヶー][へぺべ] [へぺべ][ァ-ヶー]
–lonely-katakana
片仮名文字列でない中に、一字混じった片仮名をチェックする。
[^ァ-ヶー][ロエセカニタトリハオ][^ァ-ヶー]
–lonely-non-katakana
片仮名文字列の中に、一字混じった片仮名ではない文字をチェックする。
[ァ-ヶー]+[口工七力二夕卜り一八才][ァ-ヶー]
–kyuji
新字ファイルに混じる旧字をチェックする。
[亞惡壓圍爲醫壹稻飮隱營榮衞驛圓艷鹽奧應歐毆穩假價畫會壞懷繪擴殼覺學嶽樂勸卷歡罐觀關陷巖顏歸氣龜僞戲犧舊據擧峽挾狹堯曉區驅勳徑惠溪經繼莖螢輕鷄藝缺儉劍圈檢權獻縣險顯驗嚴效廣恆鑛號國濟碎齋劑櫻册雜參慘棧蠶贊殘絲齒兒辭濕實舍寫釋壽收從澁獸縱肅處敍奬將燒稱證乘剩壤孃條淨疊穰讓釀囑觸寢愼晉眞盡圖粹醉隨髓數樞聲靜齊攝竊專戰淺潛纖踐錢禪雙壯搜插爭總聰莊裝騷臟藏屬續墮體對帶滯臺瀧擇澤單擔膽團彈斷癡遲晝蟲鑄廳聽敕鎭遞鐵轉點傳黨盜燈當鬪獨讀屆繩貳惱腦霸廢拜賣麥發髮拔蠻祕濱拂佛竝變邊辨辯瓣舖穗寶襃豐沒飜槇萬滿默彌藥譯豫餘與譽搖樣謠遙來亂覽龍兩獵壘勵禮靈齡戀爐勞樓祿亙灣瑤]
–shinji
旧字ファイルに混じる新字をチェックする。
[亜悪圧囲為医壱稲飲隠営栄衛駅円艶塩奥応欧殴穏仮価画会壊懐絵拡殻覚学岳楽勧巻歓缶観関陥巌顔帰気亀偽戯犠旧拠挙峡挟狭尭暁区駆勲径恵渓経継茎蛍軽鶏芸欠倹剣圏検権献県険顕験厳効広恒鉱号国済砕斎剤桜冊雑参惨桟蚕賛残糸歯児辞湿実舎写釈寿収従渋獣縦粛処叙奨将焼称証乗剰壌嬢条浄畳穣譲醸嘱触寝慎晋真尽図粋酔随髄数枢声静斉摂窃専戦浅潜繊践銭禅双壮捜挿争総聡荘装騒臓蔵属続堕体対帯滞台滝択沢単担胆団弾断痴遅昼虫鋳庁聴勅鎮逓鉄転点伝党盗灯当闘独読届縄弐悩脳覇廃拝売麦発髪抜蛮秘浜払仏並変辺弁弁弁舗穂宝褒豊没翻槙万満黙弥薬訳予余与誉揺様謡遥来乱覧竜両猟塁励礼霊齢恋炉労楼禄亘湾瑶]
–kogaki
仮名を小書きしないファイルに紛れ込んだ、小書きをチェックする。
(物を数える際や地名などに用いる「ヶ」は、外してあります。)
[ぁぃぅぇぉっゃゅょゎァィゥェォッャュョヮヵ]
–lonely-alpha
全角とするべき可能性の高い、一文字の半角アルファベットをチェックする。
[^a-zA-Z][a-zA-Z][^a-zA-Z]
–dot-in-middle
半角の「.」の後に、半角のアキ(「 」)なしで文字が続くものをチェックする。
\.([^ ()p」])
–space-at-eol
文末に、不要な空白(全角、半角)が入っていないかをチェックする。
[ ]+$
–suspicious-nl
空白もしくは括弧以外が文頭にきているものをみて、誤って入れられた改行をチェックする。
(底本の行あきをなぞるために入れた改行も、チェックされます。)
^[^ 「[(『]
–suspicious-space
行頭の括弧の前に、青空文庫では入れないことにしている空白がないかをチェックする。
^ [(「『]
–ruby-1
ルビの中に、仮名以外がないかをチェックする。
《[^《》]*?[^あ-んァ-ヶーゞゝヽヾ・/″\][^《》]*?》
–ruby-2
ルビの文字数に対して、ルビの付く側の文字数が長めのものをみて、「|」の入れ忘れをチェックする。
[\x{3400}-\x{9fff}\x{f900}-\x{fa2d}々]{3,}《
–ruby-3
ルビの付く文字が連続するものをみて、過分割をチェックする。
《[^》]+》[^ァ-ヶーあ-ん、。?!―,『』|「」々]+《[^》]+》
–ruby-4
ルビ中の拗促音が小書きされていないものをチェックする。(正しく並みで使われているものもチェックされます。)
《[^《》]*?[つやゆよヤヨツユ][^《》]*?》
–ruby
–ruby-[1-4] を全部チェックする。
–suspicious-brace
誤入力の可能性の高い、半角の丸括弧「()」と角括弧「[]」をチェックする。
[^U][\?\!\#-\&\(-\+\<-\>\[-\]|]
–rare-chars
使われることのまれな文字をチェックする。
[′.・,‥-「♯□」、]
–susupicious-ocr
OCRの読み取りミスや誤入力が生じやすい文字をチェックする。
(以下で用いられている「|」は、検索語の区切りです。全体をコピーし、検索ウインドウにペーストします。ヒットしたものに誤りの可能性を感じたら、底本を確認してください。)
米殻|奴隸|釆女|喝釆|壷|壼|会杜|溌刺|撒去|撤布|慰籍|狼籍|酒落|曖味|瞹昧|瞹味|咋日|[気天]侯|王候|鍛治屋|掃って|帰く|因難|粛条|芸著|建薬物|表規|絵仕|猟人形|緒口|野緒|熊々|煮趣|粗の|基だ|挟まれる|立流|繁う|愚かれた|遂われる|借しく|料埋|士地|紳土|弁護土|揚所|抜露|披る|披璃|緑結び|熱柿|探夜|族行|丁推|連蜂|藤椅子|間題|振柚|限鏡|博突|乾焼|春く|海昔|撤[かきくけこ]|茄[だで]|吐潟|裟婆|呷[かきくけこ]|崇[らりるれろ]|且那|梶棒|灰暗い|瑞ぐ|沢庵潰|、辷|咄嵯|相母|きれいだた|失わたい|すまたい|状能|卯何|実似|別在|駑いた|広緑|任掛|族客|迫ひ|荼|陀俤|笶|失はり|失張|迫掛け|誥|仲聞|出違|歳の幕|警傭|対時|意気軒昴|薪手|口借し|遠反|閣魔|趣昧|貴任|崇《たた》|崇《たたり》|紆介|理寮|代日|丁日|大統頷|愛橋|天主闇|埋窟|埋屈|要頷|一骰|輿行|夕碁|哂|肓|遺《や》|柤|聨|惨澹|高梁|衿持|千渉|大低|束京|咋今|咋秋|孟蘭盆|昼問|影讐|神泌|象微|徴動|欺《か》く|遺る|軋礫|粟鼠|驕桿|戦々競々|鉄葉|愛矯|覩察|遣遥|兼葭|堂字|鄭接|天鉄羅|霊揚|奢移|お皺|清洲国|横械|横会|記情|面も|両も|兄い|年棒|逐に|吾響|件[はひふへほ]|大きた|瞬問|塞さに|タ陽|娼帰|一入|日く|精桿|沓《よう》として|沓として|沓渺|沓茫|※[#小書き平仮名わ|一杖|欺き|夫嫁|憤れ|眠が|悴|報く|別投|絞い|普投|灯寵|韜晦|俳譜|嘲る|擬と|例巧|疳癪|倦も|咳私|一過間|逮廻し|時聞|風間|教本|二と|覚東|衛道|姻突|後喬|共処|鱧|夏に|おるす|摘ん|駁諭|弩窿|追億|読計|臓腋|冒演|冒漬|乾操|出鱈日|移しい|灰燈|散術|限石|塵挨|夫死|髪髴|距雌|七首|任様|棒給|前蝕|タメ急|看護嫁|暮口|績り|備われ|[^相]違[さしすせそ]|達[わいうえ]|覚倍|両《しか》|判延|遠いあ|探[いくけき]|幕史|無隈|端侃|依沽地|摩《なび》|旅騎兵|蕉村|なちば|冩《うつ》|外冠|追る|摩詞不思議|愚かれ|膝顕|什《たお》れ|幕史|廷喜|於で|視神|蝦蝮|実李|鳴咽|──|凡帳面|鐡道|訊間|兔状|艮|末練|一暼|錬倉|郡屋|指輸|手祈|事惰|落書き|亊|擧[校生]|桑かい|嗚[っつ]た|反封|膀手|俤間|伺うし|金昆羅|喇叺|取倣|剌[さしすせそ]|世事|、、|。。|。、|、。|!、|[氣気]特|夾|[反訪]間|此虞|画し|竸|末だ|眥|鳴呼|叮噂|柳か|変た|朦瀧|杯《など》|新開|楝瓦|[へれね]ぱ|譯山|衿り|活撥|[まで]しよう|間達|達う|迫々|咋年|なけれは|含は|人[らりるれろ]|入間|酉洋|相達|あさらか|方画|那蘇|字音|項戴|食掌|肯像|潜越|黙心|畢竜|停立|比際|洒が|任所|木當|亭圭|浩身|母現|牀|速れ|缺鮎|傅説|[畫書]飯|[畫書]食|淡自|目分|白分|白己|感清|人地|差ず|隣れ|評到|宥《よ[いひ]》|都曾|周園|王義|卑狸|減多|腸[っつわるり]|卿筒|有舞う|木当|入家|ようた|かたり|いろいろた|静かた|勿諭|撤《ま》|一且|の問で|侯補|曖簾|[曖瞹]か|一ばい|茸|踉《つ》|真申|丸大|暖炉|燈龍|灯龍|大尺|アメリ力|宜伝|心待ちがい|等[いむまみめ]|伊大利|等敬|欺息|共時|弟一|寵[っるもられりろ]|児る|混度|漸う|巳《や》|代去|灰白い|几て|止当|瓢々|練返|徂合|酪酊|酩酎|酪酎|緑家|凛|埓|出偶|けれは|ならは|すれは|贔屑|脾睨|縞麗|逹|迴|完壁|下句|白身|眺躍
SEE ALSO
http://www.aozora.gr.jp/KOSAKU/MANUAL_4.html
https://qiita.com/kaz-utashiro/items/2f199409bdb1e08dc473
AUTHOR
Kazumasa Utashiro
LICENSE AND COPYRIGHT
Copyright 2014-2019 Kazumasa Utashiro
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
1.18 - greple -Mcm
NAME
App::Greple::cm - Greple module to load colormap file
SYNOPSIS
greple -Mcm --load-colormap ...
DESCRIPTION
App::Greple::cm is …
AUTHOR
Kazumasa Utashiro
LICENSE
Copyright 2020 Kazumasa Utashiro.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
1.19 - greple -Mdaemon3
NAME
daemon3 - Module for translation of the book “The Design and Implementation of the FreeBSD Operating System”
VERSION
Version 1.01
SYNOPSIS
greple -Mdaemon3 [ options ]
--by <part> makes <part> as a data record
--in <part> search from <part> section
--jp print Japanese chunk
--eg print English chunk
--egjp print Japanese/English chunk
--comment print comment block
--injp search from Japanese text
--ineg search from English text
--inej search from English/Japanese text
--retrieve retrieve given part in plain text
--colorcode show each part in color-coded
DESCRIPTION
Text is devided into forllowing parts.
e English text
j Japanese text
eg English text and comment
jp Japanese text and comment
macro Common roff macro
retain Retained original text
comment Comment block
com1 Level 1 comment
com2 Level 2 comment
com3 Level 3 comment
mark .EG, .JP, .EJ mark lines
gap empty line between English and Japanese
So [ macro ] + [ e ] recovers original text, and [ macro ] + [ j ] produces Japanese version of book text. You can do it by next command.
$ greple -Mdaemon3 --retrieve macro,e
$ greple -Mdaemon3 --retrieve macro,j
OPTION
–by part
Makes part as a unit of output. Multiple part can be given connected by commma.
–in part
Search pattern only from specified part.
–roffsafe
Exclude pattern included in roff comment and index.
–retrieve part
Retrieve specified part as a plain text.
Special word all means macro, mark, e, j, comment, retain, gap. Next command produces original text.
greple -Mdaemon3 --retrieve all
If the part start with minus (’-’) character, it is removed from specification. Without positive specification, all is assumed. So next command print all lines other than retain part.
greple -Mdaemon3 --retrieve -retain
–colorcode
Produce color-coded result.
EXAMPLE
Produce original text.
$ greple -Mdaemon3 --retrieve macro,e
Search sequence of “system call” in Japanese text and print egjp part including them. Note that this print lines even if “system” and “call” is devided by newline.
$ greple -Mdaemon3 -e "system call" --by egjp --in j
Seach English text block which include all of “socket”, “system”, “call”, “error” and print egjp block including them.
$ greple -Mdaemo3 "socket system call error" --by egjp --in e
Look the file conents each part colored in different color.
$ greple -Mdaemon3 --colorcode
Look the colored contents with all other staff
$ greple -Mdaemon3 --colorcode --all
Compare produced result to original file.
$ diff -U-1 <(lv file) <(greple -Mdaemon3 --retrieve macro,j) | sdif
TEXT FORMAT
Pattern 1
Simple Translation
.\" Copyright 2004 M. K. McKusick
.Dt $Date: 2013/12/23 09:04:26 $
.Vs $Revision: 1.3 $
.EG \"---------------------------------------- ENGLISH
.H 2 "\*(Fb Facilities and the Kernel"
.JP \"---------------------------------------- JAPANESE
.H 2 "\*(Fb の機能とカーネルの役割"
.EJ \"---------------------------------------- END
Pattern 2
Sentence-by-sentence Translation
.PP
.EG \"---------------------------------------- ENGLISH
The \*(Fb kernel provides four basic facilities:
processes,
a filesystem,
communications, and
system startup.
This section outlines where each of these four basic services
is described in this book.
.JP \"---------------------------------------- JAPANESE
The \*(Fb kernel provides four basic facilities:
processes,
a filesystem,
communications, and
system startup.
\*(Fb カーネルは、プロセス、ファイルシステム、通信、
システムの起動という4つの基本サービスを提供する。
This section outlines where each of these four basic services
is described in this book.
本節では、これら4つの基本サービスが本書の中のどこで扱われるかを解説する。
.EJ \"---------------------------------------- END
COMMENT
Block start with ※ (kome-mark) character is comment block.
.JP \"---------------------------------------- JAPANESE
The
.GL kernel
is the part of the system that runs in protected mode and mediates
access by all user programs to the underlying hardware (e.g.,
.Sm CPU ,
keyboard, monitor, disks, network links)
and software constructs
(e.g., filesystem, network protocols).
.GL カーネル
は、システムの一部として特権モードで動作し、
すべてのユーザプログラムがハードウェア (\c
.Sm CPU 、
モニタ、ディスク、ネットワーク接続等) や、ソフトウェア資源
(ファイルシステム、ネットワークプロトコル等)
にアクセスするための調停を行う。
※
protected mode は、ここでしか使われていないため、
protection mode と誤解されないために特権モードと訳すことにする。
INSTALL
cpanm App::Greple::daemon3
LICENSE
Copyright (C) Kazumasa Utashiro.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
SEE ALSO
https://github.com/kaz-utashiro/greple-daemon3
AUTHOR
Kazumasa Utashiro
POD ERRORS
Hey! The above document had some coding errors, which are explained below:
Around line 132:
Non-ASCII character seen before =encoding in ‘の機能とカーネルの役割"’. Assuming UTF-8
1.20 - greple -Mical
NAME
ical - Module to support Apple macOS Calendar data
SYNOPSIS
greple -Mical [ options ]
--simple print data in on line
--detail print one line data with descrition if available
Exported functions
&print_ical_simple
&print_ical_desc
&print_ical_detail
SAMPLES
greple -Mical [ -dnf ] …
greple -Mical –simple …
greple -Mical –detail …
greple -Mical –all –print print_desc …
DESCRIPTION
Used without options, it will search all macOS Calendar files under user’s home directory.
With –simple option, summarize content in single line. Output is not sorted.
With –detail option, print summarized line with description data if it is attached. The result is sorted.
Sample:
BEGIN:VEVENT
UID:19970901T130000Z-123401@host.com
DTSTAMP:19970901T1300Z
DTSTART:19970903T163000Z
DTEND:19970903T190000Z
SUMMARY:Annual Employee Review
CLASS:PRIVATE
CATEGORIES:BUSINESS,HUMAN RESOURCES
END:VEVENT
TIPS
Use -dfn
option to observe the command running status.
Use -ds
option to see statistics information such as how many files
were searched.
SEE ALSO
RFC2445
AUTHOR
Kazumasa Utashiro
LICENSE
Copyright 2017-2022 Kazumasa Utashiro.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
1.21 - greple -Mjq
NAME
greple -Mjq - greple module to search JSON data with jq
SYNOPSIS
greple -Mjq –glob JSON-DATA –IN label pattern
VERSION
Version 0.05
DESCRIPTION
This is an experimental module for App::Greple to search JSON formatted text using jq(1) as a backend.
Search top level json object which includes both Marvin
and
Zaphod
somewhere in its text representation.
greple -Mjq 'Marvin Zaphod'
You can search object .commit.author.name
includes Marvin
like this:
greple -Mjq --IN .commit.author.name Marvin
Search first name
field including Marvin
under .commit
:
greple -Mjq --IN .commit..name Marvin
Search any author.name
field including Marvin
:
greple -Mjq --IN author.name Marvin
Search name
is Marvin
and type
is Robot
or Android
:
greple -Mjq --IN name Marvin --IN type 'Robot|Android'
Please be aware that this is just a text matching tool for indented
result of jq(1) command. So, for example, .commit.author
includes everything under it and it matches committer
field name.
Use jq(1) filter for more complex and precise operation.
CAUTION
greple(1) commands read entire input before processing. So it should not be used for gigantic data or infinite stream.
INSTALL
CPANMINUS
$ cpanm App::Greple::jq
OPTIONS
–IN label pattern
Search pattern included in label field.
Character
%
can be used as a wildcard in label string. So%name
matches labels end withname
, andname%
matches labels start withname
.If the label is simple string like
name
, it matches any level of JSON data.If the label string contains period (
.
), it is considered as a nested labels. Name.name
matches onlyname
label at the top level. Nameprocess.name
matches onlyname
entry of someprocess
hash.If labels are separated by two or more dots (
..
), they don’t have to have direct relationship.–NOT label pattern
Specify negative condition.
–MUST label pattern
Specify required condition. If there is one or more required condition, all other positive rules move to optional. They are not required but highlighted if exist.
LABEL SYNTAX
.file
file
at the top level..file.path
path
under.file
..file..path
path
in descendants of.file
.path
path
at any level.file.path
file.path
at any level.file..path
Some
path
in descendants of somefile
.%path
Any labels end with
path
.path%
Any labels start with
path
.%path%
Any labels include
path
.
EXAMPLES
Search from any name
labels.
greple -Mjq --IN name _mina
Search from .process.name
label.
greple -Mjq --IN .process.name _mina
Object .process.name
contains _mina
and .event
contains
EXEC
.
greple -Mjq --IN .process.name _mina --IN .event EXEC
Object ppid
is 803 and .event
contains FORK
or EXEC
.
greple -Mjq --IN ppid 803 --IN event 'FORK|EXEC'
Object name
is _mina
and .event
contains CREATE
.
greple -Mjq --IN name _mina --IN event 'CREATE'
Object ancestors
contains 1132
and .event
contains EXEC
with arguments
highlighted.
greple -Mjq --IN ancestors 1132 --IN event EXEC --IN arguments .
Object *pid
label contains 803.
greple -Mjq --IN %pid 803
Object any <path> contains _mina
under .file
and .event
contains WRITE
.
greple -Mjq --IN .file..path _mina --IN .event WRITE
TIPS
Use
--all
option to show entire data.Use
--nocolor
option or setNO_COLOR=1
to disable colored output.Use
-o
option to show only matched part.Use
--blockend=
option to cancel showing block separator.Since this module implements original search function, greple(1) -i does not take effect. Set modifier in regex like
(?i)pattern
if you want case-insensitive match.Use
-Mjq::set=debug
to see actual regex.Use
-Mjq::set=noif
if you don’t have to use jq as an input filter. Data have to be well-formatted in that case.Use
--color=always
and setLESSANSIENDCHARS=mK
if you want to see the output using less(1). Put next line in your~/.greplerc
to enable colored output always.option default --color=always
SEE ALSO
App::Greple, https://github.com/kaz-utashiro/greple
https://stedolan.github.io/jq/
AUTHOR
Kazumasa Utashiro
LICENSE
Copyright 2022 Kazumasa Utashiro
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
1.22 - greple -Mmecab
NAME
mecab - Greple module to produce result by mecab
VERSION
Version 0.01
SYNOPSIS
greple -Mmecab
DESCRIPTION
Work in progress.
SEE ALSO
AUTHOR
Kazumasa Utashiro
LICENSE
Copyright 2019- Kazumasa Utashiro.
These commands and libraries are free software; you can redistribute it and/or modify it under the same terms as Perl itself.
1.23 - greple -Mppi
NAME
ppi - Greple module to use Perl PPI module
SYNOPSIS
greple -Mppi
VERSION
Version 0.01
DESCRIPTION
Greple module to use Perl PPI module. This is just a work in progress experimental module.
OPTIONS
–dumper
Dump PDOM trees produced by PPI::Dumper module.
–cdumper
Colorize dumped data.
–top
Make search data block top level object.
–state
Make search data block PPI::Statement.
–prune=type
Remove type object from tree. This option have to be used before any other options.
AUTHOR
Kazumasa Utashiro
LICENSE
Copyright 2022 Kazumasa Utashiro
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
1.24 - greple -Mpw
NAME
pw - Module to get password from file
SYNOPSIS
greple -Mpw pattern file
VERSION
0.01
DESCRIPTION
This module searches id and password information those written in text file, and displays them interactively. Passwords are not shown on display by default, but you can copy them into clipboard by specifying item mark.
PGP encrypted file can be handled by greple standard feature. Command “gpg” is invoked for files with “.gpg” suffix by default. Option –pgp is also available, then you can type passphrase only once for searching from multiple files. Consult –if option if you are using other encryption style.
Terminal scroll buffer and screen is cleared when command exits, and content of clipboard is replaced by prepared string, so that important information does not remain on the terminal.
Id and password is collected from text using some keywords like “user”, “account”, “password”, “pin” and so on. To see actual data, use pw_status function described below.
Some bank use random number matrix as a countermeasure for tapping. If the module successfully guessed the matrix area, it blackout the table and remember them.
| A B C D E F G H I J
--+--------------------
0 | Y W 0 B 8 P 4 C Z H
1 | M 0 6 I K U C 8 6 Z
2 | 7 N R E Y 1 9 3 G 5
3 | 7 F A X 9 B D Y O A
4 | S D 2 2 Q V J 5 4 T
Enter the field position to get the cell items like:
> E3 I0 C4
and you will get the answer:
9 Z 2
Case is ignored and white space is not necessary, so you can type like this as well:
> e3i0c4
INTERFACE
pw_print
Data print function. This function is set for –print option of greple by default, and user doesn’t have to care about it.
pw_epilogue
Epilogue function. This function is set for –end option of greple by default, and user doesn’t have to care about it.
pw_option
Several parameters can be set by pw_option function. If you do not want to clear screen after command execution, call pw_option like:
greple -Mpw::pw_option(clear_screen=0)
or:
greple -Mpw --begin pw_option(clear_screen=0)
with appropriate quotation.
Currently following options are available:
clear_clipboard clear_string clear_screen clear_buffer goto_home browser timeout parse_matrix parse_id parse_pw id_keys id_chars id_color id_label_color pw_keys pw_chars pw_color pw_label_color pw_blackout debug
Password is not blacked out when pw_blackout is 0. If it is 1, all password characters are replaced by ‘x’. If it is greater than 1, password is replaced by sequence of ‘x’ indicated by that number.
id_keys and pw_keys are list, and list members are separated by whitespaces. When the value start with ‘+’ mark, it is appended to current list.
pw_status
Print option status. Next command displays defaults.
greple -Mpw::pw_status= dummy /dev/null
SEE ALSO
https://github.com/kaz-utashiro/greple-pw
AUTHOR
Kazumasa Utashiro
LICENSE
Copyright (C) 2017-2020 Kazumasa Utashiro.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
1.25 - greple -Msccc2
NAME
sccc2 - Greple module for Secure Coding in C and C++ (2nd Edition)
VERSION
Version 2.02
INSTALL
cpanm で git リポジトリを指定:
cpanm git@github.com:JPCERTCC/greple-sccc2.git
cpanm https://github.com/JPCERTCC/greple-sccc2.git
または、clone して
cpanm .
SYNOPSIS
greple -Msccc2 [ options ]
OPTION
ファイル
環境変数 $SCCC2DIR
を設定すれば、以下のオプションで第一版、第二版の
原稿を検索できる。
--ed1 search 1st edition
--ed2 search 2nd edition
検索対象
--in <part> search in <part>
(jp, eg, jptxt, egtxt, comment, figure, table)
表示範囲
--by <part> display by <part>
(jp, eg, jptxt, egtxt, comment, figure, table)
--jp display Japanese block
--jptxt display Japanese text block
--eg display English block
--egtxt display English text block
--egjp display jp/eg combined block
除外範囲
--extable exclude table
--exfigure exclude figure
--exexample exclude example
--excomment exclude comment
--join-block join block into single line
用語検査
--wordcheck check against the dictionary
--wordcheck --stat show statistics only
--wordcheck --with-stat print with statistics
表示
--com show all comments
--com1 show comment level 1
--com2 show comment level 2
--com3 show comment level 3
--com2+ show comment level 2 or <
--retrieve retrieve given part in plain text
--colorcode show each part in color-coded
--oldcite old style 2digit citation
--newcite new style 4digit citation
DOCUMENT FORMAT
Society's increased dependency on networked software systems has been
matched by an increase in the number of attacks aimed at these
systems.
社会がネットワーク化したソフトウェアシステムへの依存を深めるにつれ、こ
れらのシステムを狙った攻撃の数は増加の一途を辿っています。
※ comment level 1
※※ comment level 2
※※※ comment level 3
DESCRIPTION
Text is devided into forllowing parts.
egtxt English text
jptxt Japanese text
eg English text and comment
jp Japanese text and comment
comment Comment block
gap empty line between English and Japanese
egtxt と jptxt を取り出せば英語版と日本語版の原稿になる。
$ greple -Msccc2 --retrieve egtxt
$ greple -Msccc2 --retrieve jptxt
EXAMPLE
用語チェック
次のコマンドでテキスト全体の用語チェックができる:
greple -Msccc2 --wordcheck --ed2
修正点を見る:
greple -Msccc2 --wordcheck --ed2 --diff | cdif (あるいは sdif)
「偽装」と対応する原語を表示する
「偽装」含む行を表示する。
greple -Msccc2 --ed2 偽装
--egjp
を付けると対訳部分を表示する。
greple -Msccc2 --egjp --ed2 偽装
「偽装」を -r
で必須パターンとすると、他の検索パターンはオプショナルになる。
それぞれは別の色で表示される。
greple -Msccc2 --egjp --ed2 \
-r 偽装 \
-e 'spoof\w*' -e 'craft\w+' -e 'disguis\w+' -e 'subterfug\w+' -e 'redirect\w*'
パターンをまとめてもいいが、一つのパターンにマッチする文字列は同じ色で表示される。
--uc
(--uniqcolor
) を指定しすれば文字列毎に違う色が割当てられる。
greple -Msccc2 --egjp --ed2 --uc \
-r 偽装 \
--re '(?i:subterfug|disguis|craft|fake|spoof|redirect)\w*'
-e
(--and
) の代わりに -v
(--not
) を指定すると、いずれの単
語も含まれない部分だけが表示される。
greple -Msccc2 --egjp --ed2 \
-r 偽装 \
-v 'spoof\w*' -v 'craft\w+' -v 'disguis\w+' -v 'subterfug\w+' -v 'redirect\w*'
AUTHOR
Kazumasa Utashiro
LICENSE
Copyright 2014,2020 Kazumasa Utashiro
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
2 - Tutorial
Qiita の 2021 アドベントカレンダーに書いた Greple チュートリアルです。
2.1 - はじめに
greple
greple は、作者が1990年初頭から開発を続けているコマンドです。名前の通り Unix コマンドの grep と同じように、テキストから文字列を検索するためのものです。le は lexical expression の意味ですが、それについてはこれからおいおい説明していきます。
読み方
読み方は特に決めていませんが、なんとなく「ぐれっぷる」と呼んでいます。アクセントは「れ」にあったり、今時の平板化した読み方だったりと、人によって様々です。英語的に発音すると「ぐれぽー」みたいになるんでしょうか。
「ぐれっぷ・えるいー」と読んでもいいけど、長いですね。アメリカ人、なんでも単語化しようとしますしね。SCSI が「すかじー」になるくらいで。昔、PCMCIA を無理やり読んでいる人がいて驚いたことがあります。
特徴
greple は、以下のような特徴を持っています。
- 複数のキーワードを指定することができる
- 行だけではなく、検索対象ブロックを柔軟に指定することができる
- 検索対象の範囲や条件を指定することができる
- 複数行に渡るパターンを検索することができる
- 多彩な色指定が可能
- 設定ファイルやモジュールによるカスタマイズ/拡張が可能
- 検索だけではなく、文字列処理のフレームワークとして機能する
これらの特徴や使い方について、これから毎日少しずつ解説していこうと思います。
想定される用途
元々、文章の校正作業を楽にするために作ったもので、その後もまとまった翻訳の仕事が入る度に少しずつ拡張を続けてきました。そのため、それほど多くないテキストファイル(せいぜい書籍数冊分程度)に対して、様々な条件を科して検索やそれに類する処理を行うというのが想定される主な使い方です。
大量のデータを対象にするのであれば、それに適したツールが他にたくさんあるので、そういうものを使ってください。ちなみに、ripgrep というコマンドが高速かつ高機能です。
また、greple は、すべてのデータを読み込んでから処理するため、連続して生成されるデータを逐次処理していくような目的に使うことはできません。
インストール
greple は Perl で記述されていて、cpanminus (cpanm) を使ってインストールします。cpanminus の使い方や、Perl の環境設定については、他の記事を参照してください。cpan や cpanplus など、他のインストーラーでも正しく使えば問題ないはずですが、保証はできません。
$ cpanm App::Greple
or
$ curl -sL http://cpanmin.us | perl - App::Greple
リポジトリ
ソースコードは github で管理しています。cpan にも、ほぼ最新版が上がっているので、どちらからインストールしても同じです。cpanm
コマンドで、github の URL を指定してインストールすることもできます。
関連記事
2.2 - 単純な検索
greple の最も単純な使い方は grep と同じです。
greple pattern file
ファイルが指定されなければ、標準入力を対象にします。
grep と共通のオプションもあります。以下に挙げたものは、grep とほぼ同様の意味を持ちますが、挙動が若干違うものもあります。
-n
行番号を表示する-i
: 大文字小文字を無視する-A
,-B
,-C
: 前後の行を表示する-o
: マッチした部分だけを表示する-h
,-H
: ファイル名の表示を制御する-l
: マッチしたファイル名のみを表示する-c
: マッチした数を表示する-m
: 表示数を制御する
正規表現
greple は、特別なオプションを指定しなくても、パターンに Perl の正規表現をそのまま使うことができます。どのバージョンの正規表現が使えるかは、実行する処理系に依存します。
正規表現は、行単位ではなくファイル全体を対象としてマッチしますが、デフォルトで複数行マッチが有効になっていて、行頭・行末を示す ^
・$
はすべての行にマッチします。
Perl 発祥の正規表現は PCRE (Perl Compatible Regular Expression) として、様々な言語やツールに組み込まれています。ripgrep の場合は、–pcre2 オプションをつけることで利用可能です。
実行例
次のようなファイルを用意してみました。某寿司チェーンのメニューを参考に少しアレンジしましたが、この程度ならまさか盗用とは言われないでしょう。このファイルに対して greple コマンドを実行してみます。
サービスセット 熟成まぐろ えび たまご いか サーモン いなり ねぎまぐろ サラダ
お子様セット 熟成まぐろ えび たまご いなり ツナサラダ
特上セット 中とろ 熟成まぐろ 熟成真鯛 はまち 赤えび ほたて うなぎ かに いくら
特上極旨セット 中とろ はまち かに いくら 赤えび サーモン うなぎ うに
うどんセット 熟成まぐろ サーモン たまご えび うどん
サラダ軍艦セット えびマヨ ツナサラダ サラダ シーフードサラダ
人気セット 熟成まぐろ 漬けまぐろ サーモン びんちょう いか えび えびアボカド たまご
まぐろづくしセット 中とろ 熟成まぐろ ねぎまぐろ
プレミアムセット 中とろ まぐろ サーモン 赤えび はまち うなぎ かに いくら
cat -n するとこうです。
greple
行番号を表示する -n オプションをつけて greple を実行してみます。
greple は、このように、デフォルトで検索した文字列を色付きでハイライトして表示します。最近の検索系コマンドは大概カラー出力に対応しているので珍しくはありません。
grep
grep は、–color オプションをつけることでカラー出力ができます。
ripgrep
ripgrep は、デフォルトでハイライト出力します。行番号やファイル名には異なる色がつきます。
端末以外の出力
これらのコマンドは、出力が端末でない場合には、色付き出力の機能を無効にします。パイプにも着色して出力するためには --color=always
オプションを指定します。--color
オプションの仕様は、どのコマンドもほぼ共通で、値として never
, auto
, always
を取ります。ただ、grep はデフォルトが never
、greple と ripgrep は auto
という違いがあります。
まとめ
単純な文字列を指定した検索について説明しました。もっとも、このような使い方であれば greple を使う必要はまったくないので別のツールを使った方がいいでしょう。実行速度に関しては、インタプリタ型の greple はかなり不利ですが、一般的な使い方であれば実用上問題になるようなことはありません。
SEE ALSO
2.3 - 複数キーワードによる検索
パターンオプション
greple には、パターンを指定するために以下のようなオプションがあります。オプション指定がない場合には、最初のコマンド引数がパターンとされ、これは -le
オプションで指定されたのと同様に扱われます。
PATTERN
pattern 'and +must -not ?alternative &function'
-x, --le pattern lexical expression (same as bare pattern)
-e, --and pattern pattern match across line boundary
-r, --must pattern pattern cannot be compromised
-v, --not pattern pattern not to be matched
--or pattern alternative pattern group
--re pattern regular expression
--fe pattern fixed expression
--le
オプションによる複数キーワード指定
--le
は lexical expression の略としていて greple の le もここから来ています。単なる文字列ではなくて、意味を持ったトークンの集まりというような意味合いですが、英語として通じるか実はちょっと不安です。とりあえず、ブラウザの検索欄と同じように考えてもらえばいいと思います。
検索エンジンの検索欄に空白で区切って複数の単語を並べると、そのすべての文字列にマッチするページを表示しようとしてくれます。greple の --le
オプションも同様で、空白で区切られた検索トークンのすべてが含まれる行を表示します。
前回の寿司セットを例にとってみます。
サービスセット 熟成まぐろ えび たまご いか サーモン いなり ねぎまぐろ サラダ
お子様セット 熟成まぐろ えび たまご いなり ツナサラダ
特上セット 中とろ 熟成まぐろ 熟成真鯛 はまち 赤えび ほたて うなぎ かに いくら
特上極旨セット 中とろ はまち かに いくら 赤えび サーモン うなぎ うに
うどんセット 熟成まぐろ サーモン たまご えび うどん
サラダ軍艦セット えびマヨ ツナサラダ サラダ シーフードサラダ
人気セット 熟成まぐろ 漬けまぐろ サーモン びんちょう いか えび えびアボカド たまご
まぐろづくしセット 中とろ 熟成まぐろ ねぎまぐろ
プレミアムセット 中とろ まぐろ サーモン 赤えび はまち うなぎ かに いくら
前回はまぐろが入っているセットを検索しましたが、やっぱりはまちも食べたいので、まぐろとはまちが入っているセットを探します。
2つのセットがあることがわかります。
ほたても食べたいと探すと、特上セット一択になりました。
このように、greple のパターンに空白で区切って複数のキーワードを指定すると、その全てを含む行を表示します。
grep の場合
grep も -e
オプションを繰り返し使用することで、複数のキーワードを指定することができます。同じように、まぐろとはまちを探してみます。
grep の場合は、複数のパターンを指定すると、そのいずれかが含まれる行を表示します。
では、まぐろとはまちの両方が入っているセットを検索するためにはどうするかというと、まず思いつくのはパイプでつなぐことです。
あるいは、まぐろとはまちの両方が含まれるパターンを指定する方法もあります。
しかし、これでははまちの方が先に出てきた場合に対応することができません。どちらにも対応しようとするとこうなります。
この例では grep ではなく egrep を使っています。grep で同じ結果を得るためには |
を \|
に置き換えます。
見てわかるように、まぐろとはまち以外の部分もマッチしてしまうので、その部分に色をつけるためには、最後にもう一度 egrep を通します。
これで grep でも、greple と同じ出力を得ることができました。ただ、これ以上キーワードが増えていくとちょっと大変なので、やはりパイプを使った方がよさそうです。
両方を含む行にマッチする正規表現
順序に関係なくまぐろとはまちの両方を含む行にマッチさせる正規表現を作ることも可能です。
正規表現の先読み (look-ahead) の機能を使って、(?=.*まぐろ)
と (?=.*はまち)
の両方の条件を満たす行頭 (^
) を探しています。ripgrep 標準の正規表現は先読みをサポートしていないので --pcre2
オプションを使っています。
幅のない行頭にしかマッチしていないので、まぐろとはまちはカラー出力されません。この ^
はなくても同じ結果になりますが、正規表現エンジンの作りによっては非効率な動作になる可能性があるので、あった方がいいでしょう。
マルチカラーハイライト
3種類の寿司ネタを探した例で、それぞれのネタが違う色で表示されていたことに気が付いたでしょうか。
greple はこのように複数のキーワードが指定された場合、それぞれにマッチする部分を異なる色でハイライトして表示します。色に関するオプションについては、いずれ詳しく説明します。
まとめ
greple で複数のキーワードを指定して、そのすべてが含まれる行を出力する方法について説明しました。
まとまった文章の執筆や校正をしていると、場所は覚えていないけど、どこかにあったはずの説明を参照したくなることがあります。「TCP の再送とタイムアウトについて、どこに書いてあったっけ」というような状況です。この例であれば ‘TCP 再送 タイムアウト’ という3つのキーワードを含む部分を探せばいいわけです。
1行の中にすべてのキーワードが含まれているとは限らないじゃないか、と思われるでしょうか。その通りです。そのような場合についても、いずれ考えてみましょう。
SEE ALSO
2.4 - 複数キーワード検索 OR
前回は、指定した複数のキーワードをすべて含む行を表示する方法について説明しました。今回は、A か B のどちらかが含まれればいいという条件を指定する方法について紹介します。
--le
オプションの ?
指定
前回と同じ寿司セットの検索を考えます。
サービスセット 熟成まぐろ えび たまご いか サーモン いなり ねぎまぐろ サラダ
お子様セット 熟成まぐろ えび たまご いなり ツナサラダ
特上セット 中とろ 熟成まぐろ 熟成真鯛 はまち 赤えび ほたて うなぎ かに いくら
特上極旨セット 中とろ はまち かに いくら 赤えび サーモン うなぎ うに
うどんセット 熟成まぐろ サーモン たまご えび うどん
サラダ軍艦セット えびマヨ ツナサラダ サラダ シーフードサラダ
人気セット 熟成まぐろ 漬けまぐろ サーモン びんちょう いか えび えびアボカド たまご
まぐろづくしセット 中とろ 熟成まぐろ ねぎまぐろ
プレミアムセット 中とろ まぐろ サーモン 赤えび はまち うなぎ かに いくら
前回はまぐろとはまちの両方を含むセットを探しましたが、はまちが入っているセットは少ないので選択肢が限られます。妥協して、代わりにえびでもいいことにしましょう。
このような場合は、次のようにキーワードの先頭に ?
マークを指定します。
greple -n 'まぐろ ?はまち ?えび' sushi.txt
だいぶ選択肢が増えました。
複数の --le
オプションを使う
選択肢が増えて気が大きくなってきたので、上の条件に加えて、うにかいくらも食べたくなりました。
greple -n 'まぐろ ?はまち ?えび ?うに ?いくら' sushi.txt
こうすると、はまち、えび、うに、いくら、のどれかが入っていればいいということになってしまって、期待した結果が得られません。
このような場合は --le
を複数回指定します。
greple -n --le 'まぐろ ?はまち ?えび' --le '?うに ?いくら' sushi.txt
OR キーワードは、それぞれの --le
オプション内でグループ化され、オプション同士は AND で評価されるため、全体としては次のような意味になります。
まぐろ AND (はまち OR えび) AND (うに OR いくら)
結果は、それぞれの検索条件毎に色分けされて、見やすく表示されています。
上の例で、最初の --le
を省略することはできません。greple は、パターンオプションが指定されていない時のみ、最初の引数をパターンと解釈するので、この --le
を省略すると次の引数はファイル名として扱われてしまいます。
正規表現を使う
上の例は、正規表現の |
記法を使って次のように書いても、まったく同じ意味になります。実は ?
記号がついたキーワードについては、内部的にこのような表現に変換して評価しています。
greple -n --le 'まぐろ はまち|えび' --le 'うに|いくら' sushi.txt
さらには、この例では ?
を使っていないので --le
オプションは1つにまとめてしまっても問題ないし、その場合は省略することもできます。
greple -n 'まぐろ はまち|えび うに|いくら' sushi.txt
慣れている人には、この方がわかりやすいかもしれません。
まとめ
--le
オプションに複数キーワードを指定する際に、OR 条件を指定する方法を紹介しました。
grep を使って同じことをやろうとすれば、やはりパイプを使うことになると思います。
grep -n まぐろ sushi.txt | egrep 'はまち|えび' | egrep 'うに|いくら
このくらいになってくると、少し greple を使うメリットが感じられてくるでしょうか。条件が複雑になってくると、条件毎に色分けしてくれるのがありがたく感じられる局面も増えてきます。
SEE ALSO
2.5 - 複数キーワード検索 NOT
これまでに、複数キーワードを使った AND と OR の組み合わせを紹介しました。AND、OR とくれば、当然次は NOT です。
実行例
また寿司セットを使って考えてみます。
サービスセット 熟成まぐろ えび たまご いか サーモン いなり ねぎまぐろ サラダ
お子様セット 熟成まぐろ えび たまご いなり ツナサラダ
特上セット 中とろ 熟成まぐろ 熟成真鯛 はまち 赤えび ほたて うなぎ かに いくら
特上極旨セット 中とろ はまち かに いくら 赤えび サーモン うなぎ うに
うどんセット 熟成まぐろ サーモン たまご えび うどん
サラダ軍艦セット えびマヨ ツナサラダ サラダ シーフードサラダ
人気セット 熟成まぐろ 漬けまぐろ サーモン びんちょう いか えび えびアボカド たまご
まぐろづくしセット 中とろ 熟成まぐろ ねぎまぐろ
プレミアムセット 中とろ まぐろ サーモン 赤えび はまち うなぎ かに いくら
まぐろは必ず食べたくて、はまちかえびのどちらかが入ったセットは、次のようにして探すことができました。
greple -n 'まぐろ はまち|えび' sushi.txt
でも、実はいくらはアレルギーで食べられないことを思い出しました。
greple -n 'まぐろ はまち|えび いくら' sushi.txt
とすれば、候補の中からいくらを含む食べられないセットを表示することができます。
でも、本当に探したいのはいくらを含まないセットです。そのような場合には、やはり検索エンジンと同じように、キーワードの先頭にマイナス記号 -
を付けます。
greple -n 'まぐろ はまち|えび -いくら' sushi.txt
これで、望みの結果を得ることができました。
grep -v との違い
grep の -v
オプションは、全体から指定したパターンを含まない行を選び出すためのものです。それに対して、greple の NOT キーワードは、他のキーワードによって選ばれた選択肢から、指定したパターンを含むものを削除するためのものです。ですから NOT キーワードだけのパターンはエラーになります。
grep と同様に動作して欲しければ、どの行にも必ずマッチするパターン、たとえば行頭にマッチする ^
を指定することで実現は可能です。ただ、そのために greple を使う必要性は、あまり感じられません。
まとめ
今回は、複数キーワードを使った検索について AND と OR に続いて NOT の使い方を紹介しました。使い方は簡単で、検索エンジンと同じように、除外したいパターンの先頭に -
を付けるだけです。
寿司セットの例は、数が少ないので目grepでも望む情報は得られますが、もし数千行の情報から選び出すとすれば、ツールの力を借りた方が得策です。AND、OR、NOT を組み合わせて情報を絞り込んでいくことで、望みの結果に近づくことができます。
SEE ALSO
2.6 - 複数キーワード検索 MUST
これまでに、複数キーワードを使った検索について AND、OR、NOT と紹介してきました。
今回は、ちょっと変わった MUST について紹介します。
注目したいキーワード
前回は、以下のような寿司セットの中から、まぐろとはまちかえびを含んでいて、いくらが入っていないものを探すという例を考えました。
サービスセット 熟成まぐろ えび たまご いか サーモン いなり ねぎまぐろ サラダ
お子様セット 熟成まぐろ えび たまご いなり ツナサラダ
特上セット 中とろ 熟成まぐろ 熟成真鯛 はまち 赤えび ほたて うなぎ かに いくら
特上極旨セット 中とろ はまち かに いくら 赤えび サーモン うなぎ うに
うどんセット 熟成まぐろ サーモン たまご えび うどん
サラダ軍艦セット えびマヨ ツナサラダ サラダ シーフードサラダ
人気セット 熟成まぐろ 漬けまぐろ サーモン びんちょう いか えび えびアボカド たまご
まぐろづくしセット 中とろ 熟成まぐろ ねぎまぐろ
プレミアムセット 中とろ まぐろ サーモン 赤えび はまち うなぎ かに いくら
そして、条件を満たすものは、次のようにして得られました。
greple -n 'まぐろ はまち|えび -いくら' sushi.txt
しかし、どのセットにいくらが入っていたかにも興味がないでしょうか。いくら以外はいい内容だったら、誰かと交換してもいいかもしれません。もちろん -いくら
の指定を外せばいいのですが、そうするとどこにいくらがあるのかを自分の目で探さなければなりません。
このように「なければならない」「あってはいけない」だけではなく、「注目したい」ワードを指定したいことがあります。このような時に使えるのが MUST キーワードです。
+
による MUST 指定
これもまた検索エンジンと同様に、キーワードの先頭にプラス記号 +
を付けることで実現します。実際、検索エンジンに複数のキーワードを与えた時に、すべてのキーワードが含まれてはいない結果が表示されることがあります。その時に、絶対にあってほしいキーワードに対して +
をつけて検索します。
先の例であれば、このように使います。
greple -n '+まぐろ +はまち|えび いくら' sushi.txt
MUST キーワードがあると、記号なしのキーワードはオプショナルに格下げされます。つまり、いくらはあってもなくても構わないが、あれば色付きで表示されます。
これも、ハイライトカラーが複数あることの効果が感じられる例です。試しに、単色での表示にしてみると、こんな風になります。
まとめ
今回は MUST キーワードについて説明しましたが、それによって新たに導入されるのは「オプショナルキーワード」であると言うこともできます。
最後に、以前の記事で紹介した実際の使用例を載せておきます。これは compromise という単語がどういう日本語に翻訳されているかを調べた時のものです。compromise のみを必須として、候補となる訳語を着色して表示しています。いずれ、詳しく説明できるかもしれません。
SEE ALSO
2.7 - 複数キーワード検索 単独オプション
これまで --le
オプションを使って、複数のキーワードを指定する方法について紹介してきました。キーワードの前に記号を付けることで AND, OR, NOT, MUST の意味を持たせることができました。
単独オプション
実は --le
オプションで扱うそれぞれのキーワードは、個別の単独オプションで指定することもできます。一覧にすると、こうです。
記号 | 意味 | 単独オプション |
---|---|---|
なし | AND or OPTIONAL | -e, –and |
- | NOT | -v, –not |
? | OR | –or |
+ | MUST | -r, –must |
このため、以下のコマンドは、どれも同じ意味になります。
greple 'まぐろ ?はまち ?えび -いくら' sushi.txt
greple 'まぐろ はまち|えび -いくら' sushi.txt
greple -e まぐろ --or はまち --or えび -v いくら sushi.txt
greple -e まぐろ -e 'はまち|えび' -v いくら sushi.txt
機能的に違いはないので、どれでも使いやすい方法で指定すればいいと思います。--or
オプションに関しては、単独オプションで指定したものがグループ化されます。
絞り込み検索
単独オプションが便利なのは、次々にオプションを追加して、選択肢を絞り込んでいくような場合です。シェルのヒストリー機能を使って直前のコマンドを呼び出して、そのまま最後に追加していくことができます。
特に -v
オプションは検索ワードを指定するオプションとしては解釈されないので、裸の(オプション指定なしの)検索ワードがあっても、最後に追加するだけで検索結果を絞り込むことが可能です。オプション引数とそれ以外の引数は、このように混在させることができます。
greple -n まぐろ sushi.txt
greple -n まぐろ sushi.txt -v いくら
greple -n まぐろ sushi.txt -v いくら -v いか
greple -n まぐろ sushi.txt -v いくら -v いか -v サーモン
実行してみるとこのような結果になります。
ちょうど、パイプで grep -v
コマンドをつないでいくのと同じ要領で絞り込んでいくことができます。
毎回まぐろの色が変わってしまっているのは実装上の都合です。この結果を見て、修正した方がいいのではないかと感じていますが、この場合むしろ見やすくなっていいですね。そのうち直します。
-v
オプションにも正規表現を使えるので、最後のコマンドは次のように書いても同じです。
greple -n まぐろ sushi.txt -v 'いくら|うに|たまご'
まとめ
複数キーワードの AND, OR, NOT, MUST 指定を単独のオプションで行う方法について紹介しました。--le
オプションとどちらが適しているかは、好みや状況によって変わってくると思います。
grep 系のコマンドを使う状況では、最初に出た結果が多すぎて、どうやって絞り込んでいくか試行錯誤するようなことがよくあります。この場合、上で紹介した -v
オプションの使い方は有効です。
オプションを最後にまとめて、こんな風に使うやり方もいいかもしれません。この場合は、パターンが最初の引数ではないのでオプション指定が必要ですが、--le
の代わりに -x
を使うこともできます。
greple sushi.txt -x 'まぐろ -いくら -いか -サーモン'
SEE ALSO
https://qiita.com/advent-calendar/2021/greple
- https://qiita.com/kaz-utashiro/items/5b6bcbe54891b3bd9db5
- https://qiita.com/kaz-utashiro/items/eb8c7067e6de34842fe3
- https://qiita.com/kaz-utashiro/items/165e744d4250adedc4c1
- https://qiita.com/kaz-utashiro/items/439e6abcecf36c520703
- https://qiita.com/kaz-utashiro/items/24ac0b8fdd30b598e069
- https://qiita.com/kaz-utashiro/items/a1ba4e3d07cf37dc25e3
2.8 - 検索範囲指定 --inside/--outside
これはgrepleチュートリアルの8日目です。これまでに、複数キーワードを使って、様々なロジックで対象データから文字列を検索する方法を紹介しました。今回は、検索する対象の範囲を指定する方法について紹介します。
検索する場所を選びたい
今まで、寿司セットから好みのネタを探す方法について考えてきました。
しかし、一点どうも気に入らない部分があります。おわかりでしょうか。寿司ネタを検索しているのに、「まぐろづくしセット」というセット名がマッチしてしまっている点です。
それくらい別にいいじゃないかというかもしれませんが、まぐろづくしだからいいものの、これがもしまぐろ嫌いセットだったらどうでしょうか。まぐろ好きは激おこです。
このように、検索対象データの内容は一様ではないのに、一般の検索コマンドはそれを無視して全体を検索対象にしてしまいます。解決するためにはデータベースを使うとか、専用の検索コマンドを用意するなどの必要があります。
greple による検索範囲指定
greple には、検索範囲を指定する機能があります。まず、寿司セット名範囲を表すパターンを考えてみましょう。セット名部分は ^\S+
という正規表現でマッチすることができます。行頭から始まる空白以外の文字の連続という意味です。まずは、そのパターンを検索してみます。
うまく動いているようです。次に、この部分を --inside
オプションとして指定して、まぐろを検索します。
greple --inside '^\S+' まぐろ sushi.txt
意図した通りに、セット名に含まれるまぐろだけが検索されました。
でも、求めているのはセット名に含まれないまぐろです。ですから、今度は --inside
ではなくて --outside
オプションとして指定します。
greple -n --outside '^\S+' まぐろ sushi.txt
これで、セット名以外の範囲にあるまぐろだけがハイライトされるようになりました。もちろん、セット名以外の部分にまぐろが含まれなければ、その行は表示されません。
繰り返し指定
--inside
と --outside
は、繰り返し指定することができます。その場合、それぞれの範囲の和集合が検索対象範囲となります。
--inside
と --outside
で同じ範囲を指定するとどうなるかというと、ある領域とその補集合の和になるので、当然全体になります。
greple --inside '^\S+' --outside '^\S+' まぐろ sushi.txt
このように、何も指定しないのと同じ部分がマッチしていますが、寿司セット名とそれ以外の部分で色が変わっています。greple は、複数の検索範囲が指定され、検索ワードが1つしかなかった場合、検索範囲毎にハイライトカラーを変えて表示します。
まとめ
--inside
と --outside
オプションにパターンを指定することで、指定した領域の内側、あるいは外側だけに検索対象を限定する方法について紹介しました。
指定するパターンは、いくらでも複雑化することができます。たとえば、次のようにすれば、C のソースコードのコメント部分のみを対象にして検索することができます。/*
で始まって */
で終わる最小範囲を指定しています。
greple --inside '(?s)/\*.*?\*/' pattern ...
次の例は、メールの Subject 行と本文のみを対象にして検索しています。今回のチュートリアルの中では正規表現そのものについて詳しく説明はしません。ちょっとトリッキーな表現になっているので、どうして動作するのか考えてみてください。
greple --inside '\A(.+\n)*\KSubject:.+' --outside '\A(?s:.*?)\n\n'
SEE ALSO
https://qiita.com/advent-calendar/2021/greple
- https://qiita.com/kaz-utashiro/items/5b6bcbe54891b3bd9db5
- https://qiita.com/kaz-utashiro/items/eb8c7067e6de34842fe3
- https://qiita.com/kaz-utashiro/items/165e744d4250adedc4c1
- https://qiita.com/kaz-utashiro/items/439e6abcecf36c520703
- https://qiita.com/kaz-utashiro/items/24ac0b8fdd30b598e069
- https://qiita.com/kaz-utashiro/items/a1ba4e3d07cf37dc25e3
- https://qiita.com/kaz-utashiro/items/0c8c944c17a72724b771
2.9 - 検索範囲指定 --include/--exclude
これはgrepleチュートリアルの9日目です。前回は --inside
と --outside
オプションを使って、検索する対象の範囲を指定する方法について紹介しました。今回は、その続きで --include
と --exclude
オプションについて紹介します。
例外条件を指定したい
--inside
と --outside
は、全体の中から、どこを検索対象にするかという観点で使いました。それに対して --include
と --exclude
は、どこを検索対象から外すかという観点で使います。
--outside
オプションを使って、寿司ネタ領域からまぐろを検索しています。
でも、まぐろは好物だけど、ヅケはあまり好きではなくて、ネギも勘弁して欲しいなあということで、漬けまぐろとねぎまぐろを除外したいと思いました。
この場合 --exclude
オプションに除外したい文字列を与えることで、それに対するマッチを抑制することができます。
greple -n --outside '^\S+' まぐろ sushi.txt --exclude 漬けまぐろ --exclude ねぎまぐろ
ヅケとネギはハイライトされなくなりました。でも、選択肢は変わりませんでした。
文字列には正規表現を使用することができるので --exclude '(漬け|ねぎ)まぐろ'
と書くこともできます。加えて、お子様セットも年齢制限があるので除外しましょう。この場合は単なる文字列ではなく、「お子様セット」ではじまる行全体を指定したいので ^お子様セット.*
と指定します。
sh greple -n --outside '^\S+' まぐろ sushi.txt --exclude '(漬け|ねぎ)まぐろ' --exclude '^お子様セット.*'
お子様セットは候補に出てこなくなりました。
–inside/–outside と –include/–exclude の使い分け
流れとしては --inside/--outside
で検索対象範囲を拾い出して、--include/--exclude
で絞り込むという使い方になります。
上の例で、--exclude
の代わりに --outside
を使うことはできません。前回書いたように --inside
と --outside
で指定した範囲は和集合として扱われるので、結果として全体に戻ってしまいます。
この例では、--outside
を一度しか使っていないので、そこを --exclude
にしても結果は変わりません。単独で使う場合は、どちらを使っても同じ結果になります。
まとめ
前回の --inside/outside
に加えて --include/exclude
オプションについて紹介しました。
TeX や roff など、マークアップを使った文書ファイルを校正しようとする場合、マークアップ命令やコメント部分は対象から外して、純粋に文章の部分だけを処理する必要があります。
また、原稿の中には自分で書いた文章以外に、引用文であったり、別の文書や書籍のタイトル、固有名詞など、ローカルなルールを適用すべきではない部分もあります。それらを例外として処理対象から外すことができないと実用的なツールとはなりません。greple の機能は、そのような経験から進化してきました。
greple のオプションは、なるべく grep に合わせるようにしているのですが、--include
と --excude
については、まったく違う意味になるので注意してください。
SEE ALSO
https://qiita.com/advent-calendar/2021/greple
- https://qiita.com/kaz-utashiro/items/5b6bcbe54891b3bd9db5
- https://qiita.com/kaz-utashiro/items/eb8c7067e6de34842fe3
- https://qiita.com/kaz-utashiro/items/165e744d4250adedc4c1
- https://qiita.com/kaz-utashiro/items/439e6abcecf36c520703
- https://qiita.com/kaz-utashiro/items/24ac0b8fdd30b598e069
- https://qiita.com/kaz-utashiro/items/a1ba4e3d07cf37dc25e3
- https://qiita.com/kaz-utashiro/items/0c8c944c17a72724b771
- https://qiita.com/kaz-utashiro/items/8783c2fd0cc4315b9a3d
2.10 - 検索ブロック指定
一般的に、grep 系のツールが検索の対象とするのは「行」です。しかし、扱いたい情報の単位は行とは限りません。greple は、検索対象とするデータの単位を柔軟に指定することができます。
–all
--all
は、ファイル全体を検索単位とするオプションです。
このオプションが有効なのは、当たり前ですが、ファイルの内容を全部見たい場合です。
寿司セットの中には魚介類以外のネタとして「サラダ」「アボカド」「うどん」があって、すべてを AND で指定すると、そのすべてを含むセットはありませんが --all
を指定すれば全体を眺めることができます。
対象が原稿であれば、注目する部分をハイライトして、全体を眺めたいような場合に有効です。
ファイル名のみを表示する -l
オプションと組み合わせて使用すると、指定したワードをすべて含むファイルを探すことができます。検索エンジンと同じような使い方です。
-p, –paragraph
-p
(--paragraph
) オプションを指定すると、行ではなく、空白行で区切られたパラグラフを単位として検索を行います。複数キーワードの処理も、行ではなくパラグラフ単位で行われるので、指定したワードは同一行に含まれる必要はありません。
次の例では AND, OR, NOT, MUST という単語がすべて含まれるパラグラフを検索しています。
デフォルトでは、ファイル名は grep と同様にすべての行に表示されますが、--filestyle=once
というオプションを指定することで、最初に一度だけ表示しています。
–block
--block
オプションでパターンを指定すると、それにマッチする部分が検索単位となります。次の例では git のソースコードから、Cのコメント部分 (/*...*/
) をブロックとして指定して UTF, Unicode, BOM という単語がすべて含まれるブロックを検索しています。
greple --border '(?s)/\*.*?\*/' --fs=once -i 'utf unicode bom' *.h
この場合、検索対象のブロックは連続していません。ブロックに相当しない場所でのマッチは無視されます。
–border
--border
オプションには --block
と同じようにパターンを指定して、それを元にブロックを作りますが、--block
と違い、マッチの始点と終点を境界とする連続したブロックを作ります。
デフォルトの行単位での挙動は --border=^
を指定したのと同じで、パラグラフモードは連続する改行文字 --border='\n\n+'
を指定したのと同じです1。
次の例は、シャープ (#) で始まる行の行頭にブロック境界を設定します。つまり、# ではじまる行から次の行頭の # までの、マークダウン形式のパラグラフがブロックとなります。
greple --border '^(?=#)' PCRE *.md --fs=once
-A, -B, -C
-A (after), -B (before), -C (context) オプションは、検索行の前後を同時に表示するためのもので、grep とほぼ同様に使うことができます。次の例は、マッチした前後1行ずつを表示しています。
greple -n -e びんちょう sushi.txt -C1
この前後コンテキストも、指定したブロックに対して適用されます。ですから、パラグラフモードであれば、前後のパラグラフを一緒に表示します2。
この時、検索ロジックもコンテキスト同様に拡張されます。次の例で、最初のコマンドではびんちょうとサラダを両方含むセットは見つかりませんが、次のコマンドは -C1
でコンテキストを前後に1行ずつ拡張することで、条件を満たすデータが発見されます。
greple -n -e びんちょう -e サラダ sushi.txt
greple -n -e びんちょう -e サラダ sushi.txt -C1
まとめ
greple の検索単位を指定するブロック機能について紹介しました。テキストファイルであっても、行単位で情報が管理されていることはむしろ稀です。処理対象の情報単位を柔軟に設定することで、論理的に意味のあるデータブロックに対して検索等の操作をすることが可能になります。
今回紹介した単純なパターンマッチによる方法では処理できないような複雑なデータ形式もあります。そのような場合には、また別のアプローチがあります。
SEE ALSO
https://qiita.com/advent-calendar/2021/greple
- https://qiita.com/kaz-utashiro/items/5b6bcbe54891b3bd9db5
- https://qiita.com/kaz-utashiro/items/eb8c7067e6de34842fe3
- https://qiita.com/kaz-utashiro/items/165e744d4250adedc4c1
- https://qiita.com/kaz-utashiro/items/439e6abcecf36c520703
- https://qiita.com/kaz-utashiro/items/24ac0b8fdd30b598e069
- https://qiita.com/kaz-utashiro/items/a1ba4e3d07cf37dc25e3
- https://qiita.com/kaz-utashiro/items/0c8c944c17a72724b771
- https://qiita.com/kaz-utashiro/items/8783c2fd0cc4315b9a3d
- https://qiita.com/kaz-utashiro/items/84f5a6be6bf996076c64
2.11 - 拡張モジュールの使用法
greple の特徴の一つは、モジュールによって拡張可能なことです。今回は、モジュールの使い方について紹介します。
利用可能なモジュールを表示する
モジュールは -M
オプションで指定します。単独で使用すると、利用できるモジュールを表示します。この中には、greple に標準で含まれるものと、別パッケージからインストールしたもの、プライベートに作成したものが含まれます。
$ greple -M
Use -M option at the beginning with module name.
Available modules:
dig
find
line
perl
pgp
select
subst
autocolor
i18n
termcolor
greple の拡張モジュールは、App::Greple
の下にある小文字ではじまる Perl モジュールです。大文字ではじまるものは greple そのものを実装するためのものです。
greple の拡張モジュール機能は Getopt::EX
モジュールで実現されており、App::Greple
に加えて Getopt::EX
の下にある汎用のモジュールを使うこともできます。i18n
, termcolor
などは Getopt::EX
のモジュールです。
モジュールを呼び出す
-M
オプションにモジュール名を指定することで、そのモジュールを呼び出すことができます。次の例は App::Greple::find
モジュールを使うことを指示していますが、greple の拡張モジュールとして使用する場合には、単に -Mfind
と指示します。
greple -Mfind
マニュアルを表示する
モジュールオプションに続いて --help
オプションを指定することで、そのモジュールのマニュアルを表示することができます。
$ greple -Mfind --man
NAME
find - Greple module to use find command
SYNOPSIS
greple -Mfind find-options -- greple-options ...
DESCRIPTION
Provide find command option with ending '--'.
Greple will invoke find command with provided options and read its output
from STDIN, with option --readlist. So
greple -Mfind . -type f -- pattern
is equivalent to:
find . -type f | greple --readlist pattern
If the first argument start with `!', it is taken as a command name and
executed in place of find. You can search git managed files like this:
greple -Mfind !git ls-files -- pattern
ただ、実装上の都合で、別のモジュールのマニュアルが表示されてしまうことがあります。その場合は、man
コマンドや perldoc
で App::Greple
に続いてモジュール名を指定します。
man App::Greple::find
perldoc App::Greple::find
ちなみに、--man
の代わりに --show
を使うと、モジュールの内容を見ることができます。perldoc -m
としても同じです。
モジュールを使う
モジュールの使い方について find
と dig
モジュールを例に説明します。
greple には grep -r
のようにディレクトリの下を再起的に検索するオプションはありません。その代わりに --readist
というオプションがあって、標準入力から読んだファイル名を検索対象とします。find コマンドの出力を --readlist
オプションで読み込めば再起的な検索が可能です。
find . -type f | greple --readlist Copyright
find
モジュールを使うと、greple コマンドのオプションとして find オプションを指定することができるようになります。-Mfind
から --
までの間の引数を find
コマンドのオプションとして処理します。
greple -Mfind . -type f -- Copyright
find コマンドのオプションは非常に詳細な指定が可能ですが、使いこなすのにはちょっとコツが必要です。たとえば .git ディレクトリを検索対象から外すためには次のように指定します。あまり直感的ではなくて、-prune
オプションの使い方はいつもわからなくなります。
find . -name .git -prune -o -type f
git だけではなく様々なケースに対応しようとすると、オプションはさらに複雑化してきます。dig
モジュールは find
モジュールの面倒なオプションを自動的に設定して、必要のないリポジトリや画像ファイルなどをスキップするためのモジュールです。次のように使うことができます。
greple -Mdig Copyright --dig .
このコマンドは、実際には次のようなオプションに展開されて実行されます。
greple -Mfind .
( ( ( -name .git -o -name .svn -o -name RCS -o -name CVS ) -o ( -name .vscode ) -o ( -name .build -o -name _build ) ) -prune -o -type f )
! -name .* ! -name *,v ! ( -name *~ -o -name *.swp )
! ( -iname *.jpg -o -iname *.jpeg -o -iname *.gif -o -iname *.png -o -iname *.ico -o -iname *.heic -o -iname *.heif -o -iname *.svg )
! ( -iname *.tar -o -iname *.tar.gz -o -iname *.tbz -o -iname *.tgz -o -name *.a -o -name *.zip )
! -iname *.pdf ! ( -name *.db -o -iname *.bdb )
! ( -name *.bundle -o -name *.dylib -o -name *.o -o -name *.fits )
-print --
これを手で入力することを考えると気が遠くなりそうです。
dig
モジュールには --git
というオプションも定義されていて、git ls-files
の結果に対して検索をかけることができます。
まとめ
今回は greple のモジュール機能について、その使い方を紹介しました。greple には、これまで紹介していないオプションが数多くありますが、モジュールを使うことで、複雑なオプション指定を組み合わせてシンプルなインタフェースを提供することが可能になります。モジュールの作り方については、機会を改めて説明します。
SEE ALSO
https://qiita.com/advent-calendar/2021/greple
- https://qiita.com/kaz-utashiro/items/5b6bcbe54891b3bd9db5
- https://qiita.com/kaz-utashiro/items/eb8c7067e6de34842fe3
- https://qiita.com/kaz-utashiro/items/165e744d4250adedc4c1
- https://qiita.com/kaz-utashiro/items/439e6abcecf36c520703
- https://qiita.com/kaz-utashiro/items/24ac0b8fdd30b598e069
- https://qiita.com/kaz-utashiro/items/a1ba4e3d07cf37dc25e3
- https://qiita.com/kaz-utashiro/items/0c8c944c17a72724b771
- https://qiita.com/kaz-utashiro/items/8783c2fd0cc4315b9a3d
- https://qiita.com/kaz-utashiro/items/84f5a6be6bf996076c64
- https://qiita.com/kaz-utashiro/items/ebc7ea99f800cfc8c90c
2.12 - 初期設定ファイル .greplerc
greple は、UNIX の他の多くのコマンドと同様に、ホームページに初期設定ファイル (~/.greplerc) を用意することで、起動時にそれを読み込むことができます。この中では、様々な設定が可能です。
default: デフォルトオプション
option
命令で新しいオプションを定義することができます。中でも default
という名前は特別で、実行時に自動的に付与するオプションを指定することができます。たとえば、次のように設定すると、ハイライトモードが常に有効になります。
option default --color=always
さらに、行番号を表示させたければ、このようにします。default の定義が複数行あった場合には、最後の定義だけが有効になります。
option default --color=always -n
option: オリジナルオプション
自分でオリジナルのオプションを定義することもできます。次のように設定しておけば --sushi
と書くだけで、そこに定義したオプション指定したことになります。
option --sushi --outside '^\S+' --exclude '^お子様セット.*'
-do
はデバッグ用のオプションで、最終的に実行されるオプションを表示します。
define: マクロ定義
複雑なオプションを定義する際には、マクロによる文字列置換を使用することができます。
define {base03} #002b36
define {base02} #073642
define {base01} #586e75
define {base00} #657b83
define {base0} #839496
define {base1} #93a1a1
define {base2} #eee8d5
define {base3} #fdf6e3
define {yellow} #b58900
define {orange} #cb4b16
define {red} #dc322f
define {magenta} #d33682
define {violet} #6c71c4
define {blue} #268bd2
define {cyan} #2aa198
define {green} #859900
option --solarized \
--cm {yellow} \
--cm {orange} \
--cm {red} \
--cm {magenta} \
--cm {violet} \
--cm {blue} \
--cm {cyan} \
--cm {green}
autoload: モジュールの自動読み込み
autoload
は、モジュールを自動的に読み込むための命令です。次の設定は --dig
および --git
オプションは dig
モジュールで定義されていることを指示します。--dig
オプションは -Mdig --dig
に展開され、dig
モジュールが自動的に読み込まれます。
autoload -Mdig --dig --git
__PERL__
: 任意コードの実行
.greplerc
の中に __PERL__
という文字列があると、それより後の内容を Perl プログラムとして評価します。
greple のオプションの中には、任意の関数を呼び出すことができるものがあります。たとえば --begin
オプションを使うと、ファイルを検索する前にその内容に対して行う処理を指定することができます。
.greplerc
に次のようなコードを用意したとします。
__PERL__
sub remove_header {
s/\A---\n(.+\n)+---\n\n*//;
}
すると、次のようにして、マークダウンファイルを検索する前に、ヘッダ部分を削除することができます。
greple --begin '&remove_header' *.md
他の言語にも対応できるように、このような形式にしていますが、今のところ対応しているのは Perl だけです。
まとめ
greple の初期設定ファイル .greplerc について紹介しました。
オプションを組み合わせることで、様々なタイプのデータに対応する複雑な処理を実現することができますが、どうしてもコマンド行が複雑化してきます。.greplerc に自分でよく使うオリジナルオプションを定義することで、作業を効率化することができます。
デフォルトオプションについては、.greplerc に記述する以外に、GREPLEOPTS
という環境変数を設定する方法もあります。
SEE ALSO
https://qiita.com/advent-calendar/2021/greple
- https://qiita.com/kaz-utashiro/items/5b6bcbe54891b3bd9db5
- https://qiita.com/kaz-utashiro/items/eb8c7067e6de34842fe3
- https://qiita.com/kaz-utashiro/items/165e744d4250adedc4c1
- https://qiita.com/kaz-utashiro/items/439e6abcecf36c520703
- https://qiita.com/kaz-utashiro/items/24ac0b8fdd30b598e069
- https://qiita.com/kaz-utashiro/items/a1ba4e3d07cf37dc25e3
- https://qiita.com/kaz-utashiro/items/0c8c944c17a72724b771
- https://qiita.com/kaz-utashiro/items/8783c2fd0cc4315b9a3d
- https://qiita.com/kaz-utashiro/items/84f5a6be6bf996076c64
- https://qiita.com/kaz-utashiro/items/ebc7ea99f800cfc8c90c
- https://qiita.com/kaz-utashiro/items/25a14e75380c39b5e0af
2.13 - カラーオプション
greple は、複数のハイライトカラーを使って結果を表示することができます。これまで紹介したような、検索キーワードの色分け以外にも、様々な色の指定が可能です。今回は、色の指定方法について紹介します。
カラーマップ
greple の色情報はカラーマップで管理され、その中にはインデックスリストと名前付きのリストという2種類の要素があります。つまり、リストとハッシュです。
-dc
というデバッグオプションを使うと、カラーマップ情報を見ることができます。デフォルトのカラーマップはこうです。
最初に表示されているのが名前付きリストで、ファイル名 (FILE
) や行番号 (LINE
) の表示色に関する情報を保持しています。
次にあるのがインデックスリストで、検索した文字列をハイライトするために使われます。
–colormap, –cm オプション
カラーマップは --colormap
オプションで指定します。ちょっと長いので --cm
と省略することもできます。デフォルトのカラーマップは、次のように指定したのと同じです。
--cm BLOCKEND=/WE,FILE=G,LINE=Y,PROGRESS=B,TEXT= \
--cm 000D/544,000D/454,000D/445 \
--cm 000D/455,000D/545,000D/554 \
--cm 000D/543,000D/453,000D/435,000D/534,000D/354,000D/345 \
--cm 000D/444,000D/433,000D/343,000D/334 \
--cm 000D/344,000D/434,000D/443,000D/333
1行目の名前付きのリストは NAME=color
という形式、2行目以下のインデックスリストはラベルなしの color
だけです。複数の要素をカンマで区切って同時に指定することができます。--cm
オプションは繰り返し使うことができるので、上の例では分けて書いてありますが、1つにまとめてしまっても構いません。
簡単に説明すると、G (Green), Y (Yellow) などの基本の色、D (ボールド) のような効果、544 のように 0-5 の数字3桁で表された216色で指定されています。スラッシュ (/
) の左がフォアグラウンド、右側はバックグラウンドカラーです。
色の指定
基本 16色
R r Red
G g Green
B b Blue
C c Cyan
M m Magenta
Y y Yellow
K k Black
W w White
大文字は、赤、緑、青、シアン、マジェンタ、黄、黒、白の基本の8色です。小文字は代替色を意味しますが、多くの端末では基本色よりも明るい色が指定されています。
RGB 6x6x6 216色
000 .. 555 : 6x6x6 RGB 216 colors
RGB 値を0から5の数字で表した216色です。
慣れると案外わかりやすくて、555は白、R以外を0にして500なら原色の赤。GとBの値を増やすと白に近づいて544は淡い赤という具合です。RGB と CMY のポジションが対応していて、R の部分を落とした055は、補色のシアン、Bを落として550なら黄色になります。1つだけ大きければ RGB、1つだけ小さければ CMY と憶えるとわかりやすいと思います。
基本のカラーマップは RGB に 543 のどれかを当てた組み合わせをバックグラウンドカラーとしています。
グレースケール 24色
L01
から L24
は、24階調のグレースケールです。L00
は黒 (000
)、L25
は白 (555
) と同じです。W
や w
は、真っ白とは限りません。
L00 .. L25 : Black (L00), 24 grey levels, White (L25)
12ビット/24ビット RGB
RGB を10進や16進の、12ビット値あるいは24ビット値で指定することもできます。ただし、256色表示の端末では表示できないので、その場合は近似する216色に変換して表示します。
(255,255,255) : 24bit decimal RGB colors
#000000 .. #FFFFFF : 24bit hex RGB colors
#000 .. #FFF : 12bit hex RGB 4096 colors
名前
https://en.wikipedia.org/wiki/X11_color_names で定義されている名前で色を指定することができます。
<red> <blue> <green> <cyan> <magenta> <yellow>
<aliceblue> <honeydue> <hotpink> <mooccasin>
<medium_aqua_marine>
効果
色以外の効果を指定することもできます。よく使うのはボールド(D
)、イタリック(I
)、アンダーライン(U
)、反転(S
)あたりでしょうか。見えなくする H
や、見え消し線を引く X
はサポートされていない端末もありますが、使い方によっては効果的です。点滅(F
)は面白いけど、うるさいのであまり使いません。早い点滅 (Q) をサポートしている端末は見たことがありません。
N None
Z 0 Zero (reset)
D 1 Double strike (boldface)
P 2 Pale (dark)
I 3 Italic
U 4 Underline
F 5 Flash (blink: slow)
Q 6 Quick (blink: rapid)
S 7 Stand out (reverse video)
H 8 Hide (concealed)
X 9 Cross out
E Erase Line
; No effect
/ Toggle foreground/background
^ Reset to foreground
行消去の E
は特殊で、文字の属性を定義するものではありません。これを指定すると、色設定のシークエンスと同時に、カーソルがある場所から行末までを消去するシークエンスを出力します。文字を消すのと同時に背景色で埋めるので、その色の線が引かれたように見えます。最初の例の BLOCKEND
で使われています。
ANSI 256色 ターミナルカラー
標準的な256色表示のANSI端末は、これまでに説明した基本の16色、6階調の RGB 216色、24階調のグレースケールを表示することができます。これは、次のコマンドで一覧することができます。
perl -MGetopt::EX::Colormap=:all -E colortable
フルカラーターミナル
24ビットフルカラーに対応している端末では、フルカラーの指定をそのまま出力することができます。Apple の標準ターミナルは256色表示ですが、iTerm2 などはフルカラー表示が可能です。greple は、COLORTERM
という環境変数に truecolor
という値が設定されていると、フルカラー端末であると認識します。
まとめ
greple は、複数の色を扱えることが特徴の1つです。しかし、色の指定が煩雑だと使いこなすのが難しいので、シンプルで統一的な方法で指定できるように工夫しています。現在は、色を管理する部分を切り出して Getopt::EX::Colormap
という独立したモジュールでリリースしています。
SEE ALSO
https://qiita.com/advent-calendar/2021/greple
- https://qiita.com/kaz-utashiro/items/5b6bcbe54891b3bd9db5
- https://qiita.com/kaz-utashiro/items/eb8c7067e6de34842fe3
- https://qiita.com/kaz-utashiro/items/165e744d4250adedc4c1
- https://qiita.com/kaz-utashiro/items/439e6abcecf36c520703
- https://qiita.com/kaz-utashiro/items/24ac0b8fdd30b598e069
- https://qiita.com/kaz-utashiro/items/a1ba4e3d07cf37dc25e3
- https://qiita.com/kaz-utashiro/items/0c8c944c17a72724b771
- https://qiita.com/kaz-utashiro/items/8783c2fd0cc4315b9a3d
- https://qiita.com/kaz-utashiro/items/84f5a6be6bf996076c64
- https://qiita.com/kaz-utashiro/items/ebc7ea99f800cfc8c90c
- https://qiita.com/kaz-utashiro/items/25a14e75380c39b5e0af
- https://qiita.com/kaz-utashiro/items/ebbeb8a5538a15ff04fc
2.14 - カラーオプション(続き)
色についてもっと色々
前回は、カラーマップを指定する --colormap
オプションと、色の指定の仕方について紹介しました。今回は、色を扱う様々なオプションについて紹介します。
–colormap
greple のカラーマップには、名前付きカラーと、インデックスカラーの2種類があり、どちらも --colormap
(--cm
) オプションで設定することができました。今回紹介するのは、マッチしたパターンをハイライトするために使用されるうインデックスカラーに関するものです。
–regioncolor, –ri
以前に、複数の検索範囲を指定すると、それぞれの領域に別の色が割り当てられる例を紹介しました。
greple -n --inside '^\S+' --outside '^\S+' '[ー\p{Katakana}]+' sushi.txt