■元のテキスト
■行追加(aコマンド)
1.指定したテキストを1行追加
3行目の後ろに「ABC」を追加。aコマンドの後ろに「\」を入れて、その後に追加したい文字列を入力。
aの前の「3」で追加する行数を指定。
2.指定したテキストを複数行追加
3行目の後ろに追加。改行を入れるときは「\」を入力してから改行する。
■行挿入(iコマンド)
1.指定したテキストを1行挿入
3行目の前に「ABC」を追加。aコマンドと同様の入力方法だが、追加される位置が指定行の前になる。
2.指定したテキストを複数行挿入
3行目の前に追加。aコマンドと同様の入力方法だが、追加される位置が指定行の前になる。
■ 記事で使ったsedのバージョン
■参照
sed, a stream editor
$ cat sample.txt
1 [.exe
2 a2p.exe
3 addftinfo.exe
4 afmtodit
5 apropos
6 arch.exe
7 ascii.exe
8 ash.exe
9 awk
10 banner.exe
■行追加(aコマンド)
1.指定したテキストを1行追加
3行目の後ろに「ABC」を追加。aコマンドの後ろに「\」を入れて、その後に追加したい文字列を入力。
aの前の「3」で追加する行数を指定。
$ sed '3a\ABC' sample.txt行数の範囲を指定する場合。aの前の「3,5」で追加する行の範囲(3~5)を指定。
1 [.exe
2 a2p.exe
3 addftinfo.exe
ABC
4 afmtodit
5 apropos
6 arch.exe
7 ascii.exe
8 ash.exe
9 awk
10 banner.exe
$ sed '3,5a\ABC' sample.txt正規表現で行位置を指定する場合。aの前の「/apropos/」で追加する行の位置(正規表現「apropos」が含まれる行)を指定。
1 [.exe
2 a2p.exe
3 addftinfo.exe
ABC
4 afmtodit
ABC
5 apropos
ABC
6 arch.exe
7 ascii.exe
8 ash.exe
9 awk
10 banner.exe
$ sed '/apropos/a\ABC' sample.txt正規表現で行範囲を指定することも可能。(「apropos」の含まれる行から「ascii」の含まれる行まで)
1 [.exe
2 a2p.exe
3 addftinfo.exe
4 afmtodit
5 apropos
ABC
6 arch.exe
7 ascii.exe
8 ash.exe
9 awk
10 banner.exe
$ sed '/apropos/,/ascii/a\ABC' sample.txt
1 [.exe
2 a2p.exe
3 addftinfo.exe
4 afmtodit
5 apropos
ABC
6 arch.exe
ABC
7 ascii.exe
ABC
8 ash.exe
9 awk
10 banner.exe
2.指定したテキストを複数行追加
3行目の後ろに追加。改行を入れるときは「\」を入力してから改行する。
$ sed '3a\ABC\「\n」を入力しても改行が入る。
> DEF' sample.txt
1 [.exe
2 a2p.exe
3 addftinfo.exe
ABC
DEF
4 afmtodit
5 apropos
6 arch.exe
7 ascii.exe
8 ash.exe
9 awk
10 banner.exe
$ sed '3a\ABC\nDEF' sample.txt
1 [.exe
2 a2p.exe
3 addftinfo.exe
ABC
DEF
4 afmtodit
5 apropos
6 arch.exe
7 ascii.exe
8 ash.exe
9 awk
10 banner.exe
■行挿入(iコマンド)
1.指定したテキストを1行挿入
3行目の前に「ABC」を追加。aコマンドと同様の入力方法だが、追加される位置が指定行の前になる。
$ sed '3i\ABC' sample.txt
1 [.exe
2 a2p.exe
ABC
3 addftinfo.exe
4 afmtodit
5 apropos
6 arch.exe
7 ascii.exe
8 ash.exe
9 awk
10 banner.exe
2.指定したテキストを複数行挿入
3行目の前に追加。aコマンドと同様の入力方法だが、追加される位置が指定行の前になる。
$ sed '3i\ABC\
> DEF' sample.txt
1 [.exe
2 a2p.exe
ABC
DEF
3 addftinfo.exe
4 afmtodit
5 apropos
6 arch.exe
7 ascii.exe
8 ash.exe
9 awk
10 banner.exe
■ 記事で使ったsedのバージョン
$ sed --version
GNU sed version 4.2.1
Copyright (C) 2009 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE,
to the extent permitted by law.
GNU sed home page: <http://www.gnu.org/software/sed/>.
General help using GNU software: <http://www.gnu.org/gethelp/>.
E-mail bug reports to: <bug-gnu-utils@gnu.org>.
Be sure to include the word ``sed'' somewhere in the ``Subject:'' field.
■参照
sed, a stream editor
コメント