▼すべてを開く。
▲すべてを閉じる。
$ cat Sep2005.txt 41th Monthly Meeting Date: Sep.9 (Fri),2005 Time: 18:30-21:00 Place: A4 Engineering Dep. Room 103 Subject: Refer to the attached file
$ cat change s/41th/42th/ s/Sep/Oct/ s/9/7/ s/103/101/
$ sed -f change < Sep2005.txt 42th Monthly Meeting Date: Oct.7 (Fri),2005 Time: 18:30-21:00 Place: A4 Engineering Dep. Room 101 Subject: Refer to the attached file
sedコマンドで、-fオプション、編集コマンドを記述したスクリプトファイル「change」を指定し、リダイレクト「<」で編集するファイル「Sep2005.txt」をsedコマンドに与える。これで、ファイル「change」に記述された編集コマンドがファイル「Sep2005.txt」に対して実行される。
複数の置換を行うときにファイルに編集コマンドを記述しておく。この編集コマンドは、1行に1つのコマンドを記述する。
$ sed -f change < Sep2005.txt > Oct2005.txt
ファイルに保存する場合は、コマンドの出力をリダイレクト「>」でファイル「Oct2005.txt」に指定する。
$ sed -f change < Sep2005.txt | tee Oct2005.txt 42th Monthly Meeting Date: Oct.7 (Fri),2005 Time: 18:30-21:00 Place: A4 Engineering Dep. Room 101 Subject: Refer to the attached file
ファイルに保存して、画面にも表示する場合は、teeコマンドを使う。sedコマンドの出力をパイプ「|」でteeコマンドに渡し、保存するファイル「Oct2005.txt」を指定する。これで、コマンドの実行結果がファイルに保存され、画面にも表示される。