正規表現を使ったテキスト検索
正規表現
-
正規表現(Regular Expression)
- 特定の条件を表す文字列を抽象的に表現したもの
文字
a
とか- そのまま、文字そのものを表す
任意の1文字
.
文字クラス
-
一連の文字集合を
[]
で囲んで表す-
[a-z]
- 英小文字
-
-
余集合を表すには
^
を[]
内冒頭に付す-
[^0-9]
- 数字以外
-
行頭と行末
-
^
- 行頭
-
$
- 行末
-
^$
- 空行を表す
-
アンカー
- 文字列内の位置にマッチする正規表現
直前の文字の繰り返し
-
*
- 0文字以上
-
?
- 【拡張正規表現】0 or 1文字
-
+
- 【拡張正規表現】1文字以上
-
+?
-
【拡張正規表現】1文字以上・最短一致数量子
ab+
はabbbb
のabbbb
部分にマッチするab+?
はabbbb
のab
部分にマッチする
-
-
数量詞
-
{n}
n
文字
-
{n,m}
- 【拡張正規表現】
n
文字以上m
文字以下
- 【拡張正規表現】
-
()
で囲むと、複数文字を1文字とみなして繰り返すことが出来る
特殊文字
-
文字として使いたいとき
\
によるエスケープが必要な連中.
*
+
^
$
\
- などなど
基本正規表現と拡張正規表現
-
基本正規表現には
|
がなかったりする?
,+
なんかもないが、数量子で替えが効く
grep
コマンド
- ファイルやテキストストリームの中から、正規表現にマッチする文字列を検索する
- ファイルは複数指定可能
man grep
GREP(1) User Commands GREP(1)
NAME
grep, egrep, fgrep, rgrep - print lines matching a pattern
SYNOPSIS
grep [OPTIONS] PATTERN [FILE...]
grep [OPTIONS] -e PATTERN ... [FILE...]
grep [OPTIONS] -f FILE ... [FILE...]
DESCRIPTION
...
-e PATTERN, --regexp=PATTERN
Use PATTERN as the pattern. If this option is used
multiple times or is combined with the -f (--file) option,
search for all patterns given. This option can be used to
protect a pattern beginning with “-”.
...
-c, --count
Suppress normal output; instead print a count of matching
lines for each input file. With the -v, --invert-match
option (see below), count non-matching lines.
...
-f FILE, --file=FILE
Obtain patterns from FILE, one per line. If this option is
used multiple times or is combined with the -e (--regexp)
option, search for all patterns given. The empty file
contains zero patterns, and therefore matches nothing.
...
-i, --ignore-case
Ignore case distinctions, so that characters that differ
only in case match each other.
...
-n, --line-number
Prefix each line of output with the 1-based line number
within its input file.
...
-v, --invert-match
Invert the sense of matching, to select non-matching lines.
...
-E, --extended-regexp
Interpret PATTERN as an extended regular expression (ERE,
see below).
-
主なオプション
-
-e PATTERN
,--regexp=PATTERN
- 検索パターン指定
-e
はなくても良い
-
-c
,--count
- マッチした行数を出力
-
-f FILE
,--file=FILE
- 検索パターンをファイルから読み込む
-
-i
,--ignore-case
- 大文字小文字区別しない
-
-n
,--line-number
- 行番号も出す
-
-v
,--invert-match
- マッチしない行を出力する
-c
と併用すると、マッチしない行数を得る
-
-E
,--extended-regexp
- 拡張正規表現を使用する
-
grep -e 'cron' /etc/crontab
- 13ヒット、8行
# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.
17 * * * * root cd / && run-parts --report /etc/cron.hourly
25 6 * * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6 * * 7 root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6 1 * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
- 行数得る
grep -e 'cron' -c /etc/crontab
8
- 基本正規表現
grep -e 'cron|anacron' /etc/crontab
cron|anacron
なる文字列はない
- 拡張正規表現
grep -e 'cron|anacron' -E /etc/crontab
cron
またはanacron
がマッチする
# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.
17 * * * * root cd / && run-parts --report /etc/cron.hourly
25 6 * * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6 * * 7 root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6 1 * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
egrep
コマンド
- 拡張正規表現使う
grep -E
とおなじ
-E, --extended-regexp
Interpret PATTERN as an extended regular expression (ERE,
see below).
fgrep
コマンド
-
正規表現使わない
- 特殊文字のエスケープ要らない
grep -F
とおなじ
-F, --fixed-strings
Interpret PATTERN as a list of fixed strings (instead of
regular expressions), separated by newlines, any of which
is to be matched.
rgrep
コマンド
- recursiveなヤツ
grep -r
とおなじ
sedコマンド
- Stream EDitor
- コマンドや、コマンドを記述したスクリプトを与え、テキストストリームを編集する
SED(1) User Commands SED(1)
NAME
sed - stream editor for filtering and transforming text
SYNOPSIS
sed [OPTION]... {script-only-if-no-other-script} [input-file]...
...
-e script, --expression=script
add the script to the commands to be executed
...
-f script-file, --file=script-file
add the contents of script-file to the commands to be exe‐
cuted
...
-i[SUFFIX], --in-place[=SUFFIX]
edit files in place (makes backup if SUFFIX supplied)
-
主なオプション
-
-e script
,--expression=script
- コマンド指定
- コマンドが1つなら
-e
省略可能
-
-f script-file
,--file=script-file
- スクリプトファイル入力
-e
オプションで渡す内容をスクリプトにまとめておく
-
-i[SUFFIX]
,--in-place[=SUFFIX]
-
ファイルをmodifyする
- cf. デフォルトでは入力ファイルimmutable
SUFFIX
を指定すると、指定の接尾辞でバックアップ
-
-
dコマンド
d Delete pattern space. Start next cycle.
- けす
- 1-5行目消す例
sed 1,5d /etc/crontab
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
# m h dom mon dow user command
17 * * * * root cd / && run-parts --report /etc/cron.hourly
25 6 * * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6 * * 7 root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6 1 * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
#
-
この例だと
tail -n +5
と同じ- と思ったら違った
tail -n +5
は頭4行削る
sコマンド
s/regexp/replacement/
Attempt to match regexp against the pattern space. If suc‐
cessful, replace that portion matched with replacement.
The replacement may contain the special character & to
refer to that portion of the pattern space which matched,
and the special escapes \1 through \9 to refer to the cor‐
responding matching sub-expressions in the regexp.
-
全置換
g
スイッチ付ける- 【備忘録】手元のUbuntuプリインストールのものでは
g
スイッチがなくても全置換だった
sed -e 's/cron/CRON/g' /etc/crontab
# /etc/CRONtab: system-wide CRONtab
# Unlike any other CRONtab you don't have to run the `CRONtab'
# command to install the new version when you edit this file
# and files in /etc/CRON.d. These files also have username fields,
# that none of the other CRONtabs do.
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
# m h dom mon dow user command
17 * * * * root cd / && run-parts --report /etc/CRON.hourly
25 6 * * * root test -x /usr/sbin/anaCRON || ( cd / && run-parts --report /etc/CRON.daily )
47 6 * * 7 root test -x /usr/sbin/anaCRON || ( cd / && run-parts --report /etc/CRON.weekly )
52 6 1 * * root test -x /usr/sbin/anaCRON || ( cd / && run-parts --report /etc/CRON.monthly )
#
- コメント消す
sed -e 's/^#//g' /etc/crontab
/etc/crontab: system-wide crontab
Unlike any other crontab you don't have to run the `crontab'
command to install the new version when you edit this file
and files in /etc/cron.d. These files also have username fields,
that none of the other crontabs do.
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
m h dom mon dow user command
17 * * * * root cd / && run-parts --report /etc/cron.hourly
25 6 * * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6 * * 7 root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6 1 * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
yコマンド
y/source/dest/
Transliterate the characters in the pattern space which
appear in source to the corresponding character in dest.
-
source
の文字集合をdest
の文字集合に写像- 文字列長は同じであること
- シーザー暗号みたいなかんじ
sed -e 'y/abcdefghij/bcdefghija/' /etc/crontab
# /ftd/drontbc: systfm-wjef drontbc
# Unljkf bny otifr drontbc you eon't ibvf to run tif `drontbc'
# dommbne to jnstbll tif nfw vfrsjon wifn you fejt tijs gjlf
# bne gjlfs jn /ftd/dron.e. Tifsf gjlfs blso ibvf usfrnbmf gjfles,
# tibt nonf og tif otifr drontbcs eo.
SHELL=/cjn/si
PATH=/usr/lodbl/scjn:/usr/lodbl/cjn:/scjn:/cjn:/usr/scjn:/usr/cjn
# m i eom mon eow usfr dommbne
17 * * * * root de / && run-pbrts --rfport /ftd/dron.iourly
25 6 * * * root tfst -x /usr/scjn/bnbdron || ( de / && run-pbrts --rfport /ftd/dron.ebjly )
47 6 * * 7 root tfst -x /usr/scjn/bnbdron || ( de / && run-pbrts --rfport /ftd/dron.wffkly )
52 6 1 * * root tfst -x /usr/scjn/bnbdron || ( de / && run-pbrts --rfport /ftd/dron.montily )
#
ファイルの基本的な編集
- Linuxでは設定ファイルを書き換えることが多々ある
- テキストファイルを編集する能力が不可欠
-
有名なエディタ
-
vi
- たいていプリインストールされている
-
vim: vi improved
- viの改良・拡張版
-
emacs
- 高機能・高拡張性
-
nano
- わりと簡単
-
-
EDITOR環境変数
- エディタのパスを指定
- visudoとかvipwとかで起動するエディタを指定できる
viエディタの基本
- 操作体系が独特
vi [-R] [ファイル名]
-
-R
- 読み取り専用モード
- 間違って編集してしまうのを防ぐ
:w!
とかすると保存できるみたい
-
入力モードへの切替
-
i
/a
- カーソルの前/後にテキスト入力
-
I
/A
- 行頭/行末にカーソル移動しテキスト入力
-
o
/O
- カレント行の下/上に空白行を挿入し、その行でテキスト入力
-
-
カーソル移動
-
h
/j
/k
/l
- ←↓↑→
-
0
- 行頭
-
$
- 行末
-
H
/L
- 画面内の一番上/下の行頭
-
gg
/G
- ファイル先頭行/最終行の行頭
-
3G
- 3行目行頭
-
:3
- 3行目行頭
-
-
終了とか保存とか
-
:q
/:q!
- 保存せず終了、プロンプトあり/なし
-
:wq
,ZZ
- 上書き保存して終了
-
:w
- 上書き保存
-
:e!
- revert
-
:r ファイル名
- ファイルの内容をカレント行以降に読み込む
-
:!
-
シェルコマンド実行
:! ls
とか
- RETで戻る
-
-
:r コマンド
-
シェルコマンドの実行結果を挿入
:r! seq 1 10
で連番挿入とか
-
-
-
編集コマンド
-
x
/X
- del/backspace
-
dd
- カレント行削除、詰める
-
dw
- delete word
- カーソル位置から次の単語まで削除
-
yy
- カレント行をバッファにコピー
-
p
/P
- pasteか
- カレント行の下/上にバッファの内容貼り付け
-
r
- replace
- カーソル位置の1文字を置換
-
-
検索コマンド
-
/ パターン
/? パターン/
- 後方/前方に検索
-
n
/N
- 次を検索 順方向/逆方向
-
:noh
- no-highlight
- 候補のハイライト表示の解除
-
:%s/A/B/
- 文字列AをBに置換、最初のもののみ
- sedのsコマンドとおなじ
-
:%s/A/B/g
- 文字列AをBに全置換
- sedのsコマンドのgスイッチとおなじ
-
-
設定変更
-
:set nu
,:set number
- 行番号表示
-
:set nonu
,:set nonumber
- 行番号非表示
-
:set ts=タブ幅
- タブ幅を数値指定
-