sedコマンドとは?

ed ラインエディタ
(一行ずつしか編集できない)
     │
     │← 正規表現による置換機能
     │← 正規表現による検索機能
     │← etc.etc.
     │
     ├──────────┐
     ┴          │
*breakthrough*     │
     ┬          │
      ↓           ↓
vi スクリーンエディタ    grep, sed, ...
(画面全体で編集可能)
     │
     ↓
emacs, Vz, VisualStudio, Eclipse, ...
  1. 古代の人々は、一行ずつしか文書を編集できないラインエディタ(ed)を使っていた
    • 当時、ラインエディタに対して様々な便利機能が追加された
    • 行の指定機能
    • 検索機能
    • 置換機能
  2. やがて、画面全体を使って文書を編集できるスクリーンエディタ(vi)が作られた
    • viにも、edの検索や置換機能は引き継がれている
  3. viから、emacsをはじめとする近代的なエディタが生まれた
  4. ed自体は余り使われることはなくなったが、edに追加された便利機能は、コマンド として切り出されて今日に至る
    • grep
    • sed
  5. sed(stream ed)は、標準入力から入ってきた文書に対して、正規表現を使って置換 を行い、標準出力に返すコマンド

sed --help

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

 -n, --quiet, --silent
                suppress automatic printing of pattern space
 -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
 -i[SUFFIX], --in-place[=SUFFIX]
                edit files in place (makes backup if extension supplied)
 -l N, --line-length=N
                specify the desired line-wrap length for the `l" command
 --posix
                disable all GNU extensions.
 -r, --regexp-extended
                use extended regular expressions in the script.
 -s, --separate
                consider files as separate rather than as a single continuous
                long stream.
 -b, --binary
                do not convert DOS to UNIX lineendings (only on systems
                supporting different lineendings).
 -u, --unbuffered
                load minimal amounts of data from the input files and flush
                the output buffers more often
     --help     display this help and exit
     --version  output version information and exit

練習問題( 編集対象のファイル )

>cat sedExam.txt
# Constitution Class
L02
L03 USS Constellation NCC-1017
L04 USS Constitution NCC-1700
L05 USS Defiant NCC-1764
L06 USS Eagle NCC-956
L07 USS Endeavor NCC-1895
L08 USS Enterprise NCC-1701
L09 USS Enterprise NCC-1701-A
L10 USS Essex NCC-1697
L11 USS Excalibur NCC-1664
L12 USS Exeter NCC-1672
L13 USS Farragut NCC-1647
L14 USS Hood NCC-1703
L15 USS Intrepid NCC-1831
L16 USS Kongo NCC-1710
L17 USS Lexington NCC-1709
L18 USS Potemkin NCC-1657
L19 USS Republic NCC-1371
L20 USS Yorktown NCC-1717

sedスクリプトの実行方法

編集対象行の指定方法

行指定のsedスクリプト(削除=d)

特定の行に対して sedスクリプト("d"=削除)を適用する。
この場合 1行目 のみが適用対象。

>sed -e "1d" sedExam.txt
L02
L03 USS Constellation NCC-1017
L04 USS Constitution NCC-1700
L05 USS Defiant NCC-1764
L06 USS Eagle NCC-956
L07 USS Endeavor NCC-1895
L08 USS Enterprise NCC-1701
L09 USS Enterprise NCC-1701-A
L10 USS Essex NCC-1697
L11 USS Excalibur NCC-1664
L12 USS Exeter NCC-1672
L13 USS Farragut NCC-1647
L14 USS Hood NCC-1703
L15 USS Intrepid NCC-1831
L16 USS Kongo NCC-1710
L17 USS Lexington NCC-1709
L18 USS Potemkin NCC-1657
L19 USS Republic NCC-1371
L20 USS Yorktown NCC-1717

行範囲指定のsedスクリプト(削除=d)

特定の範囲に対して sedスクリプト("d"=削除)を適用する。
この場合 1行目から10行目 のみが適用対象。

>sed -e "1,10d" sedExam.txt
L11 USS Excalibur NCC-1664
L12 USS Exeter NCC-1672
L13 USS Farragut NCC-1647
L14 USS Hood NCC-1703
L15 USS Intrepid NCC-1831
L16 USS Kongo NCC-1710
L17 USS Lexington NCC-1709
L18 USS Potemkin NCC-1657
L19 USS Republic NCC-1371
L20 USS Yorktown NCC-1717

正規表現に合致する行を対象とするsedスクリプト(削除=d)

正規表現に合致する範囲に対して sedスクリプト("d"=削除)を適用する。
この場合 # から始まる行のみが適用対象。

>sed -e "/^#/d" sedExam.txt
L02
L03 USS Constellation NCC-1017
L04 USS Constitution NCC-1700
L05 USS Defiant NCC-1764
L06 USS Eagle NCC-956
L07 USS Endeavor NCC-1895
L08 USS Enterprise NCC-1701
L09 USS Enterprise NCC-1701-A
L10 USS Essex NCC-1697
L11 USS Excalibur NCC-1664
L12 USS Exeter NCC-1672
L13 USS Farragut NCC-1647
L14 USS Hood NCC-1703
L15 USS Intrepid NCC-1831
L16 USS Kongo NCC-1710
L17 USS Lexington NCC-1709
L18 USS Potemkin NCC-1657
L19 USS Republic NCC-1371
L20 USS Yorktown NCC-1717

正規表現に合致する行を対象とするsedスクリプト2(表示=p)

sedスクリプトの "p" は、選択領域のみ表示する。
"-n"をつけないと、sed のパターン・スペース(正規表現処理のためのメモリ領域)に入った行が全て表示されてしまうので、pスクリプト を使うときには "-n" を指定する必要がある。

>sed -n -e "/E.*e/p" sedExam.txt
L06 USS Eagle NCC-956
L07 USS Endeavor NCC-1895
L08 USS Enterprise NCC-1701
L09 USS Enterprise NCC-1701-A
L10 USS Essex NCC-1697
L12 USS Exeter NCC-1672

正規表現で範囲指定したsedスクリプト(表示=p)

E.* が出現する行から、H.* が出現する行までを対象とするようにするには、次のように

/${正規表現}/,/${正規表現}/${sedスクリプト}/

とする。

>sed -n -e "/E.*/,/H.*/p" sedExam.txt
L06 USS Eagle NCC-956
L07 USS Endeavor NCC-1895
L08 USS Enterprise NCC-1701
L09 USS Enterprise NCC-1701-A
L10 USS Essex NCC-1697
L11 USS Excalibur NCC-1664
L12 USS Exeter NCC-1672
L13 USS Farragut NCC-1647
L14 USS Hood NCC-1703

置換=s

最初に指定した正規表現に合致した箇所の置換

17行目について、"L17" は "行17" に変換されますが、"Lexington" は "行exintgton" には変換されません。

>sed -e "s/L/行/" sedExam.txt
# Constitution Class
行02
行03 USS Constellation NCC-1017
行04 USS Constitution NCC-1700
行05 USS Defiant NCC-1764
行06 USS Eagle NCC-956
行07 USS Endeavor NCC-1895
行08 USS Enterprise NCC-1701
行09 USS Enterprise NCC-1701-A
行10 USS Essex NCC-1697
行11 USS Excalibur NCC-1664
行12 USS Exeter NCC-1672
行13 USS Farragut NCC-1647
行14 USS Hood NCC-1703
行15 USS Intrepid NCC-1831
行16 USS Kongo NCC-1710
行17 USS Lexington NCC-1709  <-- !
行18 USS Potemkin NCC-1657
行19 USS Republic NCC-1371
行20 USS Yorktown NCC-1717

全ての指定した正規表現に合致した箇所の置換

17行目の全ての"L"が"行"に変換されました。

>sed -e "s/L/行/g" sedExam.txt
# Constitution Class
行02
行03 USS Constellation NCC-1017
行04 USS Constitution NCC-1700
行05 USS Defiant NCC-1764
行06 USS Eagle NCC-956
行07 USS Endeavor NCC-1895
行08 USS Enterprise NCC-1701
行09 USS Enterprise NCC-1701-A
行10 USS Essex NCC-1697
行11 USS Excalibur NCC-1664
行12 USS Exeter NCC-1672
行13 USS Farragut NCC-1647
行14 USS Hood NCC-1703
行15 USS Intrepid NCC-1831
行16 USS Kongo NCC-1710
行17 USS 行exington NCC-1709 <-- !
行18 USS Potemkin NCC-1657
行19 USS Republic NCC-1371
行20 USS Yorktown NCC-1717

行を指定した置換も出来ます

>sed -e "5,10s/USS/HMS/" sedExam.txt
# Constitution Class
L02
L03 USS Constellation NCC-1017
L04 USS Constitution NCC-1700
L05 HMS Defiant NCC-1764
L06 HMS Eagle NCC-956
L07 HMS Endeavor NCC-1895
L08 HMS Enterprise NCC-1701
L09 HMS Enterprise NCC-1701-A
L10 HMS Essex NCC-1697
L11 USS Excalibur NCC-1664
L12 USS Exeter NCC-1672
L13 USS Farragut NCC-1647
L14 USS Hood NCC-1703
L15 USS Intrepid NCC-1831
L16 USS Kongo NCC-1710
L17 USS Lexington NCC-1709
L18 USS Potemkin NCC-1657
L19 USS Republic NCC-1371
L20 USS Yorktown NCC-1717

"/"を含む文字列を置換したいとき

区切り文字を任意の文字に変更できます。正規表現中では、"/" は "\/" と表現します

>echo 2013/03/15 | sed -e "s:\/:-:g"
2013-03-15
>echo "/bin/mysql" | sed -e "s:\/bin\/mysql:\/usr\/local\/mysql\/bin:g"
"/usr/local/mysql/bin"

高度な置換

挿入

置換後の文字列の後ろに"&"を入れると、合致する箇所の前方への挿入になります

>sed -e "s/Enterprise/@&/" sedExam.txt
# Constitution Class
L02
L03 USS Constellation NCC-1017
L04 USS Constitution NCC-1700
L05 USS Defiant NCC-1764
L06 USS Eagle NCC-956
L07 USS Endeavor NCC-1895
L08 USS @Enterprise NCC-1701
L09 USS @Enterprise NCC-1701-A
L10 USS Essex NCC-1697
L11 USS Excalibur NCC-1664
L12 USS Exeter NCC-1672
L13 USS Farragut NCC-1647
L14 USS Hood NCC-1703
L15 USS Intrepid NCC-1831
L16 USS Kongo NCC-1710
L17 USS Lexington NCC-1709
L18 USS Potemkin NCC-1657
L19 USS Republic NCC-1371
L20 USS Yorktown NCC-1717

置換後の文字列の前に"&"を入れると、合致する箇所の後方への挿入になります

>sed -e "s/Enterprise/&@/" sedExam.txt
# Constitution Class
L02
L03 USS Constellation NCC-1017
L04 USS Constitution NCC-1700
L05 USS Defiant NCC-1764
L06 USS Eagle NCC-956
L07 USS Endeavor NCC-1895
L08 USS Enterprise@ NCC-1701
L09 USS Enterprise@ NCC-1701-A
L10 USS Essex NCC-1697
L11 USS Excalibur NCC-1664
L12 USS Exeter NCC-1672
L13 USS Farragut NCC-1647
L14 USS Hood NCC-1703
L15 USS Intrepid NCC-1831
L16 USS Kongo NCC-1710
L17 USS Lexington NCC-1709
L18 USS Potemkin NCC-1657
L19 USS Republic NCC-1371
L20 USS Yorktown NCC-1717

入れ替え

"\(\)" を使うことによって、行を領域に分割できます。

>sed -e "s/\(.*\) \(.*\) \(.*\) \(.*\)/Reg: \4 Name: \3/" sedExam.txt
# Constitution Class
L02
Reg: NCC-1017 Name: Constellation
Reg: NCC-1700 Name: Constitution
Reg: NCC-1764 Name: Defiant
Reg: NCC-956 Name: Eagle
Reg: NCC-1895 Name: Endeavor
Reg: NCC-1701 Name: Enterprise
Reg: NCC-1701-A Name: Enterprise
Reg: NCC-1697 Name: Essex
Reg: NCC-1664 Name: Excalibur
Reg: NCC-1672 Name: Exeter
Reg: NCC-1647 Name: Farragut
Reg: NCC-1703 Name: Hood
Reg: NCC-1831 Name: Intrepid
Reg: NCC-1710 Name: Kongo
Reg: NCC-1709 Name: Lexington
Reg: NCC-1657 Name: Potemkin
Reg: NCC-1371 Name: Republic
Reg: NCC-1717 Name: Yorktown

一つのコマンドで2回置換を実行する

[~]$ echo "となりのきゃくはよくかきくうきゃくだ" | sed -e "s/きゃく/客/g" -e "s/かき/柿/"
となりの客はよく柿くう客だ

sedプログラム

実行例

PERFORMER=`mediainfo $line | grep "Performer" | awk 'BEGIN{FS="[ ]*:[ ]*"}{print $2}'`
RECDATE=`echo $line | sed -e "s/^[^_]*//i" -e "s/[.].*$//i"`
MP3=`echo ${PERFORMER}${RECDATE}.mp3 | sed -e 'y/ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefgh
ijklmnopqrstuvwxyz0123456789/ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789/'`
/usr/bin/ffmpeg -y -i $line -acodec libmp3lame -vn -ab 128k /var/video/mp3/${MP3} < /dev/null

参考文献

http://itpro.nikkeibp.co.jp/article/COLUMN/20060227/230879/


Computer


添付ファイル: fileitune.png 2394件 [詳細]

トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS   sitemap
Last-modified: 2012-06-21 (木) 01:27:51 (4327d)
Short-URL:
ISBN10
ISBN13
9784061426061