実践例
EX
for f in *.cpp という扱いでOK
file数をカウント
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; // 行末の \ を削除
query-replace-regexp ■ \(.*\) -> <b id="aaa"> ■ \1</b>
file の 中身を探す際は find && grep を使用
特定のディレクトリを検索
find . -name 'bonus tool'; // file にあたりをつける
file内をキーワードでさがす
grep -R -i "layout UV" dir_bonus_tool
西暦をいれる
egrep '[0-9]{4}\.[0-9]{2}\.[0-9]{2}' file
egrep '[0-9]{4}\.([0-9]{2})\.\1' file
egrep '[0-9]{4}\.([1-2][0-9]|0[1-9]|3[0-1])\.\1' file
SpellCheck
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 を抽出 )
grep '^From:' file | cut -d ':' -f 2
Commentカンマ空行を一度に消す
egrep -v '/\*' test.c | egrep -v '^$'
egrep -v '(/\*|^$)' test.c
egrep -v '(^[^/])' test.c
sed.で.comment.を削除
cmd; ### comment ###
^.*#.* -> 最後の # で match
^[^#]*#.* -> 最初の # で match
sed 's/^\([^#]*\)#.*/\1/'
print "test"; # comment
print "test";
c cmt を削除
/* はcmt に存在しない
/\*.*\*/
'.' -> '*/' 以外の文字にmatch
[^*] == *以外の文字
\*[^/] == * + /以外の文字
([^*]|\*[^/])*
/\*([^*]|\*[^/])*\*/
htm.からheaderを抽出
echo "<h6>xxx</h6>" | grep "[hH][1-6]"
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
sed 's/
http:.*/<a href=\"\1\">\1<\/a>/g' file