クリップボードにコピーしました!

gnu_sed_command アイデア

created-2026/04/01 updated-2026/04/02

目次

骨子案

はじめに(Introduction)

  1. 自己紹介
    1. こんにちは、running terminal commandsのターナです
  2. 記事概要説明
    1. この記事では、GNUプロジェクトのsedコマンドについて公式サイトに記載されているコマンドを簡単に実行できる形で書かれています
    2. sedはファイルや標準入力に対してテキスト変換などのデータ処理を行うために使用されます
      由来は、ストリームエディタを意味する英語の「stream editor」です
      入力の読み取り単位は行で、sedスクリプトという命令文に従ってテキスト変換を行います
      出力の単位は行です
      照合ルールに従って場合分けするフィルタという認識で良い
      一括で定形の処理をおこなう場合に威力を発揮するものは、大量のテキストファイルです
      正規表現に対応しており、ある条件の範囲内の文字列を探し出して処理することができます
    3. 挙動を簡単に確認するために一部のコマンドを改変
    4. 初心者の方でも実行可能
  3. 引用元紹介
    1. 引用元のGNU sedです
    2. https://www.gnu.org/software/sed/
  4. メリット紹介
    1. 公式マニュアルの難解な例を、実際に動かせる形で整理
    2. コードをクリックすることでクリップボードにコピーされるので、ターミナルにペーストするだけで完結
    3. 「sedの学習を始めたばかりの人」向け
    4. 「逆引きリファレンスとして使いたい人」向け
    5. BSDのsedかGNUのsedかを明確化

2.1 Overview

引用元のURL記載

https://www.gnu.org/software/sed/manual/html_node/Overview.html#Overview

input.txt ファイル内の「hello」を「world」に置換するパターンの紹介

実行に必要なファイル作成

mkdir -p ~/sed_rtc/Overview
cd ~/sed_rtc/Overview
cat << 'EOF' > input2_1_1.txt; cat input2_1_1.txt;
hello world
hello hello
EOF

機能確認コマンド

sed 's/hello/world/' input2_1_1.txt > output2_1_1.txt
cat output2_1_1.txt

ファイルをどこに作成するか
読み込むファイルの作成
ファイルに出力した後の内容を表示するコマンド

実行結果

world world
world hello

ーーーーーーーーーーーーーーーーーーーーーーーーー

input.txt ファイル内の「hello」を「world」に置換

実行に必要なファイル作成

mkdir -p ~/sed_rtc/Overview
cd ~/sed_rtc/Overview
cat << 'EOF' > input2_1_2.txt; cat input2_1_2.txt;
hello world
hello hello
EOF

機能確認コマンド

sed 's/hello/world/' input2_1_2.txt > output2_1_2_1.txt
cat output2_1_2_1.txt
sed 's/hello/world/' < input2_1_2.txt > output2_1_2_2.txt
cat output2_1_2_2.txt
cat input2_1_2.txt | sed 's/hello/world/' - > output2_1_2_3.txt
cat output2_1_2_3.txt

実行結果

world world
world hello

ーーーーーーーーーーーーーーーーーーーーーーーーー

file2_1_3.txt を編集し出力

実行に必要なファイル作成

mkdir -p ~/sed_rtc/Overview
cd ~/sed_rtc/Overview
cat << 'EOF' > file2_1_3.txt; cat file2_1_3.txt;
hello world
hello hello
EOF

機能確認コマンド

sed -i 's/hello/world/' file2_1_3.txt
cat file2_1_3.txt

実行結果

world world
world hello

ーーーーーーーーーーーーーーーーーーーーーーーーー

入力ファイルの2行目のみを出力

実行に必要なファイル作成

mkdir -p ~/sed_rtc/Overview
cd ~/sed_rtc/Overview
cat << 'EOF' > file2_1_4.txt; cat file2_1_4.txt;
hello world
hello hello
world hello
EOF

機能確認コマンド

sed -n '2p' file2_1_4.txt

実行結果

hello hello

ーーーーーーーーーーーーーーーーーーーーーーーーー

3つの入力ファイルの1行目を出力

実行に必要なファイル作成

mkdir -p ~/sed_rtc/Overview
cd ~/sed_rtc/Overview
cat << 'EOF' > one.txt; cat one.txt;
hello one
EOF
cat << 'EOF' > two.txt; cat two.txt;
hello two
EOF
cat << 'EOF' > three.txt; cat three.txt;
hello three
EOF

機能確認コマンド

sed -n '1p ; $p' one.txt two.txt three.txt

実行結果

hello two
hello three

ーーーーーーーーーーーーーーーーーーーーーーーーー

3つの入力ファイルの1行目を出力

実行に必要なファイル作成

mkdir -p ~/sed_rtc/Overview
cd ~/sed_rtc/Overview
cat << 'EOF' > input2_1_6.txt; cat input2_1_6.txt;
hello world
hello hello
EOF

機能確認コマンド

sed 's/hello/world/' input2_1_6.txt > output2_1_6_1.txt
cat output2_1_6_1.txt
sed -e 's/hello/world/' input2_1_6.txt > output2_1_6_2.txt
cat output2_1_6_2.txt
sed --expression='s/hello/world/' input2_1_6.txt > output2_1_6_3.txt
cat output2_1_6_3.txt
echo 's/hello/world/' > myscript.sed
sed -f myscript.sed input2_1_6.txt > output2_1_6_4.txt
cat output2_1_6_4.txt
sed --file=myscript.sed input2_1_6.txt > output2_1_6_5.txt
cat output2_1_6_5.txt

実行結果

world world
world hello

ーーーーーーーーーーーーーーーーーーーーーーーーー

2.2 Command-Line Options

引用元のURL記載

https://www.gnu.org/software/sed/manual/html_node/Command_002dLine-Options.html#Command_002dLine-Options

sedのバージョンと著作権表示を出力

sed --version

実行結果

sed (GNU sed) 4.9
Copyright (C) 2022 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Jay Fenlason, Tom Lord, Ken Pizzini,
Paolo Bonzini, Jim Meyering, and Assaf Gordon.

This sed program was built without SELinux support.

GNU sed home page: <https://www.gnu.org/software/sed/>.
General help using GNU software: <https://www.gnu.org/gethelp/>.
E-mail bug reports to: <bug-sed@gnu.org>.

ーーーーーーーーーーーーーーーーーーーーーーーーー

コマンドラインオプションとバグ報告先アドレスを簡潔にまとめた使用方法メッセージを表示

sed --help

実行結果

Usage: sed [OPTION]... {script-only-if-no-other-script} [input-file]...

  -n, --quiet, --silent
                 suppress automatic printing of pattern space
      --debug
                 annotate program execution
  -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 executed
  --follow-symlinks
                 follow symlinks when processing in place
  -i[SUFFIX], --in-place[=SUFFIX]
                 edit files in place (makes backup if SUFFIX supplied)
  -l N, --line-length=N
                 specify the desired line-wrap length for the `l' command
  --posix
                 disable all GNU extensions.
  -E, -r, --regexp-extended
                 use extended regular expressions in the script
                 (for portability use POSIX -E).
  -s, --separate
                 consider files as separate rather than as a single,
                 continuous long stream.
      --sandbox
                 operate in sandbox mode (disable e/r/w commands).
  -u, --unbuffered
                 load minimal amounts of data from the input files and flush
                 the output buffers more often
  -z, --null-data
                 separate lines by NUL characters
      --help     display this help and exit
      --version  output version information and exit

If no -e, --expression, -f, or --file option is given, then the first
non-option argument is taken as the sed script to interpret.  All
remaining arguments are names of input files; if no input files are
specified, then the standard input is read.

GNU sed home page: <https://www.gnu.org/software/sed/>.
General help using GNU software: <https://www.gnu.org/gethelp/>.
E-mail bug reports to: <bug-sed@gnu.org>.

ーーーーーーーーーーーーーーーーーーーーーーーーー

自動出力がを無効化

sed -n
sed --quiet
sed --silent

実行結果

Usage: sed [OPTION]... {script-only-if-no-other-script} [input-file]...

  -n, --quiet, --silent
                 suppress automatic printing of pattern space
      --debug
                 annotate program execution
  -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 executed
  --follow-symlinks
                 follow symlinks when processing in place
  -i[SUFFIX], --in-place[=SUFFIX]
                 edit files in place (makes backup if SUFFIX supplied)
  -l N, --line-length=N
                 specify the desired line-wrap length for the `l' command
  --posix
                 disable all GNU extensions.
  -E, -r, --regexp-extended
                 use extended regular expressions in the script
                 (for portability use POSIX -E).
  -s, --separate
                 consider files as separate rather than as a single,
                 continuous long stream.
      --sandbox
                 operate in sandbox mode (disable e/r/w commands).
  -u, --unbuffered
                 load minimal amounts of data from the input files and flush
                 the output buffers more often
  -z, --null-data
                 separate lines by NUL characters
      --help     display this help and exit
      --version  output version information and exit

If no -e, --expression, -f, or --file option is given, then the first
non-option argument is taken as the sed script to interpret.  All
remaining arguments are names of input files; if no input files are
specified, then the standard input is read.

GNU sed home page: <https://www.gnu.org/software/sed/>.
General help using GNU software: <https://www.gnu.org/gethelp/>.

ーーーーーーーーーーーーーーーーーーーーーーーーー

入力されたsedプログラムを正規形式で出力し、プログラムの実行内容を注釈として追加してください。

echo 1 | sed '\%1%s21232'
echo 1 | sed --debug '\%1%s21232'

実行結果

3

SED PROGRAM:
  /1/ s/1/3/
INPUT:   'STDIN' line 1
PATTERN: 1
COMMAND: /1/ s/1/3/
MATCHED REGEX REGISTERS
  regex[0] = 0-1 '1'
PATTERN: 3
END-OF-CYCLE:
3

ーーーーーーーーーーーーーーーーーーーーーーーーー

入力されたsedプログラムを正規形式で出力し、プログラムの実行内容を注釈として追加してください。

実行に必要なファイル作成

mkdir -p ~/sed_rtc/Command-Line-Options
cd ~/sed_rtc/Command-Line-Options
echo "foo foo bar" > FILE; cat FILE;

機能確認コマンド

sed -ni 's/foo/bar/' FILE
cat FILE

実行結果

nothing

ーーーーーーーーーーーーーーーーーーーーーーーーー

2.3 Exit status

qとQを使用して、カスタム終了コード値で sed を終了

echo | sed 'Q42' ; echo $?
echo | sed 'q8' ; echo $?

実行結果

42

8

ーーーーーーーーーーーーーーーーーーーーーーーーー

3.1 sed script overview

引用元のURL記載

https://www.gnu.org/software/sed/manual/html_node/sed-script-overview.html#sed-script-overview

入力の2行目から4行目を削除

実行に必要なファイル作成

mkdir -p ~/sed_rtc/sed-script-overview
cd ~/sed_rtc/sed-script-overview
cat << 'EOF' > input31.txt; cat input31.txt;
hello hello
hello foo
hello world
foo hello
world foo
EOF

機能確認コマンド

sed '2,4d' input31.txt > output311.txt
cat output311.txt

実行結果

hello hello
world foo

ーーーーーーーーーーーーーーーーーーーーーーーーー

「foo」で始まる行が見つかるまで、入力されたすべての文字列を出力

cat input31.txt
sed '/^foo/q42' input31.txt > output312.txt
cat output312.txt

実行結果

hello hello
hello foo
hello world
foo hello

ーーーーーーーーーーーーーーーーーーーーーーーーー

正規表現 /^foo/ に一致する行を削除し、文字列「hello」のすべての出現箇所を「world」に置換

cat input31.txt
sed '/^foo/d ; s/hello/world/' input31.txt > output3131.txt
cat output3131.txt
cat input31.txt
sed -e '/^foo/d' -e 's/hello/world/' input31.txt > output3132.txt
cat output3132.txt
cat input31.txt
echo '/^foo/d' > script.sed
echo 's/hello/world/' >> script.sed
sed -f script.sed input31.txt > output3133.txt
cat output3133.txt
cat input31.txt
echo 's/hello/world/' > script2.sed
sed -e '/^foo/d' -f script2.sed input31.txt > output3134.txt
cat output3134.txt

実行結果

world hello
world foo
world world
world foo

ーーーーーーーーーーーーーーーーーーーーーーーーー

3.3 The s Command

引用元のURL記載

https://www.gnu.org/software/sed/manual/html_node/The-_0022s_0022-Command.html#The-_0022s_0022-Command

文字列の「a-b-」をキャプチャとupperケースを使用して置換

echo "a-b-" | sed 's/\(b\?\)-/x\u\1/g'

\(b\?)-は、-かb-に該当するかどうか

実行結果

hello hello
world foo

ーーーーーーーーーーーーーーーーーーーーーーーーー

文字列の「a-b-」をキャプチャとupperケースを使用して置換

echo "a-b-" | sed 's/\(b\?\)-/\u\1x/g'

\1が空文字の場合、後の文字(今回はx)に\uが適用されて大文字になる

実行結果

hello hello
world foo

ーーーーーーーーーーーーーーーーーーーーーーーーー

3.4 Often-Used Commands

引用元のURL記載

https://www.gnu.org/software/sed/manual/html_node/Common-Commands.html#Common-Commands

2行目を出力した後に停止

seq 3 | sed 2q

実行結果

1
2

ーーーーーーーーーーーーーーーーーーーーーーーーー

2行目の入力を削除

seq 3 | sed 2d

実行結果

1
3

ーーーーーーーーーーーーーーーーーーーーーーーーー

2行目の入力のみを出力

seq 3 | sed -n 2p

実行結果

2

ーーーーーーーーーーーーーーーーーーーーーーーーー

3行ごとに置換(その1)

seq 6 | sed 'n;n;s/./x/'

実行結果

1
2
x
4
5
x

ーーーーーーーーーーーーーーーーーーーーーーーーー

3行ごとに置換(その2)

seq 6 | sed '0~3s/./x/'

実行結果

1
2
x
4
5
x

ーーーーーーーーーーーーーーーーーーーーーーーーー

置換してから、2行目の入力を出力

seq 3 | sed -n '2{s/2/X/ ; p}'

実行結果

X

ーーーーーーーーーーーーーーーーーーーーーーーーー

3.5 Less Frequently-Used Commands

引用元のURL記載

https://www.gnu.org/software/sed/manual/html_node/Other-Commands.html#Other-Commands

「a~j」を「0~9」に置換

echo hello world | sed 'y/abcdefghij/0123456789/'

同じ文字数である必要

実行結果

74llo worl3

ーーーーーーーーーーーーーーーーーーーーーーーーー

2行目の後に「hello」を追加(その1)

seq 3 | sed '2a hello'

実行結果

1
2
hello
3

ーーーーーーーーーーーーーーーーーーーーーーーーー

2行目の後に「hello」を追加(その2)

seq 3 | sed '2a\
hello'

実行結果

1
2
hello
3

ーーーーーーーーーーーーーーーーーーーーーーーーー

3行目をXに置換後、2行目の後に「hello」と「world」を追加

seq 3 | sed '2a\
hello\
world
3s/./X/'

実行結果

1
2
hello
world
X

ーーーーーーーーーーーーーーーーーーーーーーーーー

2行目の後に「hello」を追加(その3)

seq 3 | sed -e '2a\' -e hello

実行結果

1
2
hello
3

ーーーーーーーーーーーーーーーーーーーーーーーーー

2行目の後に「hello」を追加(その4)

VAR="hello"
seq 3 | sed -e '2a\' -e "$VAR"

実行結果

1
2
hello
3

ーーーーーーーーーーーーーーーーーーーーーーーーー

2行目の前に「hello」を追加(その1)

seq 3 | sed '2i hello'

実行結果

1
hello
2
3

ーーーーーーーーーーーーーーーーーーーーーーーーー

2行目の前に「hello」を追加(その2)

seq 3 | sed '2i\
hello'

実行結果

1
hello
2
3

ーーーーーーーーーーーーーーーーーーーーーーーーー

2行目の前に「hello」と「world」を追加し、1文字の文字をXに置換

seq 3 | sed '2i\
hello\
world
s/./X/'

実行結果

X
hello
world
X
X

ーーーーーーーーーーーーーーーーーーーーーーーーー

2行目の前に「hello」を追加(その3)

seq 3 | sed -e '2i\' -e hello

実行結果

1
hello
2
3

ーーーーーーーーーーーーーーーーーーーーーーーーー

2行目の前に「hello」を追加(その4)

VAR="hello"
seq 3 | sed -e '2i\' -e "$VAR"

実行結果

1
hello
2
3

ーーーーーーーーーーーーーーーーーーーーーーーーー

2行目から9行目を「hello」に置換

seq 10 | sed '2,9c hello'

実行結果

1
hello
10

ーーーーーーーーーーーーーーーーーーーーーーーーー

2行目から4行目を「hello」と「world」に置換

seq 5 | sed '2,4c\
hello\
world'

実行結果

1
hello
world
5

ーーーーーーーーーーーーーーーーーーーーーーーーー

2行目を「hello」に置換し、1文字の行をXに置換

seq 3 | sed '2c\
hello
s/./X/'

helloがhなどの1文字でも置換対象にはならない

実行結果

X
hello
X

ーーーーーーーーーーーーーーーーーーーーーーーーー

2行目を「hello」に置換(その1)

seq 3 | sed -e '2c\' -e hello

実行結果

1
hello
3

ーーーーーーーーーーーーーーーーーーーーーーーーー

2行目を「hello」に置換(その2)

VAR="hello"
seq 3 | sed -e '2c\' -e "$VAR"

実行結果

1
hello
3

ーーーーーーーーーーーーーーーーーーーーーーーーー

現在の入力行番号を(末尾に改行を付けて)出力

printf '%s\n' aaa bbb ccc | sed =

実行結果

1
aaa
2
bbb
3
ccc

ーーーーーーーーーーーーーーーーーーーーーーーーー

ファイルを読み込み、指定した番号の後に追加

実行に必要なファイル作成

mkdir -p ~/sed_rtc/Less-Frequently-Used-Commands
cd ~/sed_rtc/Less-Frequently-Used-Commands
cat << 'EOF' > filename.txt; cat filename.txt;
running terminal commands
EOF
seq 3 | sed '2rfilename.txt'

実行結果

1
2
running terminal commands
3

ーーーーーーーーーーーーーーーーーーーーーーーーー

3.7 Commands Specific to GNU sed

引用元のURL記載

https://www.gnu.org/software/sed/manual/html_node/Extended-Commands.html#Extended-Commands

何も出力しない

echo -e "hello\nworld" | sed '
:eat
$d
N
g
b eat
'

$dを無くして実行しても空行が出力される

実行結果

ーーーーーーーーーーーーーーーーーーーーーーーーー

3.8 Multiple commands syntax

引用元のURL記載

https://www.gnu.org/software/sed/manual/html_node/Multiple-commands-syntax.html#Multiple-commands-syntax

1、3、5行目を削除(その1)

seq 6 | sed '1d
3d
5d'

コマンドの挙動を簡単に確認できた

実行結果

2
4
6

ーーーーーーーーーーーーーーーーーーーーーーーーー

1、3、5行目を削除(その2)

seq 6 | sed -e 1d -e 3d -e 5d

コマンドの挙動を簡単に確認できた

実行結果

2
4
6

ーーーーーーーーーーーーーーーーーーーーーーーーー

1、3、5行目を削除(その3)

seq 6 | sed '1d;3d;5d'

コマンドの挙動を簡単に確認できた

実行結果

2
4
6

ーーーーーーーーーーーーーーーーーーーーーーーーー

1、3行目を削除

seq 4 | sed '{1d;3d}'

コマンドの挙動を簡単に確認できた

実行結果

2
4

ーーーーーーーーーーーーーーーーーーーーーーーーー

1、3、5行目を削除(その4)

seq 6 | sed '{1d;3d};5d'

コマンドの挙動を簡単に確認できた

実行結果

2
4
6

ーーーーーーーーーーーーーーーーーーーーーーーーー

2行目の先頭に=を追加して3行目を削除(その1)

seq 3 | sed '/1/b x ; s/^/=/ ; :x ; 3d'

コマンドの挙動を簡単に確認できた

実行結果

1
=2

ーーーーーーーーーーーーーーーーーーーーーーーーー

2行目の先頭に=を追加して3行目を削除(その2)

seq 3 | sed -e '/1/bx' -e 's/^/=/' -e ':x' -e '3d'

コマンドの挙動を簡単に確認できた

実行結果

1
=2

ーーーーーーーーーーーーーーーーーーーーーーーーー

意図しない結果

seq 2 | sed '1aHello ; 2d'

コマンドの挙動を簡単に確認できた

実行結果

1
Hello ; 2d
2

ーーーーーーーーーーーーーーーーーーーーーーーーー

2行目を削除して1行目の後にHelloを追加(その1)

seq 2 | sed -e 1aHello -e 2d

コマンドの挙動を簡単に確認できた

実行結果

1
Hello

ーーーーーーーーーーーーーーーーーーーーーーーーー

2行目を削除して1行目の後にHelloを追加(その2)

seq 2 | sed '1aHello
2d'

コマンドの挙動を簡単に確認できた

実行結果

1
Hello

ーーーーーーーーーーーーーーーーーーーーーーーーー

2行目を削除して1行目の後にHelloを追加(その3)

seq 2 | sed '1a\
Hello
2d'

コマンドの挙動を簡単に確認できた

実行結果

1
Hello

ーーーーーーーーーーーーーーーーーーーーーーーーー

1~3を出力

seq 3 | sed '# this is a comment ; 2d'

コマンドの挙動を簡単に確認できた

実行結果

1
2
3

ーーーーーーーーーーーーーーーーーーーーーーーーー

1~3を出力して2行目を削除

seq 3 | sed '# this is a comment
2d'

コマンドの挙動を簡単に確認できた

実行結果

1
3

ーーーーーーーーーーーーーーーーーーーーーーーーー

「hello.txt ; 2d」ファイルを作成して内容を出力

実行に必要なファイル作成

mkdir -p ~/sed_rtc/Multiple-commands-syntax
cd ~/sed_rtc/Multiple-commands-syntax

機能確認コマンド

seq 2 | sed '1w hello.txt ; 2d'
ls -log
cat 'hello.txt ; 2d'

ファイルが作成されるので専用のフォルダを作成して管理する必要があった

実行結果

1
2

-rw-r--r--  1   2  3 15 22:18 hello.txt ; 2d

1

ーーーーーーーーーーーーーーーーーーーーーーーーー

「hello.txt ; N」ファイルを読み込んでxを表示

seq 3 | sed '# this is a comment
2d'

hello.txt ; Nファイルが無くてもエラーが出ずに実行される

実行結果

x

ーーーーーーーーーーーーーーーーーーーーーーーーー

touchコマンドでfoo#barとfooファイルを作成

実行に必要なファイル作成

mkdir -p ~/sed_rtc/Multiple-commands-syntax
cd ~/sed_rtc/Multiple-commands-syntax

機能確認コマンド

echo a | sed '1e touch foo#bar'
cat foo#bar
ls -1
echo a | sed '1e touch foo ; s/a/b/'
cat foo

ファイルが作成されるので専用のフォルダを作成して管理する必要があった
foo#barファイルとfooファイルを作成するがファイル内の内容は何もない

実行結果

a

foo#bar

sh: s/a/b/: No such file or directory
a

ーーーーーーーーーーーーーーーーーーーーーーーーー

touchコマンドでfoo#barとfooファイルを作成

実行に必要なファイル作成

mkdir -p ~/sed_rtc/Multiple-commands-syntax
cd ~/sed_rtc/Multiple-commands-syntax

機能確認コマンド

echo a | sed 's/a/b/w1.txt#foo'
ls -1
cat 1.txt#foo

ファイルが作成されるので専用のフォルダを作成して管理する必要があった

実行結果

b

1.txt#foo

b

ーーーーーーーーーーーーーーーーーーーーーーーーー

4.1 Addresses overview

引用元のURL記載

https://www.gnu.org/software/sed/manual/html_node/Addresses-overview.html#Addresses-overview

2行目のhelloをworldに置換

実行に必要なファイル作成

mkdir -p ~/sed_rtc/Addresses-overview
cd ~/sed_rtc/Addresses-overview
cat << 'EOF' > input41.txt; cat input41.txt;
hello world
hello hello
world hello
EOF

機能確認コマンド

sed '2s/hello/world/' input41.txt > output411.txt
cat output411.txt

144行目は長いので2行目に変更
ファイルを作成する必要あり

実行結果

hello world
world hello
world hello

ーーーーーーーーーーーーーーーーーーーーーーーーー

全ての行で初めに該当したhelloをworldに置換

実行に必要なファイル作成

mkdir -p ~/sed_rtc/Addresses-overview
cd ~/sed_rtc/Addresses-overview
cat << 'EOF' > input41.txt; cat input41.txt;
hello world
hello hello
world hello
EOF

機能確認コマンド

sed 's/hello/world/' input41.txt > output412.txt
cat output412.txt

ファイルを作成する必要あり

実行結果

world world
world hello
world world

ーーーーーーーーーーーーーーーーーーーーーーーーー

「apple」という単語を含む行のみ「hello」を「world」に置換

実行に必要なファイル作成

mkdir -p ~/sed_rtc/Addresses-overview
cd ~/sed_rtc/Addresses-overview
cat << 'EOF' > input413.txt; cat input413.txt;
hello world
hello hello apple
world hello apple
EOF

機能確認コマンド

sed '/apple/s/hello/world/' input413.txt > output413.txt
cat output413.txt

ファイルを作成する必要あり

実行結果

hello world
world hello apple
world world apple

ーーーーーーーーーーーーーーーーーーーーーーーーー

2行目から4行目で「hello」を「world」に置換

実行に必要なファイル作成

mkdir -p ~/sed_rtc/Addresses-overview
cd ~/sed_rtc/Addresses-overview
cat << 'EOF' > input414.txt; cat input414.txt;
hello world
hello hello
world hello
world world
hello hello hello
EOF

機能確認コマンド

sed '2,4s/hello/world/' input414.txt > output414.txt
cat output414.txt

ファイルを作成する必要あり

実行結果

hello world
world hello
world world
world world
hello hello hello

ーーーーーーーーーーーーーーーーーーーーーーーーー

「apple」を含まない行のみ「hello」を「world」に置換

実行に必要なファイル作成

mkdir -p ~/sed_rtc/Addresses-overview
cd ~/sed_rtc/Addresses-overview
cat << 'EOF' > input415.txt; cat input415.txt;
hello world
hello hello apple
world hello apple
EOF

機能確認コマンド

sed '/apple/!s/hello/world/' input415.txt > output415.txt
cat output415.txt

ファイルを作成する必要あり

実行結果

hello world
world hello apple
world world apple

ーーーーーーーーーーーーーーーーーーーーーーーーー

1行目と5行目の「hello」を「world」に置換

実行に必要なファイル作成

mkdir -p ~/sed_rtc/Addresses-overview
cd ~/sed_rtc/Addresses-overview
cat << 'EOF' > input416.txt; cat input416.txt;
hello world
hello hello
world hello
world world
hello hello hello
EOF

機能確認コマンド

sed '2,4!s/hello/world/' input416.txt > output416.txt
cat output416.txt

ファイルを作成する必要あり

実行結果

hello world
world hello
world world
world world
hello hello hello

ーーーーーーーーーーーーーーーーーーーーーーーーー

4.2 Selecting lines by numbers

引用元のURL記載

https://www.gnu.org/software/sed/manual/html_node/Numeric-Addresses.html#Numeric-Addresses

4ごとに表示

seq 10 | sed -n '0~4p'

挙動を確認しやすい

実行結果

4
8

ーーーーーーーーーーーーーーーーーーーーーーーーー

1から3ごとに表示

seq 10 | sed -n '1~3p'

挙動を確認しやすい

実行結果

1
4
7
10

ーーーーーーーーーーーーーーーーーーーーーーーーー

4.3 selecting lines by text matching

引用元のURL記載

https://www.gnu.org/software/sed/manual/html_node/Regexp-Addresses.html#Regexp-Addresses

末尾が 「bash」 で終わる行を出力

sed -n '/bash$/p' /etc/passwd

/etc/passwdというパスが存在するのか心配になった

実行結果

~:/bin/bash

ーーーーーーーーーーーーーーーーーーーーーーーーー

/home/alice/documents/で始まる行を出力(その1)

echo -e "/home/alice/documents/\n/home/alice/desktop/test\n/home/alice/documents" | sed -n '/^\/home\/alice\/documents\//p'

文字列を自身で用意する必要があった

実行結果

/home/alice/documents/

ーーーーーーーーーーーーーーーーーーーーーーーーー

/home/alice/documents/で始まる行を出力(その2)

echo -e "/home/alice/documents/\n/home/alice/desktop/test\n/home/alice/documents" | sed -n '\%^/home/alice/documents/%p'

文字列を自身で用意する必要があった

実行結果

/home/alice/documents/

ーーーーーーーーーーーーーーーーーーーーーーーーー

bの行を削除して表示

printf "%s\n" a b c | sed '/b/Id'

挙動確認しやすかった

実行結果

a
c

ーーーーーーーーーーーーーーーーーーーーーーーーー

bの前にdを追加

printf "%s\n" a b c | sed '/b/id'

挙動を確認しやすかった

実行結果

a
d
b
c

ーーーーーーーーーーーーーーーーーーーーーーーーー

2の行をXに置換して数字の行を表示

seq 3 | sed -n 's/2/X/ ; /[0-9]/p'

挙動を確認しやすかった

実行結果

1
3

ーーーーーーーーーーーーーーーーーーーーーーーーー

末尾が 「bash」 で終わる行を出力(その2)

grep 'bash$' /etc/passwd

その1の後に記載して欲しかった

実行結果

~:/bin/bash

ーーーーーーーーーーーーーーーーーーーーーーーーー

末尾が 「bash」 で終わる行を出力(その3)

awk -F: '$7 == "/bin/bash"' /etc/passwd

その1の後に記載して欲しかった

実行結果

~:/bin/bash

ーーーーーーーーーーーーーーーーーーーーーーーーー

4.4 Range Addresses

引用元のURL記載

https://www.gnu.org/software/sed/manual/html_node/Range-Addresses.html#Range-Addresses

4~6を表示

seq 10 | sed -n '4,6p'

挙動を確認しやすかった

実行結果

4
5
6

ーーーーーーーーーーーーーーーーーーーーーーーーー

4と5を表示

seq 10 | sed -n '4,/[0-9]/p'

挙動を確認しやすかった
正規表現を2番目のアドレスに指定したときの挙動がわかりづらかった

実行結果

4
5

ーーーーーーーーーーーーーーーーーーーーーーーーー

4を表示

seq 10 | sed -n '4,1p'

挙動を確認しやすかった

実行結果

4

ーーーーーーーーーーーーーーーーーーーーーーーーー

入力行とその次の行を表示

seq 10 | sed -n '1,/[0-9]/p'

挙動を確認しやすかった

実行結果

1
2

ーーーーーーーーーーーーーーーーーーーーーーーーー

入力行のみを表示

seq 10 | sed -n '0,/[0-9]/p'

挙動を確認しやすかった

実行結果

1

ーーーーーーーーーーーーーーーーーーーーーーーーー

6に続く2行を表示

seq 10 | sed -n '6,+2p'

挙動を確認しやすかった

実行結果

6
7
8

ーーーーーーーーーーーーーーーーーーーーーーーーー

6から4の倍数までの数字を表示

seq 10 | sed -n '6,~4p'

挙動を確認しやすかった

実行結果

6
7
8

ーーーーーーーーーーーーーーーーーーーーーーーーー

5.1 Overview of regular expression in sed

引用元のURL記載

https://www.gnu.org/software/sed/manual/html_node/Regular-Expressions-Overview.html#Regular-Expressions-Overview

「hello」を含む行を出力

echo -e "hello world\nworld hello\nworld world\nsed world" | sed -n '/hello/p'

例文を考える必要があった

実行結果

hello world
world hello

ーーーーーーーーーーーーーーーーーーーーーーーーー

grepコマンドと同じ

echo -e "hello world\nworld hello\nworld world\nsed world" | grep "hello"

例文を考える必要があった

実行結果

hello world
world hello

ーーーーーーーーーーーーーーーーーーーーーーーーー

1文字目が「b」、2文字目が任意の1文字、3文字目が「d」を含む行を出力

printf "%s\n" abode bad bed bit bid byte body | sed -n '/^b.d/p'

挙動を確認しやすかった

実行結果

bad
bed
bid
body

ーーーーーーーーーーーーーーーーーーーーーーーーー

5.2 Basic (BRE) and extended (ERE) regular expression

引用元のURL記載

https://www.gnu.org/software/sed/manual/html_node/BRE-vs-ERE.html#BRE-vs-ERE

「a+b」を含む行を出力(basic regular expression)

実行に必要なファイル作成

mkdir -p ~/sed_rtc/Basic-and-extended-regular-expression
cd ~/sed_rtc/Basic-and-extended-regular-expression

機能確認コマンド

echo 'a+b=c' > foo511
sed -n '/a+b/p' foo511

ファイルを保存するディレクトリを作成する必要があった

実行結果

a+b=c

ーーーーーーーーーーーーーーーーーーーーーーーーー

「a+b」を含む行を出力(extended regular expression)

実行に必要なファイル作成

mkdir -p ~/sed_rtc/Basic-and-extended-regular-expression
cd ~/sed_rtc/Basic-and-extended-regular-expression

機能確認コマンド

echo 'a+b=c' > foo512
sed -E -n '/a\+b/p' foo512

ファイルを保存するディレクトリを作成する必要があった

実行結果

a+b=c

ーーーーーーーーーーーーーーーーーーーーーーーーー

aを1つ以上とbを含む行を出力(basic regular expression)

実行に必要なファイル作成

mkdir -p ~/sed_rtc/Basic-and-extended-regular-expression
cd ~/sed_rtc/Basic-and-extended-regular-expression

機能確認コマンド

echo aab > foo513
sed -n '/a\+b/p' foo513

ファイルを保存するディレクトリを作成する必要があった

実行結果

aab

ーーーーーーーーーーーーーーーーーーーーーーーーー

aを1つ以上とbを含む行を出力(extended regular expression)

実行に必要なファイル作成

mkdir -p ~/sed_rtc/Basic-and-extended-regular-expression
cd ~/sed_rtc/Basic-and-extended-regular-expression

機能確認コマンド

echo aab > foo514
sed -E -n '/a+b/p' foo514

ファイルを保存するディレクトリを作成する必要があった

実行結果

aab

ーーーーーーーーーーーーーーーーーーーーーーーーー

5.5 Character Classes and Bracket Expressions

引用元のURL記載

https://www.gnu.org/software/sed/manual/html_node/Character-Classes-and-Bracket-Expressions.html#Character-Classes-and-Bracket-Expressions

greyとgrayをblueに置換

echo -e "gray\ngriy\ngrey\ngriygray" | sed 's/gr[ae]y/blue/'

例文を考える必要があった

実行結果

blue
griy
blue
griyblue

ーーーーーーーーーーーーーーーーーーーーーーーーー

0~9の文字を1文字だけ置換

echo -e "1\n123\n93" | sed 's/[[:digit:]]/X/'

例文が複数あると分かりやすかった

実行結果

X
X23
X3

ーーーーーーーーーーーーーーーーーーーーーーーーー

現在使用できない構文

echo 1 | sed 's/[:digit:]/X/'

挙動がわかりやすかった

実行結果

sed: character class syntax is [[:space:]], not [:space:]

ーーーーーーーーーーーーーーーーーーーーーーーーー

5.6 regular expression extensions

引用元のURL記載

https://www.gnu.org/software/sed/manual/html_node/regexp-extensions.html#regexp-extensions

英字、数字、_(アンダースコア)を置換

echo "abc %-_= def." | sed 's/\w/X/g'

挙動は分かりやすかったが_(アンダースコア)が含まれていなかった

実行結果

XXX %-X= XXX.

ーーーーーーーーーーーーーーーーーーーーーーーーー

英字、数字、_(アンダースコア)に一致しない文字を置換

echo "abc %-_= def." | sed 's/\W/X/g'

挙動は分かりやすかったが_(アンダースコア)が含まれていなかった

実行結果

abcXXX_XXdefX

ーーーーーーーーーーーーーーーーーーーーーーーーー

2つの文字の間から見て左右に英数字とそれ以外が並んでいる場合にXを追加

echo "abc %-_= def." | sed 's/\b/X/g'

_(アンダースコア)が含まれていなかった
挙動の説明がしにくかった

実行結果

XabcX %-X_X= XdefX.

ーーーーーーーーーーーーーーーーーーーーーーーーー

2つの文字の間から見て左右に英数字同士、それ以外同士が並んでいる場合にXを追加

echo "abc %-_= def." | sed 's/\B/X/g'

_(アンダースコア)が含まれていなかった
挙動の説明がしにくかった

実行結果

aXbXc X%X-_=X dXeXf.X

ーーーーーーーーーーーーーーーーーーーーーーーーー

空白文字(スペースとタブ)をXに置換

echo "abc %-_= def." | sed 's/\s/X/g'

_(アンダースコア)が含まれていなかった

実行結果

abcX%-_=Xdef.

ーーーーーーーーーーーーーーーーーーーーーーーーー

空白以外の文字をXに置換

echo "abc %-_= def." | sed 's/\S/X/g'

_(アンダースコア)が含まれていなかった

実行結果

XXX XXXX XXXX

ーーーーーーーーーーーーーーーーーーーーーーーーー

英数字が始まる文字の前にXを追加

echo "abc %-_= def." | sed 's/\</X/g'

_(アンダースコア)が含まれていなかった
英数字とそれ以外の説明が欲しかった

実行結果

Xabc %-X_= Xdef.

ーーーーーーーーーーーーーーーーーーーーーーーーー

英数字が終わる文字の後にXを追加

echo "abc %-_= def." | sed 's/\>/X/g'

_(アンダースコア)が含まれていなかった
英数字とそれ以外の説明が欲しかった

実行結果

abcX %-_X= defX.

ーーーーーーーーーーーーーーーーーーーーーーーーー

各行の先頭にXを追加

printf "a\nb\nc\n" | sed 'N;N;s/^/X/gm'

挙動を確認しやすかった

実行結果

Xa
Xb
Xc

ーーーーーーーーーーーーーーーーーーーーーーーーー

1行目の先頭にXを追加

printf "a\nb\nc\n" | sed 'N;N;s/\`/X/gm'

挙動を確認しやすかった

実行結果

Xa
b
c

ーーーーーーーーーーーーーーーーーーーーーーーーー

5.7 Back-references and Subexpressions

引用元のURL記載

https://www.gnu.org/software/sed/manual/html_node/Back_002dreferences-and-Subexpressions.html#Back_002dreferences-and-Subexpressions

任意の文字、「o」、さらに最初の文字と同じ文字が続く単語を表示

sed -E -n '/^(.)o\1$/p' /usr/share/dict/words

挙動は分かりやすかった

実行結果

bob
dod
gog
non
pop
tot
wow
yoy

ーーーーーーーーーーーーーーーーーーーーーーーーー

先頭が任意の3文字でそれら文字を逆順にした6文字の文字を表示

sed -E -n '/^(.)(.)(.)\3\2\1$/p' /usr/share/dict/words

挙動は分かりやすかった

実行結果

degged
hallah
kakkak
redder
retter
tebbet
terret

ーーーーーーーーーーーーーーーーーーーーーーーーー

スペースで区切られた2つの単語を異なる順序で出力

echo "James Bond" | sed -E 's/(.*) (.*)/The name is \2, \1 \2./'

挙動は分かりやすかった

実行結果

The name is Bond, James Bond.

ーーーーーーーーーーーーーーーーーーーーーーーーー

5.8 Escape Sequences – specifying special characters

引用元のURL記載

https://www.gnu.org/software/sed/manual/html_node/Escapes.html#Escapes

行の先頭に「^」を追加(その1)

echo -e 'a^c\n^abc' | sed 's/^/b/'

例を追加して確認する必要があった

実行結果

ba^c
b^abc

ーーーーーーーーーーーーーーーーーーーーーーーーー

行の先頭に「^」を追加(その2)

echo -e 'a^c\n^abc' | sed 's/\x5e/b/'

例を追加して確認する必要があった

実行結果

ba^c
b^abc

ーーーーーーーーーーーーーーーーーーーーーーーーー

「a」をxに置換(その1)

echo abc | sed 's/[a]/x/'

Xbcではなくxbcだった

実行結果

xbc

ーーーーーーーーーーーーーーーーーーーーーーーーー

「a」をxに置換(その2)

echo abc | sed 's/\x5ba\x5d/x/'

Xbcではなくxbcだった

実行結果

xbc

ーーーーーーーーーーーーーーーーーーーーーーーーー

^をbに置換(予期せぬ挙動)

echo -e 'a^c\n\x5e\n\\\x5e' | sed 's/\\\x5e/b/'

\\\x5eがどうなるから出力がa^cになるという説明が欲しかった
\\\が\\\→\\→\になり、\x5eをbに置換する
複数の例が欲しかった

実行結果

a^c
^
b

ーーーーーーーーーーーーーーーーーーーーーーーーー

7.2 Centering Lines
echo -e “hello\ngnu\nsed\nworld” | sed -f test.sed

7.3 Increment a Number
echo “13” | sed -f test2.sed

7.4 Rename Files to Lower Case
./test3.sh *同じ階層の大文字を小文字化
./test3.sh -Rフォルダ名を指定してフォルダ以下の大文字を小文字化

7.5 Print bash Environment
./test5.shシェルの情報が表示される

結論(conclusion)

  1. 「公式ドキュメントには他にも多くの例があるので、ぜひ参照してください」
  2. リンクの再提示
  3. 次は〇〇コマンドの解説記事を書きます
  4. 次へのアクションがサイト内の回遊率を高める
  5. 置換対象の文字列がはじめから用意されているとコマンドの挙動を確認しやすい
  6. input.txtなどのファイルを扱う場合は、ファイルを作成するコマンドを一緒に記載されていると挙動を確認しやすい
  7. シェルスクリプトファイルにする必要があるコードは、chmod 744 test.shなど実行するまでのコマンドが併記されているとよい
  8. 正規表現がたくさん出てくるが、実行可能なコマンドが多いので勉強になる

コメントを残す

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

©︎ 2025-2026 todo