LoginSignup
0
0

grepコマンドからの正規表現使用について(CSH)

Posted at

概要

本日は前回のシェルスクリプトから設定ファイルの内容を抽出してくるソースの追加の話になります。
例えば設定ファイルの行がコメントアウトされている行を除外するための、
GREPコマンドの正規表現を使うオプションを確認です。

前回の記事

前回の記事からの追加していきますので、こちらの設定ファイルを参照して作成します。

設定ファイル内容

下記のように設定ファイルの前行に同じ設定項目でコメントアウトされている設定があります。

<?
#$GLOBAL_BASE_DATE = "20230124"; // TEST : YYYYMMDD, TODAY : date("Ymd");
$GLOBAL_BASE_DATE = "20230130"; // TEST : YYYYMMDD, TODAY : date("Ymd");
?>

シェルスクリプト

前回のソースからGREPコマンドのオプションを以下のように追加します。
grep -e '^$GLOBAL_BASE_DATE'
※「'」記号にしないと正常に動かないですので、注意してください。

■ソース

#!/bin/csh -f
set BASE_DATE = `cat conf.php|grep -e '^\$GLOBAL_BASE_DATE'`
set DATE = `echo $BASE_DATE | awk 'BEGIN{FS=";"}{print $1}'|awk 'BEGIN{FS="="}{print $2}' | sed 's/\"//g'`
echo $DATE

■実行結果
下記のようにコメントアウトされている設定ファイルの内容ではなく先頭が「$」から始まる変数を取得しています。

pi@RPI3B:~/work/php $ cat conf.php
#$GLOBAL_BASE_DATE = "20230124"; // TEST : YYYYMMDD, TODAY : date("Ymd");
$GLOBAL_BASE_DATE = "20230130"; // TEST : YYYYMMDD, TODAY : date("Ymd");

pi@RPI3B:~/work/php $ csh test.csh
20230130
pi@RPI3B:~/work/php $

■Grepコマンドのオプション
下記のオプションで「-g」を利用しました。
まだ使っていないオプションもありますが、一応参考まで!!!

pi@RPI3B:~/work/php $ grep --help
Usage: grep [OPTION]... PATTERNS [FILE]...
Search for PATTERNS in each FILE.
Example: grep -i 'hello world' menu.h main.c
PATTERNS can contain multiple patterns separated by newlines.

Pattern selection and interpretation:
  -E, --extended-regexp     PATTERNS are extended regular expressions
  -F, --fixed-strings       PATTERNS are strings
  -G, --basic-regexp        PATTERNS are basic regular expressions
  -P, --perl-regexp         PATTERNS are Perl regular expressions
  -e, --regexp=PATTERNS     use PATTERNS for matching
  -f, --file=FILE           take PATTERNS from FILE
  -i, --ignore-case         ignore case distinctions
  -w, --word-regexp         match only whole words
  -x, --line-regexp         match only whole lines
  -z, --null-data           a data line ends in 0 byte, not newline

Miscellaneous:
  -s, --no-messages         suppress error messages
  -v, --invert-match        select non-matching lines
  -V, --version             display version information and exit
      --help                display this help text and exit

Output control:
  -m, --max-count=NUM       stop after NUM selected lines
  -b, --byte-offset         print the byte offset with output lines
  -n, --line-number         print line number with output lines
      --line-buffered       flush output on every line
  -H, --with-filename       print file name with output lines
  -h, --no-filename         suppress the file name prefix on output
      --label=LABEL         use LABEL as the standard input file name prefix
  -o, --only-matching       show only nonempty parts of lines that match
  -q, --quiet, --silent     suppress all normal output
      --binary-files=TYPE   assume that binary files are TYPE;
                            TYPE is 'binary', 'text', or 'without-match'
  -a, --text                equivalent to --binary-files=text
  -I                        equivalent to --binary-files=without-match
  -d, --directories=ACTION  how to handle directories;
                            ACTION is 'read', 'recurse', or 'skip'
  -D, --devices=ACTION      how to handle devices, FIFOs and sockets;
                            ACTION is 'read' or 'skip'
  -r, --recursive           like --directories=recurse
  -R, --dereference-recursive  likewise, but follow all symlinks
      --include=GLOB        search only files that match GLOB (a file pattern)
      --exclude=GLOB        skip files and directories matching GLOB
      --exclude-from=FILE   skip files matching any file pattern from FILE
      --exclude-dir=GLOB    skip directories that match GLOB
  -L, --files-without-match  print only names of FILEs with no selected lines
  -l, --files-with-matches  print only names of FILEs with selected lines
  -c, --count               print only a count of selected lines per FILE
  -T, --initial-tab         make tabs line up (if needed)
  -Z, --null                print 0 byte after FILE name

Context control:
  -B, --before-context=NUM  print NUM lines of leading context
  -A, --after-context=NUM   print NUM lines of trailing context
  -C, --context=NUM         print NUM lines of output context
  -NUM                      same as --context=NUM
      --color[=WHEN],
      --colour[=WHEN]       use markers to highlight the matching strings;
                            WHEN is 'always', 'never', or 'auto'
  -U, --binary              do not strip CR characters at EOL (MSDOS/Windows)

When FILE is '-', read standard input.  With no FILE, read '.' if
recursive, '-' otherwise.  With fewer than two FILEs, assume -h.
Exit status is 0 if any line (or file if -L) is selected, 1 otherwise;
if any error occurs and -q is not given, the exit status is 2.

Report bugs to: bug-grep@gnu.org
GNU grep home page: <http://www.gnu.org/software/grep/>
General help using GNU software: <https://www.gnu.org/gethelp/>
pi@RPI3B:~/work/php $

終わりに

バッチコマンドを作成したのですが、取得する設定値の行にコメントアウトされていて該当する設定値が正しくデータが取得できなかった原因でバッチが失敗しました。
GREPコマンドに正規表現のオプションはあるかなと思って調査してみたらありました。これで正規表現をそのまま利用して正しい設定ファイルの値を取得することができました。
簡単にできてラッキーでした。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0