実践例





EX


for f in *.cpp という扱いでOK


file数をカウント


# CurrentDirectory の jpg の数 ls *.jpg | wc ls | egrep "lzh|zip" | wc ls | grep "jpg" | grep "png" | wc



xxx.zip.を見つけたら消す


find . -name '*.zip' -exec rm {}\;



cur dir以下の特定名のfileをcp|mv


cp `find . -name 'foo'` foodir query-replace-regexp \\$ -> NULL; // 行末の \ を削除
# tag を追加, ascii OK !, [""]は \ するとエラー, ie quato の必要なし query-replace-regexp ■ \(.*\) -> <b id="aaa"> ■ \1</b> # htm/ 以下に <meta charset="Shift_JIS"> を先頭にいれたいconcat を使用する file の 中身を探す際は find && grep を使用


特定のディレクトリを検索


# Layout UV 使用方法を bonus tool からさがしている find . -name 'bonus tool'; // file にあたりをつける



file内をキーワードでさがす


# file から説明を探す grep -R -i "layout UV" dir_bonus_tool



西暦をいれる


# 1999.10.15 のみ抽出 egrep '[0-9]{4}\.[0-9]{2}\.[0-9]{2}' file # 後方参照 egrep '[0-9]{4}\.([0-9]{2})\.\1' file # : day[01-31] egrep '[0-9]{4}\.([1-2][0-9]|0[1-9]|3[0-1])\.\1' file


SpellCheck


# daemon <-> demon とまちがえてしまう egrep 'd?emon' test.txt



CSV項目を入れ替えてカンマ区切りに変換


sed 's/\(foo\)\t\(bar\)/\2,\1'



list から文字列を抽出


: /usr/bin 以下から a から始まるfile を取得 ls /usr/bin | egrep '^a'



条件を満たすfileを取得


file == xxxx.0 find /usr/bin -name '?*[a-zA-Z].[0-9]' egrep でmail address を抽出( mailer から address を抽出 ) # user 名のみをとる # From: usrname@domain in file grep '^From:' file | cut -d ':' -f 2



Commentカンマ空行を一度に消す


egrep -v '/\*' test.c | egrep -v '^$' # egrep -v '(/\*|^$)' test.c # [^/] == / 以外の一文字 == 空行は !match egrep -v '(^[^/])' test.c



sed.で.comment.を削除


cmd; ### comment ### ^.*#.* -> 最後の # で match ^[^#]*#.* -> 最初の # で match # 変換パターン ^\([^#]*\)#.* # 置換 \1 sed 's/^\([^#]*\)#.*/\1/'
print "test"; # comment print "test";
c cmt を削除 /* はcmt に存在しない /\*.*\*/ '.' -> '*/' 以外の文字にmatch [^*] == *以外の文字 \*[^/] == * + /以外の文字 ([^*]|\*[^/])* /\*([^*]|\*[^/])*\*/



htm.からheaderを抽出


# パターン [hH][1-6] echo "<h6>xxx</h6>" | grep "[hH][1-6]" # Insentive 指定 cat foo.htm | grep -i "h[1-6]"



emacs.で時刻を条件に.logをchk


2005/01/10 - 12 23:00 - 23:05 の log を見る ptn 1[0-2]/Jan/2005:23:0[0-5]



emacsで表記の一貫性のチェック


(xxx) の 全角、半角の統一 ptn ( (|\( ) .+? ( )|\) ) rep ( (|\( ) (.+?) ( )|\) ) -> \2 WARNING emacs meta chara |() -> escape で meta chara に変換 \( (\|( \) \(.+?\) \( )\|) \) -> \2 \((\|(\)\(.+?\)\()\|)\) -> \2 POINT [] で \ を代用 [((]\(.*?\)[))] -> (\1) POINT [ xxx ] など表記の統一に利用可能 htm の tag を取り除く ptn <.+> 空行 [\t ]* [\t ]*<.+> 最短一致 [\t ]*<[^>]+> header 作成 sed s/^\([^[:space:]]\+\)/<h1>\1<\/h1>/g test.txt # URL を Anchor タグでくくる sed 's/http:.*/<a href=\"\1\">\1<\/a>/g' file