一,文本过滤
1,正值表达式
句点“.”匹配单个字符
“^”匹配行首
“$“匹配行尾
“^$”匹配空行
”^.$“匹配一个字符的行
”*“匹配任意字符
”\“屏蔽特殊字符(",',||,*,+等)
”[]“表示字符集合,可用”,“分隔,或者”-“表示范围
2,find
格式: find pathname -options [-print -exec -ok]
$ find `pwd` -name "*.sh" -print
/home/zqm/test/case.sh
/home/zqm/test/read.sh
/home/zqm/test/for.sh
--------------------------------------
$ find -newer "hello.sh" ! -newer "until.sh"
.
./case.sh
./read.sh
./for.sh
----------------------------
大于3K的文件
$ find -size +3000c -print
.
./t1
./t2
-------------------------------------
$ find -type f -exec ls -alh {} \;
-rwxr--r-- 1 zqm zqm 225 2011-04-05 13:49 ./case.sh
-rw-r--r-- 1 zqm zqm 0 2011-04-03 10:45 ./t1/t1.txt
-rwxr--r-- 1 zqm zqm 175 2011-04-04 13:41 ./read.sh
--------------------------------------
$ find -type d -ok rm -rf {} \;
$ find -name "*.txt" -exec rm {} \;
xargs不会发起多个进程,而exec会,并且可能参数溢出
$ find -size -1c -print|xargs rm
---------------------------------------------
$ find -size -1c -print|xargs file
./2.txt: empty
./1.txt: empty
./3.txt: empty
3,grep
$ grep "2011-04-05 20:5[0-9]" *
行头不是“2010”的
$ grep "^[^2010]" *
4,awk
awk命令格式:awk [-F field-spearator] 'command' input-file
$ awk -F ";" '{print $1}' 1.txt
--------------------------------------------------------------------------
awk插入文件格式:awk -f awk-sripte-file input-files
awk脚本有模式和操作组成。模式:决定动作合适触发及触发事件;动作:处理数据,放在大括号内{}.
$0为所有域,$1,$2,...$n为域标识
--------------------------------------------------------------------------
$ awk -F ; '{print $1"\t"$4}' 1.txt
--------------------------------------------------------------------------
awk 'BEGIN {print "date time\n------------------"} {print $1"\t"$2} END {print "-------------------------the end -----------------------------"}' test.log|more > test.out
----------------------------------------------------------------------------------
awk中的特殊字符:+,?
+:匹配任意字符
?:匹配单个字符
匹配操作符:~:匹配;!~:不匹配
awk '$0~/2011-04-09/' test.out > 1.out
awk '{if($1=="2011-04-07") print $0}' test.out > 2.out
5,sed
sed不操作初始化文件,它只是操作文件的拷贝,然后所有的改动如果不重定向到文件,就输出到屏幕。
调用sed的三种方式:
a:sed [选项] 'sed命令' 输入文件
b:sed [选项] -f sed脚本 输入文件
c:sed脚本文件 [选项] 输入文件
如果没有指定输入文件,sed将从标准输入中接受输入,一般是键盘或重定向结果。
-------------------------------------------------------------------------------------------
sed命令选项
n 不打印
c 下一命令是编辑命令
f 正在调用sed脚本文件
-------------------------------------------------------------
$ sed -n '2p' 1.out --- 查看文件1.out的第二行(匹配行),不打印不匹配的行
$ sed -n '2,4p' 1.out --- 打印2-4行
---------------------------------------------------------------------------
$ sed -n '/a/p' 1.txt --- 匹配到“a”的行打印
$ sed -n '3,/a/p' 1.txt --- 从第三行开始,匹配到“a”的行打印
$ sed -n '/^$/=' 1.txt --- 打印空行行号
$ sed -n -e '/^$/p' -e '/^$/=' 1.txt -- 打印匹配行及其行号
$ sed '/hhh/a\ hi' 1.txt --- 在匹配行后追加“ hi”
$ sed '/hhh/i\ hi' 1.txt --- 在匹配行前追加“ hi”
$ sed '/hhh/c\ hi' 1.txt --- 将匹配行替换为“ hi”
-----------------------------------------------------------------------
sed '1,2d' 1.txt --- 删除第一,二行
sed 's/hhh/www/g' 1.txt --- 将匹配行替换为“www”
-------------------------------------------------------------------------
sed -n 's/h/www/g' 1.txt --- 将匹配字符“h”替换为字符“www”
------------------------------------------------------------------------
sed -n 's/hhh/& helo /p' 1.txt -- 匹配字符后插入字符“helo”
sed -n 's/hhh/helo &/p' 1.txt -- 匹配字符前插入字符“helo”
6,sort
sort -c 1.txt --- 检测文件是否排序
sort -u 1.txt --- 合并重复行
统计ip访问数目
cat /data/logs/1.log | awk '{print $1}'|sort|uniq -c |sort -n -r> 2.out
7,uniq
awk '{pring $1}' 1.txt|sort|uniq -c
grep "a.action" 1.txt|awk '{pring $1}'|sort|uniq -c
8,join
9,split
10,cut
11,应用
cat 4.log | awk '{if($2~/^21[0-9]/) print $1 "---" $2}'