dot install sed | ぽにょ、ぽにょぽーにょ

ぽにょ、ぽにょぽーにょ

2012年からWebエンジニア。
プログラムとか、趣味とか、人生とかの備忘録。

なんかアメブロってださくて使いづらいからそのうち引っ越しまふ。


sed -e '3d' names.txt
⇒3行目を消す

 sed '3d' names.txt
⇒ 1つだけならeを省略できる

 sed -i '3d' names.txt
ファイルを上書き

 sed -i.bk '3d' names.txt
ファイルを上書きしてバックアップ

 vim ex1.sed
 sed -f ex1.sed names.txt
外部ファイル読み込み


>>>>>>>>>>>>>>>>>>>>>>>
sed '3d' names.txt

address 3
command d ←パターン表示を削除

パターンスペースっていうバッファーを使って処理をやる
1.ファイルから1行読み込んでパターンスペースに格納
2.addressにマッチ? ⇒ commandを実行!
3. パターンスペースを表示

>>>>>>>>>>>>>>>>>>>>>>

 1062  sed '3!d' names.txt
3行目以外をd

 1063  sed '1d;3d' names.txt
複数指定。1行目と3行目をd


 1064  sed '1,3d' names.txt
1~3行目をd



 1065  sed '1~2d' names.txt
1,3,5,,,,をd

 1066  sed '$d' names.txt
最後の行をd

 1067  sed '3,$d' names.txt
3~最後の行をd


 1068  sed '/i$/d' names.txt
正規表現。最後がiで終わるやつをd


 1069  sed 'd' names.txt
全部の行でd

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


 1072  sed '3p' names.txt
3行目をprint


 1073  sed -n '3p' names.txt
3行目をprintマッチしないところは出力しない

 1074  sed '3q' names.txt
3行目を出力してquit

 1076  sed '1i\--- start ---' names.txt
1行目の前に--- start ---を代入

 1077  sed -e '1i\---- start---' -e '$a\ === end ===' names.txt
複数コマンドはe
1行目の前に--- start ---
最後の行の後に end

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


 1082  sed 's/apple/Apple/' items.txt
appleをAppleに変換(最初の一つのみ)

 1083  sed 's/apple/Apple/g' items.txt
appleをAppleに変換(すべて)

 1085  sed 's/apple/Apple/2' items.txt
appleをAppleに変換(2番目)

 1086  sed 's/apple/Ringo/ig' items.txt
appleをRingoに変換(すべて、大文字区別なし)

 1087  sed 's/[aA]pple/Ringo/g' items.txt
正規表現ver

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

# sed 's/[0-5]/[&]/' items.txt
& : [0-5]で抜き出したものを使う

[1] taguchi Apple, apple, apple, grape
[2] fkoji Banana, apple, Apple, lemon
[3] dotinstall Grape, apple, strawberry
[4] tkahashi cherry, pear, kiwi
[5] yasuda cherry, Cherry


# sed 's/\([0-5]\) \(.*\)/\2 [\1]/' items.txt

()でとじると\1とか\2であらわすことができる。
()の前はエスケープ文字が必要

taguchi Apple, apple, apple, grape [1]
fkoji Banana, apple, Apple, lemon [2]
dotinstall Grape, apple, strawberry [3]
tkahashi cherry, pear, kiwi [4]
yasuda cherry, Cherry [5]