- 追加された行はこの色です。
- 削除された行はこの色です。
*sedの使い方 [#b3cefd15]
目次~
#contents
サンプルファイルの中のOldTextをNewTextに変換してresultに書き出す
sed -e "s/OldText/NewText/g" samplefile >result
*文字操作 [#n81223ce]
**変換 [#r09aacde]
-タブをスペースに変換
sed -e 's///g
文字列の削除
sed -e "s/TexttoRemove//g"
-複数のスペースを1個スペースに変換
sed -e '*//g'
行頭の文字列を消す
sed -e "s/^TexttoRemove//"
**削除 [#a2ee30da]
-空白行を削除
sed -e '/^$/d'
行末の文字列を消す
sed -e "s/TexttoRemove\$//"
-行頭のホワイトスペースを削除する
sed -e 's/^[]*//'
Patternを含む行を削除する
sed -e "s/Pattern.*//"
-行末のホワイトスペースを削除
sed -e "s/[]*\$//"
ユーザーIDをUSERに代入
USER=`id |sed 's/uid=.*(\(.*\)) gid=.*/\1/'`
-Textという文字列を含んだ行を削除する
sed -e "/Text/d"
タブをスペースに変換
sed -e 's///g
-文字列の削除
sed -e "s/TexttoRemove//g"
複数のスペースを1個スペースに変換
sed -e '*//g'
-行頭の文字列を消す
sed -e "s/^TexttoRemove//"
行頭のホワイトスペースを削除する
sed -e 's/^[]*//'
-行末の文字列を消す
sed -e "s/TexttoRemove\$//"
行末のホワイトスペースを削除
sed -e "s/[]*\$//"
-Patternを含む行を削除する
sed -e "s/Pattern.*//"
Textという文字列を含んだ行を削除する
sed -e "/Text/d"
空白行を削除
sed -e '/^$/d'
*処理範囲の指定 [#o2776d0a]
5行目から3行目を処理
sed -e "5,20s/OldText/NewText/g" file
5行目から最後までを処理
sed -e "5,$s/OldText/NewText/g" file
1行目を削除
sed -e '1d' file
1行目から1行目を削除
sed -e '1,4d' file
最終行を削除
sed -e '$d' file
最終4行を削除
sed -e '$-3,$d' file
*実用Tips [#y26d620e]
nからmまでを表示
sed -n 'n,mp' file
配下のディレクトリを表示
ls -l|grep ^[d].*|awk '{print $9}'~
サンプルファイルの中のOldTextをNewTextに変換してresultに書き出す
sed -e "s/OldText/NewText/g" samplefile >result
ユーザーIDをUSERに代入
USER=`id |sed 's/uid=.*(\(.*\)) gid=.*/\1/'`