便利な一行コマンド集  ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ver 0.2 2002/08/19 ver 0.1 2000/04/04 ver 0.0 2000/01/24 江端智一 E-mail:See http://www.kobore.net/mailAddress.gif http://www.kobore.net/  言うまでもありませんが、本文章は 無保証です。この文章の影響によりどの ような事が起ころうとも当方は一切の責任を持ちません。 ○ファイル内文字列の置換 -i -p -eオプションを使用します。 # カレントディレクトリにある拡張子.htmlというファイル中のfromをformに # 置換する # UNIX系 perl -i -p -e 's/from/form/g;' *.html  オプションはそれぞれ以下の働きをします。 -i ファイルを修正する。拡張子の指定があれば、その拡張子でバックアップ を取る。 -p 1行ずつ処理し、結果を出力する。 -e コマンドライン上でスクリプトを指定する。 -pによってファイルが処理され(処理内容は-eで指定)、出力されます。そして その結果が-iによって元のファイルに出力されます。  ただしWin98+Active Perl v 5.005_03でやってみたところ、以下のような修正が必要でした。 ・-iに拡張子を指定する。 ・-eではシングルクォートではなくダブルクォートを使う。 ・ファイル名にワイルドカード(*や?)は使えない。 # index.htmlというファイル中のfromをformに置換する # (オリジナルのファイルがindex.html.bakという名前で作成される) # Win98+Active Perl perl -i.bak -p -e "s/from/form/g;" index.html ***************************************** 成功例 WindowsXP + ActivePerlで "http://www.hh.iij4u.or.jp/~yukon" を、"http://www.kobore.net" に変換する処理 perl -i.bak -p -e "s/http:\/\/www.hh.iij4u.or.jp\/\~yukon\//http:\/\/www.kobore.net\//g;" index.shtml ***************************************** ○カレントディレクトリにある各ディレクトリの総サイズを出力 % du -sk * ○netstatを使って、特定の複数のポート(20番と21番)の状態を検索 netstat -an | grep -E -w "20|21" # "grep -E" で正規表現利用可能 ○ある文章内にある単語 "ebata" を "god"に変更したいが、そのときのファ イル dummy.txt の中で、単語 "tomoichi"が登場する行に対してのみ、これを 行なう。 sed '/tomoichi/ s/ebata/god/g' dummy.txt ○空白行の削除 sed '/^$/ d' dummy.txt ○文字列"hoge"が存在するファイルを検索する。 find . -type f -name "*.c" -exec grep -l hoge {} \; ○カレントにあるファイル or ディレクトリ数表示 ls -1 | awk 'END{print NR}' ○hoge.txt内の空白行を削除してhoge2.txtに書き出す。 grep -v ^$ hoge.txt > hoge2.txt ○カレント以下のファイルリスト表示 find . -type f -print ○ディレクトリのコピー cp -p hoge.txt hoge.txt.org ○hoge foo aho foo aho とかいうファイルのランキングを集計。 sort filename | uniq -c | sort -rn ○C のコメントを削除 投稿日 perl -i.dist -0pe 's!/\*.*?\*/!!sg' hogehoge.c ○? ps aux | grep R | grep -v grep ○カレントディレクトリ以下の、任意のファイルを消す。 rm `find ./ -name filename` find ./ -name filename -exec /bin/rm {} \; find . -name filename -print | xargs rm ○10行目から24行目をみる %head -24 hoge.txt | tail -15 ○カレントにあるディレクトリ ls -l | grep ^d ○man manualを通常のテキストファイルにする。 man command | col -b > command.txt