正規表現に関してのいくつかのトピックスを学んで行く
文の頭 ^
文の頭は、^
でマッチする。Ubuntu の環境で実行したサンプルは次の通り。本来は、Ubuntu 14.x だがなかったので、16.x で実行して、NTP をインストールして環境をつくった。
文頭のマッチをテストするためには、grep
を使うのが簡単。最初はシェルプログラムを書いていた
#!/usr/local/bin/bash
declare -a word;
word[0]="Color"
word[1]="color"
word[2]="colour"
word[3]="colors"
for ((i = 0 ; i < ${#word[@]} ; i++ )); do
# \b should work but not work.
if [[ ${word[i]} =~ ^[Cc]olou?r$ ]]; then
echo "Match ${word[i]}"
else
echo "Not Match ${word[i]}"
fi
done
がインストラクションの通り、grep で試す方がめっちゃ簡単だ。
server
にマッチ
$ grep server /etc/ntp.conf
# Specify one or more NTP servers.
# Use servers from the NTP Pool Project. Approved by Ubuntu Technical Board
# Use Ubuntu's ntp server as a fallback.
# Note that "restrict" applies to both servers and clients, so a configuration
# up blocking replies from your own upstream servers.
# Local users may interrogate the ntp server more closely.
#server 127.127.8.1 mode 135 prefer # Meinberg GPS167 with PPS
#server 127.127.22.1 # ATOM(PPS)
ここから、先頭の#server のみを抽出したいので^
を使う
$ grep '^#server' /etc/ntp.conf
#server 127.127.8.1 mode 135 prefer # Meinberg GPS167 with PPS
#server 127.127.22.1 # ATOM(PPS)
完璧。
文の最後 $
文の最後は、$
でマッチする。
$ grep 'PPS$' /etc/ntp.conf
#server 127.127.8.1 mode 135 prefer # Meinberg GPS167 with PPS
完璧
試しにログローテーションのファイル群で、4で終わっているものを見つける。意図していることは、ログローテーションで、いくつの古いログを保持しておくか?それ以上になると古いものを消す。という設定のおそらくデフォルトの4を見つける。
$ grep '4$' /etc/logrotate.d/*
/etc/logrotate.d/rsyslog: rotate 4
/etc/logrotate.d/ufw: rotate 4
ちなみに、grep -v
は条件を含まないものになる。
z$ grep -v '4$' /etc/logrotate.d/*
/etc/logrotate.d/apport:/var/log/apport.log {
/etc/logrotate.d/apport: daily
/etc/logrotate.d/apport: rotate 7
/etc/logrotate.d/apport: delaycompress
/etc/logrotate.d/apport: compress
/etc/logrotate.d/apport: notifempty
/etc/logrotate.d/apport: missingok
/etc/logrotate.d/apport:}
/etc/logrotate.d/apport:
/etc/logrotate.d/apt:/var/log/apt/term.log {
/etc/logrotate.d/apt: rotate 12
/etc/logrotate.d/apt: monthly
/etc/logrotate.d/apt: compress
/etc/logrotate.d/apt: missingok
/etc/logrotate.d/apt: notifempty
/etc/logrotate.d/apt:}
/etc/logrotate.d/apt:
/etc/logrotate.d/apt:/var/log/apt/history.log {
/etc/logrotate.d/apt: rotate 12
/etc/logrotate.d/apt: monthly
/etc/logrotate.d/apt: compress
/etc/logrotate.d/apt: missingok
/etc/logrotate.d/apt: notifempty
/etc/logrotate.d/apt:}
:
確かにどれも4終わりじゃない。
応用として、空行を検出するのはこれ。
$ grep '^$' /etc/ntp.conf
-v
でから行以外
$ grep -v '^$' /etc/ntp.conf
# /etc/ntp.conf, configuration for ntpd; see ntp.conf(5) for help
driftfile /var/lib/ntp/ntp.drift
# Enable this if you want statistics to be logged.
#statsdir /var/log/ntpstats/
statistics loopstats peerstats clockstats
filegen loopstats file loopstats type day enable
filegen peerstats file peerstats type day enable
filegen clockstats file clockstats type day enable
# Specify one or more NTP servers.
おまけで、該当のファイルをコントロールコードが見えるようにして表示してみる。改行コードが、$
として表示されている。ちなみに、オプションの-vet は、コントロールコードの表示、!$
はひつ前のコマンドのパラメータを表す。
$ cat -vet !$
cat -vet /etc/ntp.conf
# /etc/ntp.conf, configuration for ntpd; see ntp.conf(5) for help$
$
driftfile /var/lib/ntp/ntp.drift$
$
# Enable this if you want statistics to be logged.$
#statsdir /var/log/ntpstats/$
$
statistics loopstats peerstats clockstats$
filegen loopstats file loopstats type day enable$
filegen peerstats file peerstats type day enable$
filegen clockstats file clockstats type day enable$
$
# Specify one or more NTP servers.$
$
: