常用文本过滤命令笔记(find grep wc awk sed sort uniq split)

来源:互联网 发布:算法导论第二版pdf 编辑:程序博客网 时间:2024/04/27 14:20

自:http://hi.baidu.com/bomob987/blog/item/d80292afa4e4e1104a36d67a.html

 

都支持regular expression
1. find
-name 按名字查找
-perm 根据权限查找
-ctime –n +m 根据创建时间查找,-n n天内,+n n天之前
-mtime –n +n 最后修改时间
-exec command {} \; 对每条记录执行command
-ok command {}\;同上
例:find . –name “*.log” –exec ls –al {} \;
find /var -name "*.log" -mtime +10 -exec ls -l {} \;

2. grep
grep –c “sdf” *.txt 只返回匹配的行数
grep –n 输出行号
grep “2010-5-1[0-9]” myfile 10号到19号的
grep “^[^123]” myfile 不是以1,2,3大头的
grep “4\{2\}” myfile 连续2个4
grep “4\{2,\}” myfile 连续至少2个4
grep “4\{2,5\}” myfile 连续2到5个4
grep “^$” myfile 空行
grep “\^” myfile 查找^符号,用\过滤掉转义

3. wc
Wc用来计算文件字符数,字节数,行数等信息的
Wc –l <myfile 返回myfile的行数

4. awk(很好很强大)
awk ‘{print $0}’ myfile 显示myfile的所有域($0),分隔符为空格(默认)
awk –F “asdf” ‘{print $1}’ myfile 显示1域,分隔符asdf
awk -F ": " 'BEGIN {print "hire_date\n-------------------"}{print $1}' messages |head
//awk中的BEGIN {command} END{command} 结构,指定头和尾的操作,不同于一般的BEGIN END块结构

Awk中特殊元字符:+ , ?   //+匹配所有,?匹配单个字符
匹配操作符:~ , !~         //~匹配,!~不匹配

head messages |awk '$0 ~ /21:59/' 匹配21:59的行,相当于grep功能了,不过awk可以更精确的控制具体域!
head messages |awk '$0 !~ /21:59/' 不匹配21:59的行(grep -i)

[mysql@node1 ~]$ head messages |awk '{if($3=="21:59:48") print $0}'
May 10 21:59:48 node1 dhclient: DHCPREQUEST on eth1 to 192.168.217.254 port 67
May 10 21:59:48 node1 dhclient: DHCPACK from 192.168.217.254
May 10 21:59:48 node1 dhclient: bound to 192.168.217.133 -- renewal in 843 seconds.
//AWK自己的控制流结构,相比一般shell语法似乎更接近于C

5. sed
sed 是一种在线编辑器,它一次处理一行内容。处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用sed命令处理缓冲区中的内容,处理完成后,把缓冲区的内容送往屏幕。接着处理下一行,这样不断重复,直到文件末尾。文件内容并没有改变。

-n 是打印不匹配的行,默认是打印所有
-e 后面跟脚本,直到新行或者遇到-e结束。如果不给-e,则第一个命令直到新行才结束
-f 后面跟脚本文件

基本编辑命令:
p 打印匹配行
= 显示行号
a\ 在定位行后添加信息(新行)
i\ 在定位行前插入信息(新行)
d 删除定位行
c\ 替换定位的行内容,注意是整行内容
s/re/string 用string替换正则表达式re。
g表示行内全面替换。

注意,pattern的内容都是正则表达式
sed ‘2p’ myfile 打印所有行
sed –n ‘2p’ myfile 答应第二行,-n是打印不匹配的行,默认是打印所有
sed –n ‘1,7p’ myfile 打印1到7行(1,$就是1到最后一行)
sed –n ‘/fuyar/p’ myfile 打印匹配fuyar的行,模式匹配方式
sed –n ‘2,/fuyar/p’ myfile 从第二行开始到匹配到fuyar结束。行号方式跟模式匹配方式的结合使用
=号:Print the current line number.
sed –n ‘/^$/=’ myfile 显示空行行号
sed –n –e ‘/^$/=’ –e ‘/^$/p’ myfile 显示空行行号并打印
sed -n 's/param/& hellow /p' yy.sh 在param后加上hellow
sed -n 's/param/ hellow &/p' yy.sh 在param前加hellow

6. sort
-c 检查文件是否已排序
-u unique的意思,排序后重复记录只显示一条
-r reverse,反序
-n 数字排序
-kn 按第n个域进行排序,相当于+ n-1 –n,现在推荐用-k

sort -t: -k3 messages | head 等同于sort -t: +2 -3 messages | head

7. uniq
从一个文本中去除或禁止重复行,这里的重复行指的是相邻的!可与sort结合使用。
-c 显示每条记录重复的行数
-u 只显示不重复的行,即-c为1的那些行
-d只显示记录重复的行,但每条只显示1次

8. split
分割文件,分割后的文件为:prefix[aa-zz],例如yyaa,yyab….yyzz
一般格式:split [options] infile outfile_prefix
[options]:
-b n 以大小为n(k,m)分割
-l n 每个文件的分割的行数n
-n 同-l n

几个命令结合使用可以编写一些简单的shell脚本。
例:监测磁盘使用情况,每分钟检测一次,如果快满了(使用超过90%)则给root发封邮件提醒。
[root@node1 ~]# cat space_oversee.sh
#!/bin/bash
#space_oversee.sh by fuyar(417226209).
#to oversee the space usage of all disks.
#oversee per minute.

while [ 1 -lt 2 ]
do
for DISK in `df | awk '{print $1}' | sed -n '2,/$/p'`
        do
                USED=`df | grep "$DISK" |awk '{print $5}'|sed -n 's/%//p'`
                if [ $USED -ge 90 ]
                        then echo "`date`:$DISK is nearly full: ${USED}%."|mail root
                fi
        done
sleep 60
done

该脚本可放后台执行

 

 

===============================================================

 

http://hi.baidu.com/%C1%F5%CB%BC%B3%BC84/blog/item/ced7ccc5bc7ac5e652664fbc.html

 

http://hi.baidu.com/ghosthtf/blog/item/3b12a492cb972704d21b70c7.html

 

http://hi.baidu.com/luoleicn/blog/item/a81e1719ba7ad84342a9ad75.html

 

 

原创粉丝点击