【Linux】grep コマンドで【コメント行】および【空白行】を削除する方法

公開日時:2018年11月18日 / 最終更新日時:2019年02月11日

Linux には多数のコンフィグファイルがありますが、デフォルトではほとんどの行がコメントアウトされていることもあります。

コンフィグファイル内で実際に設定されている箇所だけを抽出できたら、より短時間で正確にコンフィグファイルを把握できるようになるでしょう。

そこで今回は grep コマンドで「コメント行」および「空白行」を削除する方法について解説します。

 

 

 

【コメント除外後】

-bash-4.2$ grep -v '^\s*#' postgresql.conf | grep -v '^\s*$' 
max_connections = 100                   # (change requires restart) 
shared_buffers = 32MB                   # min 128kB 
logging_collector = on                  # Enable capturing of stderr and csvlog 
log_filename = 'postgresql-%a.log'      # log file name pattern, 
log_truncate_on_rotation = on           # If on, an existing log file with the 
log_rotation_age = 1d                   # Automatic rotation of logfiles will 
log_rotation_size = 0                   # Automatic rotation of logfiles will 
log_timezone = 'UTC' 
datestyle = 'iso, mdy' 
timezone = 'UTC' 
lc_messages = 'en_US.UTF-8'                     # locale for system error message 
lc_monetary = 'en_US.UTF-8'                     # locale for monetary formatting 
lc_numeric = 'en_US.UTF-8'                      # locale for number formatting 
lc_time = 'en_US.UTF-8'                         # locale for time formatting 
default_text_search_config = 'pg_catalog.english' 
-bash-4.2$

 

実際の行数は15行です。

-bash-4.2$ grep -v '^\s*#' postgresql.conf | grep -v '^\s*$' | wc -l
15
-bash-4.2$

 

 

【コメント除外前】

コメント除外前の「postgresql.conf」ファイルです。デフォルトではほとんどが除外されています。

-bash-4.2$ cat postgresql.conf 
# ----------------------------- 
# PostgreSQL configuration file 
# ----------------------------- 

# This file consists of lines of the form: 

#   name = value 

# (The "=" is optional.)  Whitespace may be used.  Comments are introduced with 
# "#" anywhere on a line.  The complete list of parameter names and allowed 
# values can be found in the PostgreSQL documentation. 

 

~ 省略 ~

 
#------------------------------------------------------------------------------ 
# ERROR HANDLING 
#------------------------------------------------------------------------------ 
 
#exit_on_error = off                    # terminate session on any error? 
#restart_after_crash = on               # reinitialize after backend crash? 
 
 
#------------------------------------------------------------------------------ 
# CUSTOMIZED OPTIONS 
#------------------------------------------------------------------------------ 
 
# Add settings for extensions here 
-bash-4.2$

 

 

コメントアウトしない「postgresql.conf」ファイルの行数は「578」行あります。

これだけの行数の中からコメントアウトされていないパラメータを探し出すのは時間が掛かりますしミスも誘発しやすいです。

-bash-4.2$ cat postgresql.conf | wc -l
578
-bash-4.2$

 

 

以下のページを参考にさせていただきました。

grepでコメント行および空白行を削除する

 

 

Posted by 100%レンタルサーバーを使いこなすサイト管理人

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

AlphaOmega Captcha Medica  –  What Do You See?
     
 

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください

Secured By miniOrange