LoginSignup
1
0

More than 5 years have passed since last update.

sedで置換とテキストフィルタリングしよう

Last updated at Posted at 2018-10-03

はじめに

この記事では、sedの置換とテキストフィルタリングの基本的な使い方を紹介します。
sedのエッジの効いた使い方は、下記のQiita記事を読んでみてください。

sedのよさげなQiita記事

ワンライナーでお手軽実行可能なsed入門

sedコマンドの備忘録

find + sed で一斉置換

置換

sコマンドで置換

sed -e 's/SCRIPTCMD/g'

-e = EXPRESSION スクリプトコマンドを追加する

Ex) 行頭の#を置換して、コメントアウトを外す

$ sed -e 's/^#//g'

BEFORE

$ head /etc/httpd/conf/httpd.conf
#
# This is the main Apache HTTP server configuration file.  It contains the
# configuration directives that give the server its instructions.
# See <URL:http://httpd.apache.org/docs/2.4/> for detailed information.
# In particular, see
# <URL:http://httpd.apache.org/docs/2.4/mod/directives.html>
# for a discussion of each configuration directive.
#
# Do NOT simply read the instructions in here without understanding
# what they do.  They're here only as hints or reminders.  If you are unsure

AFTER

$ head /etc/httpd/conf/httpd.conf | sed -e 's/^#//g'

This is the main Apache HTTP server configuration file.  It contains the
configuration directives that give the server its instructions.
See <URL:http://httpd.apache.org/docs/2.4/> for detailed information.
In particular, see
<URL:http://httpd.apache.org/docs/2.4/mod/directives.html>
for a discussion of each configuration directive.

Do NOT simply read the instructions in here without understanding
what they do.  They're here only as hints or reminders.  If you are unsure

テキストフィルタリング

pコマンドで引っかかった行だけ出力

sed -n '/SCRIPTCMD/p'

-n = SILENT pコマンドで引っかかった行だけ出力
※ -nとpコマンドはセットで使う

Ex) 最初の行と最後の行の文字列を指定して、出力範囲をフィルタリングする

$ sed -n '/This is the main/,/In particular, see/p'

BEFORE

$ head /etc/httpd/conf/httpd.conf
#
# This is the main Apache HTTP server configuration file.  It contains the
# configuration directives that give the server its instructions.
# See <URL:http://httpd.apache.org/docs/2.4/> for detailed information.
# In particular, see
# <URL:http://httpd.apache.org/docs/2.4/mod/directives.html>
# for a discussion of each configuration directive.
#
# Do NOT simply read the instructions in here without understanding
# what they do.  They're here only as hints or reminders.  If you are unsure

AFTER

$ head /etc/httpd/conf/httpd.conf | sed -n '/This is the main/,/In particular, see/p'
# This is the main Apache HTTP server configuration file.  It contains the
# configuration directives that give the server its instructions.
# See <URL:http://httpd.apache.org/docs/2.4/> for detailed information.
# In particular, see

Ex) 最初の行番号と最後の行番号を指定して、出力範囲をフィルタリングする

$ sed -n '2,3p'

BEFORE

$ head /etc/httpd/conf/httpd.conf
#
# This is the main Apache HTTP server configuration file.  It contains the
# configuration directives that give the server its instructions.
# See <URL:http://httpd.apache.org/docs/2.4/> for detailed information.
# In particular, see
# <URL:http://httpd.apache.org/docs/2.4/mod/directives.html>
# for a discussion of each configuration directive.
#
# Do NOT simply read the instructions in here without understanding
# what they do.  They're here only as hints or reminders.  If you are unsure

AFTER

$ head /etc/httpd/conf/httpd.conf | sed -n '2,3p'
# This is the main Apache HTTP server configuration file.  It contains the
# configuration directives that give the server its instructions.

dコマンドで指定した文字列が含まれる行を削除

Ex) 空白行を出力しないようにフィルタリングする

$ sed -e '/^#$/d'

BEFORE

$ head /etc/httpd/conf/httpd.conf
#
# This is the main Apache HTTP server configuration file.  It contains the
# configuration directives that give the server its instructions.
# See <URL:http://httpd.apache.org/docs/2.4/> for detailed information.
# In particular, see
# <URL:http://httpd.apache.org/docs/2.4/mod/directives.html>
# for a discussion of each configuration directive.
#
# Do NOT simply read the instructions in here without understanding
# what they do.  They're here only as hints or reminders.  If you are unsure

AFTER

$ head /etc/httpd/conf/httpd.conf | sed -e '/^#$/d'
# This is the main Apache HTTP server configuration file.  It contains the
# configuration directives that give the server its instructions.
# See <URL:http://httpd.apache.org/docs/2.4/> for detailed information.
# In particular, see
# <URL:http://httpd.apache.org/docs/2.4/mod/directives.html>
# for a discussion of each configuration directive.
# Do NOT simply read the instructions in here without understanding
# what they do.  They're here only as hints or reminders.  If you are unsure
1
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
1
0