骨子案
はじめに(Introduction)
- 自己紹介
- こんにちは、running terminal commandsのターナです
- 記事概要説明
- この記事では、GNUプロジェクトのsedコマンドについて公式サイトに記載されているコマンドを簡単に実行できる形で書かれています
- sedはファイルや標準入力に対してテキスト変換などのデータ処理を行うために使用されます
由来は、ストリームエディタを意味する英語の「stream editor」です
入力の読み取り単位は行で、sedスクリプトという命令文に従ってテキスト変換を行います
出力の単位は行です
照合ルールに従って場合分けするフィルタという認識で良い
一括で定形の処理をおこなう場合に威力を発揮するものは、大量のテキストファイルです
正規表現に対応しており、ある条件の範囲内の文字列を探し出して処理することができます - 挙動を簡単に確認するために一部のコマンドを改変
- 初心者の方でも実行可能
- 引用元紹介
- 引用元のGNU sedです
- https://www.gnu.org/software/sed/
- メリット紹介
- 公式マニュアルの難解な例を、実際に動かせる形で整理
- コードをクリックすることでクリップボードにコピーされるので、ターミナルにペーストするだけで完結
- 「sedの学習を始めたばかりの人」向け
- 「逆引きリファレンスとして使いたい人」向け
- BSDのsedかGNUのsedかを明確化
2.1 Overview
引用元のURL記載
https://www.gnu.org/software/sed/manual/html_node/Overview.html#Overview
input.txt ファイル内の「hello」を「world」に置換するパターンの紹介
実行に必要なファイル作成
機能確認コマンド
ファイルをどこに作成するか
読み込むファイルの作成
ファイルに出力した後の内容を表示するコマンド
実行結果
world world
world hello
ーーーーーーーーーーーーーーーーーーーーーーーーー
input.txt ファイル内の「hello」を「world」に置換
実行に必要なファイル作成
機能確認コマンド
実行結果
world world
world hello
ーーーーーーーーーーーーーーーーーーーーーーーーー
file2_1_3.txt を編集し出力
実行に必要なファイル作成
機能確認コマンド
実行結果
world world
world hello
ーーーーーーーーーーーーーーーーーーーーーーーーー
入力ファイルの2行目のみを出力
実行に必要なファイル作成
機能確認コマンド
実行結果
hello hello
ーーーーーーーーーーーーーーーーーーーーーーーーー
3つの入力ファイルの1行目を出力
実行に必要なファイル作成
機能確認コマンド
実行結果
hello two
hello three
ーーーーーーーーーーーーーーーーーーーーーーーーー
3つの入力ファイルの1行目を出力
実行に必要なファイル作成
機能確認コマンド
実行結果
world world
world hello
ーーーーーーーーーーーーーーーーーーーーーーーーー
2.2 Command-Line Options
引用元のURL記載
sedのバージョンと著作権表示を出力
実行結果
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>.
ーーーーーーーーーーーーーーーーーーーーーーーーー
コマンドラインオプションとバグ報告先アドレスを簡潔にまとめた使用方法メッセージを表示
実行結果
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>.
ーーーーーーーーーーーーーーーーーーーーーーーーー
自動出力がを無効化
実行結果
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プログラムを正規形式で出力し、プログラムの実行内容を注釈として追加してください。
実行結果
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プログラムを正規形式で出力し、プログラムの実行内容を注釈として追加してください。
実行に必要なファイル作成
機能確認コマンド
実行結果
nothing
ーーーーーーーーーーーーーーーーーーーーーーーーー
2.3 Exit status
qとQを使用して、カスタム終了コード値で sed を終了
実行結果
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行目を削除
実行に必要なファイル作成
機能確認コマンド
実行結果
hello hello
world foo
ーーーーーーーーーーーーーーーーーーーーーーーーー
「foo」で始まる行が見つかるまで、入力されたすべての文字列を出力
実行結果
hello hello
hello foo
hello world
foo hello
ーーーーーーーーーーーーーーーーーーーーーーーーー
正規表現 /^foo/ に一致する行を削除し、文字列「hello」のすべての出現箇所を「world」に置換
実行結果
world hello
world foo
world world
world foo
ーーーーーーーーーーーーーーーーーーーーーーーーー
3.3 The s Command
引用元のURL記載
文字列の「a-b-」をキャプチャとupperケースを使用して置換
\(b\?)-は、-かb-に該当するかどうか
実行結果
hello hello
world foo
ーーーーーーーーーーーーーーーーーーーーーーーーー
文字列の「a-b-」をキャプチャとupperケースを使用して置換
\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行目を出力した後に停止
実行結果
1
2
ーーーーーーーーーーーーーーーーーーーーーーーーー
2行目の入力を削除
実行結果
1
3
ーーーーーーーーーーーーーーーーーーーーーーーーー
2行目の入力のみを出力
実行結果
2
ーーーーーーーーーーーーーーーーーーーーーーーーー
3行ごとに置換(その1)
実行結果
1
2
x
4
5
x
ーーーーーーーーーーーーーーーーーーーーーーーーー
3行ごとに置換(その2)
実行結果
1
2
x
4
5
x
ーーーーーーーーーーーーーーーーーーーーーーーーー
置換してから、2行目の入力を出力
実行結果
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」に置換
同じ文字数である必要
実行結果
74llo worl3
ーーーーーーーーーーーーーーーーーーーーーーーーー
2行目の後に「hello」を追加(その1)
実行結果
1
2
hello
3
ーーーーーーーーーーーーーーーーーーーーーーーーー
2行目の後に「hello」を追加(その2)
実行結果
1
2
hello
3
ーーーーーーーーーーーーーーーーーーーーーーーーー
3行目をXに置換後、2行目の後に「hello」と「world」を追加
実行結果
1
2
hello
world
X
ーーーーーーーーーーーーーーーーーーーーーーーーー
2行目の後に「hello」を追加(その3)
実行結果
1
2
hello
3
ーーーーーーーーーーーーーーーーーーーーーーーーー
2行目の後に「hello」を追加(その4)
実行結果
1
2
hello
3
ーーーーーーーーーーーーーーーーーーーーーーーーー
2行目の前に「hello」を追加(その1)
実行結果
1
hello
2
3
ーーーーーーーーーーーーーーーーーーーーーーーーー
2行目の前に「hello」を追加(その2)
実行結果
1
hello
2
3
ーーーーーーーーーーーーーーーーーーーーーーーーー
2行目の前に「hello」と「world」を追加し、1文字の文字をXに置換
実行結果
X
hello
world
X
X
ーーーーーーーーーーーーーーーーーーーーーーーーー
2行目の前に「hello」を追加(その3)
実行結果
1
hello
2
3
ーーーーーーーーーーーーーーーーーーーーーーーーー
2行目の前に「hello」を追加(その4)
実行結果
1
hello
2
3
ーーーーーーーーーーーーーーーーーーーーーーーーー
2行目から9行目を「hello」に置換
実行結果
1
hello
10
ーーーーーーーーーーーーーーーーーーーーーーーーー
2行目から4行目を「hello」と「world」に置換
実行結果
1
hello
world
5
ーーーーーーーーーーーーーーーーーーーーーーーーー
2行目を「hello」に置換し、1文字の行をXに置換
helloがhなどの1文字でも置換対象にはならない
実行結果
X
hello
X
ーーーーーーーーーーーーーーーーーーーーーーーーー
2行目を「hello」に置換(その1)
実行結果
1
hello
3
ーーーーーーーーーーーーーーーーーーーーーーーーー
2行目を「hello」に置換(その2)
実行結果
1
hello
3
ーーーーーーーーーーーーーーーーーーーーーーーーー
現在の入力行番号を(末尾に改行を付けて)出力
実行結果
1
aaa
2
bbb
3
ccc
ーーーーーーーーーーーーーーーーーーーーーーーーー
ファイルを読み込み、指定した番号の後に追加
実行に必要なファイル作成
実行結果
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
何も出力しない
$dを無くして実行しても空行が出力される
実行結果
ーーーーーーーーーーーーーーーーーーーーーーーーー
3.8 Multiple commands syntax
引用元のURL記載
1、3、5行目を削除(その1)
コマンドの挙動を簡単に確認できた
実行結果
2
4
6
ーーーーーーーーーーーーーーーーーーーーーーーーー
1、3、5行目を削除(その2)
コマンドの挙動を簡単に確認できた
実行結果
2
4
6
ーーーーーーーーーーーーーーーーーーーーーーーーー
1、3、5行目を削除(その3)
コマンドの挙動を簡単に確認できた
実行結果
2
4
6
ーーーーーーーーーーーーーーーーーーーーーーーーー
1、3行目を削除
コマンドの挙動を簡単に確認できた
実行結果
2
4
ーーーーーーーーーーーーーーーーーーーーーーーーー
1、3、5行目を削除(その4)
コマンドの挙動を簡単に確認できた
実行結果
2
4
6
ーーーーーーーーーーーーーーーーーーーーーーーーー
2行目の先頭に=を追加して3行目を削除(その1)
コマンドの挙動を簡単に確認できた
実行結果
1
=2
ーーーーーーーーーーーーーーーーーーーーーーーーー
2行目の先頭に=を追加して3行目を削除(その2)
コマンドの挙動を簡単に確認できた
実行結果
1
=2
ーーーーーーーーーーーーーーーーーーーーーーーーー
意図しない結果
コマンドの挙動を簡単に確認できた
実行結果
1
Hello ; 2d
2
ーーーーーーーーーーーーーーーーーーーーーーーーー
2行目を削除して1行目の後にHelloを追加(その1)
コマンドの挙動を簡単に確認できた
実行結果
1
Hello
ーーーーーーーーーーーーーーーーーーーーーーーーー
2行目を削除して1行目の後にHelloを追加(その2)
コマンドの挙動を簡単に確認できた
実行結果
1
Hello
ーーーーーーーーーーーーーーーーーーーーーーーーー
2行目を削除して1行目の後にHelloを追加(その3)
コマンドの挙動を簡単に確認できた
実行結果
1
Hello
ーーーーーーーーーーーーーーーーーーーーーーーーー
1~3を出力
コマンドの挙動を簡単に確認できた
実行結果
1
2
3
ーーーーーーーーーーーーーーーーーーーーーーーーー
1~3を出力して2行目を削除
コマンドの挙動を簡単に確認できた
実行結果
1
3
ーーーーーーーーーーーーーーーーーーーーーーーーー
「hello.txt ; 2d」ファイルを作成して内容を出力
実行に必要なファイル作成
機能確認コマンド
ファイルが作成されるので専用のフォルダを作成して管理する必要があった
実行結果
1
2
-rw-r--r-- 1 2 3 15 22:18 hello.txt ; 2d
1
ーーーーーーーーーーーーーーーーーーーーーーーーー
「hello.txt ; N」ファイルを読み込んでxを表示
hello.txt ; Nファイルが無くてもエラーが出ずに実行される
実行結果
x
ーーーーーーーーーーーーーーーーーーーーーーーーー
touchコマンドでfoo#barとfooファイルを作成
実行に必要なファイル作成
機能確認コマンド
ファイルが作成されるので専用のフォルダを作成して管理する必要があった
foo#barファイルとfooファイルを作成するがファイル内の内容は何もない
実行結果
a
foo#bar
sh: s/a/b/: No such file or directory
a
ーーーーーーーーーーーーーーーーーーーーーーーーー
touchコマンドでfoo#barと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に置換
実行に必要なファイル作成
機能確認コマンド
144行目は長いので2行目に変更
ファイルを作成する必要あり
実行結果
hello world
world hello
world hello
ーーーーーーーーーーーーーーーーーーーーーーーーー
全ての行で初めに該当したhelloをworldに置換
実行に必要なファイル作成
機能確認コマンド
ファイルを作成する必要あり
実行結果
world world
world hello
world world
ーーーーーーーーーーーーーーーーーーーーーーーーー
「apple」という単語を含む行のみ「hello」を「world」に置換
実行に必要なファイル作成
機能確認コマンド
ファイルを作成する必要あり
実行結果
hello world
world hello apple
world world apple
ーーーーーーーーーーーーーーーーーーーーーーーーー
2行目から4行目で「hello」を「world」に置換
実行に必要なファイル作成
機能確認コマンド
ファイルを作成する必要あり
実行結果
hello world
world hello
world world
world world
hello hello hello
ーーーーーーーーーーーーーーーーーーーーーーーーー
「apple」を含まない行のみ「hello」を「world」に置換
実行に必要なファイル作成
機能確認コマンド
ファイルを作成する必要あり
実行結果
hello world
world hello apple
world world apple
ーーーーーーーーーーーーーーーーーーーーーーーーー
1行目と5行目の「hello」を「world」に置換
実行に必要なファイル作成
機能確認コマンド
ファイルを作成する必要あり
実行結果
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ごとに表示
挙動を確認しやすい
実行結果
4
8
ーーーーーーーーーーーーーーーーーーーーーーーーー
1から3ごとに表示
挙動を確認しやすい
実行結果
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」 で終わる行を出力
/etc/passwdというパスが存在するのか心配になった
実行結果
~:/bin/bash
ーーーーーーーーーーーーーーーーーーーーーーーーー
/home/alice/documents/で始まる行を出力(その1)
文字列を自身で用意する必要があった
実行結果
/home/alice/documents/
ーーーーーーーーーーーーーーーーーーーーーーーーー
/home/alice/documents/で始まる行を出力(その2)
文字列を自身で用意する必要があった
実行結果
/home/alice/documents/
ーーーーーーーーーーーーーーーーーーーーーーーーー
bの行を削除して表示
挙動確認しやすかった
実行結果
a
c
ーーーーーーーーーーーーーーーーーーーーーーーーー
bの前にdを追加
挙動を確認しやすかった
実行結果
a
d
b
c
ーーーーーーーーーーーーーーーーーーーーーーーーー
2の行をXに置換して数字の行を表示
挙動を確認しやすかった
実行結果
1
3
ーーーーーーーーーーーーーーーーーーーーーーーーー
末尾が 「bash」 で終わる行を出力(その2)
その1の後に記載して欲しかった
実行結果
~:/bin/bash
ーーーーーーーーーーーーーーーーーーーーーーーーー
末尾が 「bash」 で終わる行を出力(その3)
その1の後に記載して欲しかった
実行結果
~:/bin/bash
ーーーーーーーーーーーーーーーーーーーーーーーーー
4.4 Range Addresses
引用元のURL記載
https://www.gnu.org/software/sed/manual/html_node/Range-Addresses.html#Range-Addresses
4~6を表示
挙動を確認しやすかった
実行結果
4
5
6
ーーーーーーーーーーーーーーーーーーーーーーーーー
4と5を表示
挙動を確認しやすかった
正規表現を2番目のアドレスに指定したときの挙動がわかりづらかった
実行結果
4
5
ーーーーーーーーーーーーーーーーーーーーーーーーー
4を表示
挙動を確認しやすかった
実行結果
4
ーーーーーーーーーーーーーーーーーーーーーーーーー
入力行とその次の行を表示
挙動を確認しやすかった
実行結果
1
2
ーーーーーーーーーーーーーーーーーーーーーーーーー
入力行のみを表示
挙動を確認しやすかった
実行結果
1
ーーーーーーーーーーーーーーーーーーーーーーーーー
6に続く2行を表示
挙動を確認しやすかった
実行結果
6
7
8
ーーーーーーーーーーーーーーーーーーーーーーーーー
6から4の倍数までの数字を表示
挙動を確認しやすかった
実行結果
6
7
8
ーーーーーーーーーーーーーーーーーーーーーーーーー
5.1 Overview of regular expression in sed
引用元のURL記載
「hello」を含む行を出力
例文を考える必要があった
実行結果
hello world
world hello
ーーーーーーーーーーーーーーーーーーーーーーーーー
grepコマンドと同じ
例文を考える必要があった
実行結果
hello world
world hello
ーーーーーーーーーーーーーーーーーーーーーーーーー
1文字目が「b」、2文字目が任意の1文字、3文字目が「d」を含む行を出力
挙動を確認しやすかった
実行結果
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)
実行に必要なファイル作成
機能確認コマンド
ファイルを保存するディレクトリを作成する必要があった
実行結果
a+b=c
ーーーーーーーーーーーーーーーーーーーーーーーーー
「a+b」を含む行を出力(extended regular expression)
実行に必要なファイル作成
機能確認コマンド
ファイルを保存するディレクトリを作成する必要があった
実行結果
a+b=c
ーーーーーーーーーーーーーーーーーーーーーーーーー
aを1つ以上とbを含む行を出力(basic regular expression)
実行に必要なファイル作成
機能確認コマンド
ファイルを保存するディレクトリを作成する必要があった
実行結果
aab
ーーーーーーーーーーーーーーーーーーーーーーーーー
aを1つ以上とbを含む行を出力(extended regular expression)
実行に必要なファイル作成
機能確認コマンド
ファイルを保存するディレクトリを作成する必要があった
実行結果
aab
ーーーーーーーーーーーーーーーーーーーーーーーーー
5.5 Character Classes and Bracket Expressions
引用元のURL記載
greyとgrayをblueに置換
例文を考える必要があった
実行結果
blue
griy
blue
griyblue
ーーーーーーーーーーーーーーーーーーーーーーーーー
0~9の文字を1文字だけ置換
例文が複数あると分かりやすかった
実行結果
X
X23
X3
ーーーーーーーーーーーーーーーーーーーーーーーーー
現在使用できない構文
挙動がわかりやすかった
実行結果
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
英字、数字、_(アンダースコア)を置換
挙動は分かりやすかったが_(アンダースコア)が含まれていなかった
実行結果
XXX %-X= XXX.
ーーーーーーーーーーーーーーーーーーーーーーーーー
英字、数字、_(アンダースコア)に一致しない文字を置換
挙動は分かりやすかったが_(アンダースコア)が含まれていなかった
実行結果
abcXXX_XXdefX
ーーーーーーーーーーーーーーーーーーーーーーーーー
2つの文字の間から見て左右に英数字とそれ以外が並んでいる場合にXを追加
_(アンダースコア)が含まれていなかった
挙動の説明がしにくかった
実行結果
XabcX %-X_X= XdefX.
ーーーーーーーーーーーーーーーーーーーーーーーーー
2つの文字の間から見て左右に英数字同士、それ以外同士が並んでいる場合にXを追加
_(アンダースコア)が含まれていなかった
挙動の説明がしにくかった
実行結果
aXbXc X%X-_=X dXeXf.X
ーーーーーーーーーーーーーーーーーーーーーーーーー
空白文字(スペースとタブ)をXに置換
_(アンダースコア)が含まれていなかった
実行結果
abcX%-_=Xdef.
ーーーーーーーーーーーーーーーーーーーーーーーーー
空白以外の文字をXに置換
_(アンダースコア)が含まれていなかった
実行結果
XXX XXXX XXXX
ーーーーーーーーーーーーーーーーーーーーーーーーー
英数字が始まる文字の前にXを追加
_(アンダースコア)が含まれていなかった
英数字とそれ以外の説明が欲しかった
実行結果
Xabc %-X_= Xdef.
ーーーーーーーーーーーーーーーーーーーーーーーーー
英数字が終わる文字の後にXを追加
_(アンダースコア)が含まれていなかった
英数字とそれ以外の説明が欲しかった
実行結果
abcX %-_X= defX.
ーーーーーーーーーーーーーーーーーーーーーーーーー
各行の先頭にXを追加
挙動を確認しやすかった
実行結果
Xa
Xb
Xc
ーーーーーーーーーーーーーーーーーーーーーーーーー
1行目の先頭にXを追加
挙動を確認しやすかった
実行結果
Xa
b
c
ーーーーーーーーーーーーーーーーーーーーーーーーー
5.7 Back-references and Subexpressions
引用元のURL記載
任意の文字、「o」、さらに最初の文字と同じ文字が続く単語を表示
挙動は分かりやすかった
実行結果
bob
dod
gog
non
pop
tot
wow
yoy
ーーーーーーーーーーーーーーーーーーーーーーーーー
先頭が任意の3文字でそれら文字を逆順にした6文字の文字を表示
挙動は分かりやすかった
実行結果
degged
hallah
kakkak
redder
retter
tebbet
terret
ーーーーーーーーーーーーーーーーーーーーーーーーー
スペースで区切られた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)
例を追加して確認する必要があった
実行結果
ba^c
b^abc
ーーーーーーーーーーーーーーーーーーーーーーーーー
行の先頭に「^」を追加(その2)
例を追加して確認する必要があった
実行結果
ba^c
b^abc
ーーーーーーーーーーーーーーーーーーーーーーーーー
「a」をxに置換(その1)
Xbcではなくxbcだった
実行結果
xbc
ーーーーーーーーーーーーーーーーーーーーーーーーー
「a」をxに置換(その2)
Xbcではなくxbcだった
実行結果
xbc
ーーーーーーーーーーーーーーーーーーーーーーーーー
^を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)
- 「公式ドキュメントには他にも多くの例があるので、ぜひ参照してください」
- リンクの再提示
- 次は〇〇コマンドの解説記事を書きます
- 次へのアクションがサイト内の回遊率を高める
- 置換対象の文字列がはじめから用意されているとコマンドの挙動を確認しやすい
- input.txtなどのファイルを扱う場合は、ファイルを作成するコマンドを一緒に記載されていると挙動を確認しやすい
- シェルスクリプトファイルにする必要があるコードは、chmod 744 test.shなど実行するまでのコマンドが併記されているとよい
- 正規表現がたくさん出てくるが、実行可能なコマンドが多いので勉強になる