似水年华

在夏之中落寞…. 爱上那似水的年华

uniq和sort的一次亲密接触

with 3,655 comments

我学习linux也有段时间了,从字面意思理解uniq就是删除重复行,而sort当然是职业排序。直到某个漆黑的夜晚的那次亲密接触… …

那时我在做道百度面试题:

处理文本如下:

http://www.baidu.com/index.html

http://www.baidu.com/1.html

http://post.baidu.com/index.html

http://mp3.baidu.com/index.html

http://www.baidu.com/3.html

http://post.baidu.com/2.html

得到如下结果: 

域名的出现的次数 域名

3 www.baidu.com

2 post.baidu.com 

1 mp3.baidu.com 

可以使用bash/perl/php/c任意一种

Read the rest of this entry »

Written by silion

六月 22, 2010 at 6:15 下午

Posted in bash

Tagged with , ,

[Shell脚本]计算1+2+3+…+100的和并输出

with 2,125 comments

睡前看了下google reader订阅,发现这里有几个题挺有意思。刚学完bash,顺便来练练手。

一、for循环实现

   1: #!/bin/bash

   2: sum=0

   3:

   4: for ((i=1;i<=100;i++))

   5: do

   6:         ((sum = sum + i))

   7: done

   8:

   9: echo $sum

运行时间:

   1: [root@localhost bash]# time ./sum100.sh

   2: 5050

   3:

   4: real    0m0.008s

   5: user    0m0.003s

   6: sys     0m0.004s

二、函数递归

Read the rest of this entry »

Written by silion

六月 19, 2010 at 5:36 下午

Posted in bash

Tagged with

搭建无盘linux–LTSP

with 3,007 comments

1、简介

什么是LTSP?  LTSP 是 “Linux Terminal Server Project”的缩写。安装在一台服务器上,他能够支持许多带有完全相同环境工作站(叫作瘦客户端)。所有的应用都在服务器端运行,而你   可以使用老旧的 PC 进入到X终端。这可以减小花费和维护,特别是在你所登录的每台电脑需要一致工作空间的地方,例如,在学校或是公司。

2、安装

1. 准备工作:

  1. 设置实验服务器ip为静态ip,最好关闭防火墙。实验需要至少两台机,建议虚拟机使用host-only,因为实验中使用到了dhcp服务。
  2. LTSP依赖下面的软件包或服务:

先把这些rpm包安装好。

Xfree/X-window/KDE/GNOME

DHCP

NFS

TFTP

XINETD

2. 配置NFS

#vim /etc/exports

/opt/ltsp/i386 *(rw,sync)

# server portmap start

# server nfs restart

Read the rest of this entry »

Written by silion

六月 12, 2010 at 10:11 上午

Posted in linux

Tagged with , ,

高效处理apache日志

with 4,383 comments

        通常apache日志的格式如下:

19021 id.zdanswer.cn 222.209.211.147 - - [10/Sep/2009:12:47:10 +0800] GET /afsunion/xdpsrp.js HTTP/1.1 200 1674 http://www.baidu.com/s?dn=http%3A%2F%2Fwww.baidu.com%2Fs&pc=103&ctype=2&wd=%C9%C1%C1%C1%B5%C4%C8%D5%D7%D3+%C2%DE%B4%F3%D3%D3&tn=13800_pg Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Sicent) 
    一般是将日志轮滚,并将前期日志处理后导入mysql数据库,再进行数据挖掘和分析。但是默认的数据格式mysql
不支持。所以我们将其按需求转成如下的格式:
 
 2009-09-10 12:47:19    222.209.211.147 id.zdanswer.cn  /afsunion/3mansotrack.php?oc=richtech1_pg&iad=2&1252557749234   200     http://www.baidu.com/s?dn=http%3A%2F%Fwww.baidu.com%2Fs&pc=103&ctype=2&wd=%B0%A2%B0%D3%CA%A6%D7%A8&tn=13800_pg

  这里用到awk和sed,采用awk内置函数,充分发挥awk的性能,在一台赛扬2.2G CPU,2G内存的机器上处理500多MB
的apache日志花费2分30秒。可以说还算高效的,哈哈。各位看官有没有更好的办法啊?咱们一起学习。
 
awk  ‘{$6=substr($6,2,20) ;$13=gensub(/["]/,”",”g”,$13) ; print $6″\t”$3″\t”$2″\t”$9″\t”$11″\t”$13}  $1 |
 sed  -e ‘{s/\(.*\)\/\(.*\)\/\(….\):\(.*\)/ \3-\2-\1 \4 /;s/Jan/01/;s/Feb/02/;s/Mar/03/;s/Apr/04/;s/May/05/;s/Jun/06/;s/Jul/07/;s/Aug/08/;s/Sep/09/;s/Oct/10/;
s/Nov/11/;s/Dec/12/}’
 > /tmp/s1.log


PS:live writer的代码高亮用的很爽,哪天能直接支持啊…??

Written by silion

六月 8, 2010 at 2:29 下午

Posted in bash

Tagged with , ,