uniq [options][file1 [files]]
$ uniq -c file
file中的重复行输出一次,并在每行前显示重复次数
$ uniq -d file
file中的重复行输出一次,但不输出唯一的行
$ uniq -u file
只输出file中的唯一行
$ uniq file1 file2
把file1中的重复的相邻行删除,并把每行的一个拷贝送到file2
日志文件mtasvr.log格式如下:
T:24583088(04:02:06)[root:Info]
6KqowLDLAgC93DFIKrENAA==.41S2:from=
,to=,
queued
T:122428336(13:36:51)[root:Info] 6KqowLAbAAByYzJIZGsOAA==.2W:from=,to=, queued
需要统计从14:27:20到15:26:41之间包含queued的数量。
最初的想法,用grep匹配queued,然后用awk取出()中间的时间,最后用wc来统计行数,得到下面这条语句:
cat mtasvr.log |grep queued |awk -F\( '{print $2}' |awk -F\) '{if
($1>="14:27:20" && $1<="15:26:41") {print $1}}' |wc -l
接着觉得两次awk取出括号()中的时间比较繁琐。决定用sed替换掉括号再统计,会比较好看:
cat mtasvr.log |grep 'queued' |sed "s/[()]/;/g" |awk -F\; '{if
($2>="14:27:20" && $2<="15:26:41") {print}}' |wc -l
然后,想法应该去掉cat和grep。cat本身就没必要,grep的匹配也可以直接用sed来完成
sed "/queued/s/[()]/;/g" mtasvr.log |awk -F\; '{if ($2>="14:27:20" && $2<="15:26:41") {print}}' |wc -l
最后把wc统计行数也去掉,只用sed和awk来完成这个任务。
sed "/queued/s/[()]/;/g" mtasvr.log |awk -F\; '{if($2>="14:27:20" && $2<="15:26:41") m++} END{print m}'
总结是sed和awk很强大,当然有些简化也没必要。
比如wc就能很好的完成统计行数的任务,没必要再去想awk怎么来实现。
Sort command to sort IP address
Here is our sample input file:
192.168.1.100
192.168.1.19
192.168.1.102
192.168.2.1
192.168.0.2
Type the following sort command:
$ sort -t . -k 3,3n -k 4,4n /path/to/file
Sample output:
192.168.0.2
192.168.1.19
192.168.1.100
192.168.1.102
192.168.2.1
Where,
- -t . : Set field to . (dot) as our IPs separated by dot symbol
- -n : Makes the program sort according to numerical value
- -k opts: Sort data / fields using the given column
number. For example, the option -k 2 made the program sort using the
second column of data. The option -k 3,3n -k 4,4n sorts each column.
First it will sort 3rd column and then 4th column.
在下可以使用sort命令对相应的文件输出进行排序,但是对输出的信息的
指定字段进行排序的用法却和其他的操作系统,如Solaris, 有非常大的区别.如根据输出信息的第5个字段(例子中为文件的大小)进行降序排序
linux下的命令和输出为:
/home/deve:>ls -atl | sort +4 -5 -r -g
-rw-r----- 1 deve deve 316089 Jun 22 2007 telnetd.tar.bz2
-rw-r----- 1 deve deve 33525 Jun 22 2007 telnet-server.rpm
-rw------- 1 deve deve 10113 Oct 21 16:00 .bash_history
drwxr-xr-x 6 deve deve 4096 Jul 13 2006 .gnome2
drwxr-xr-x 3 root root 4096 May 30 2006 ..
drwxr-xr-x 3 deve deve 4096 May 10 2006 .kde
drwxr-xr-x 3 deve deve 4096 Jul 13 2006 .nautilus
drwxr-xr-x 2 deve deve 4096 Jul 13 2006 .gnome-desktop
drwxrwxr-x 3 deve deve 4096 Feb 25 temp
drwxrwxr-x 2 deve deve 4096 Feb 9 2007 ora-rpm
...
Solaris下的命令和输出为:
/export/home/deve> ls -alt | sort +4rn
-rw-r--r-- 1 deve staff 14584335 Jun 27 09:07 English.rar
drwxr-xr-x 2 deve staff 4608 Mar 30 2006 usetip
drwxr-xr-x 2 deve staff 2048 Jan 31 2008 temp
drwx------ 2 deve staff 1536 Mar 28 2006 sysinfo
drwxr-xr-x 2 deve staff 1024 Mar 30 2006 backup
drwxr-xr-x 2 deve staff 512 Apr 24 2006 others
wget `curl -s | grep googlecto|grep -v href`
阅读(794) | 评论(0) | 转发(0) |