推荐一本书:《持续交付--发布可靠软件的系统方法》

linux技巧

2012年5月10日 6 views 没有评论

1 cat ~/.ssh/id_rsa.pub | ssh -p port user@server “cat – >> ~/.ssh/authorized_keys”  #ssh-copy-id类似

2 awk ‘BEGIN{srand()}{a[NR,rand()]=$0}END{y=0;for(i in a){print a[i];y++;if(y==num){exit;}}}’  file #从file中随机出num行显示出来

分类: linux一行代码技巧 标签:

python找到ip段可用ip最大和最少ip

2012年4月27日 8 views 没有评论

前言:一个小需求,当用户输入‘XXX.XXX.XXX.XXX/XX’(ip/netmask)时候自动帮他分析出这个网段除去网络号和广播地址外的开始和结束ip

def ipto(iplist):
    data = iplist.split('/')
    ip= data[0]
    ti = int(data[1])
    d = int(ti/8)
    c = 256/(2**(ti%8))
    ip_items = ip.split('.')
    if len(ip_items[d:]) == 1:
        if ti%8 == 0:
            cmin = '%s.%s' % ('.'.join(ip_items[:d]),'0')
            cmax = '%s.%s' % ('.'.join(ip_items[:d]),'255')
        else:
            for i in range(2**(ti%8)):
	        mymax = (i+1)*c-1
	        mymin=  i*c
	        data =  int(''.join(ip_items[d:]))
	        if data < mymax and data >= mymin:
	            cmin = '%s.%s' % ('.'.join(ip_items[:d]),mymin)
  	            cmax = '%s.%s' % ('.'.join(ip_items[:d]),mymax)
    else:
        if ti%8 == 0:
             cmin = '%s.%s.%s' % ('.'.join(ip_items[:d]),'0',('0.'*(len(ip_items)-d-1))[:-1])
             cmax = '%s.%s.%s' % ('.'.join(ip_items[:d]),'255',('255.'*(len(ip_items)-d-1))[:-1])
        else:
	    for i in range(2**(ti%8)):
                mymax = (i+1)*c-1
                mymin=  i*c
	        data =  int(''.join(ip_items[d]))
	        if data < mymax and data >= mymin:
		    cmin = '%s.%s.%s' % ('.'.join(ip_items[:d]),mymin,('0.'*(len(ip_items)-d-1))[:-1])
		    cmax = '%s.%s.%s' % ('.'.join(ip_items[:d]),mymax,('255.'*(len(ip_items)-d-1))[:-1])
    print  cmin,cmax
分类: python 标签:

python终端显示彩色字符

2012年4月27日 14 views 没有评论

前言:作一个脚本或者程序在需要发音很多输出的时候,默认的白色会让人看的很困,但是有了彩色做个分割线或者让一些重要信息用彩色字体打印出来就好多了,以前一直在用模块实现,后来发现print借用ANSI控制码原来也可以:参见:http://www.oschina.net/code/snippet_87675_8599

1分析这个程序

#!/usr/bin/env python
#-*-coding:utf-8-*-
#Filename:
 
__author__ = "maqiang.jacky <maqiang.jacky@snda.com>"
__version__ = 1.0
__date__ = "12-2-9"
 
"""
0  All attributes off 默认值
1  Bold (or Bright) 粗体 or 高亮
4  Underline 下划线
5  Blink 闪烁
7  Invert 反显
30 Black text
31 Red text
32 Green text
33 Yellow text
34 Blue text
35 Purple text
36 Cyan text
37 White text
40 Black background
41 Red background
42 Green background
43 Yellow background
44 Blue background
45 Purple background
46 Cyan background
47 White background
"""
def main():
    """ """
for atrr in [0,1,4,5,7]:   0表示无属性,1表示高亮,4加下划线,5表示闪烁 ,7反显 ,8消隐
    print "attribute %d ------------------------------" % atrr
    for fore in [30,31,32,33,34,35,36,37]:
        for back in [40,41,42,43,44,45,46,47]:
            color = "\x1B[%d;%d;%dm" % (atrr,fore,back)  #除了atrr,分别代表 字的颜色,底色
            print "%s %d-%d-%d\x1B[0m" % (color,atrr,fore,back),
        print ""
if __name__ == "__main__":
    """ """
    main()

其中

字背景颜色范围:40----49
40:黑
41:深红
42:绿
43:黄色
44:蓝色
45:紫色
46:深绿
47:白色

字颜色:30-----------39
30:黑
31:红
32:绿
33:黄
34:蓝色
35:紫色
36:深绿
37:白色

因为我不需要底色,自己写了一个方法:

def myprint(color,mes):
    if color == 'r':
        fore = 31
    elif color == 'g':
        fore = 32
    elif color == 'b':
        fore = 36
    elif color == 'y':
        fore = 33
    else:
        fore = 37
    color = "\x1B[%d;%dm" % (1,fore)
    print "%s %s\x1B[0m" % (color,mes)

假如想显示一段信息为红色:

dongwm@linux-dongwm:~> myprint('r','some messages')

关于AWK研究(五)

2012年4月23日 11 views 没有评论

1 IGNORECASE忽略大小写

dongwm@linux-dongwm:~> awk ‘/aaa/{print}’ test.txt   #默认IGNORECASE是0
dongwm@linux-dongwm:~> awk ‘BEGIN{IGNORECASE=1}/aaa/{print}’ test.txt
115,AAA
2 ERRNO  错误输出
dongwm@linux-dongwm:~> cat test.awk
{
print $0;
x = getline < “dummy-file.txt”
if ( x == -1 )
print ERRNO   #当出现一个I/O错误,会把ERRNO变成输出信息
else
print $0;
}

dongwm@linux-dongwm:~> awk -f test.awk test.txt
112,dongwm,dongwm.com,1120
没有那个文件或目录
113,ailll,ailll.com,1130
没有那个文件或目录
114,ccc,ccc.com,1140
没有那个文件或目录
3 用户自定义方法

语法:

function fn-name(parameters)
{
function-body
}
dongwm@linux-dongwm:~> cat test.awk
{
i=1; total=0;
while (i <= NF) {
mydebug(“quantity is ” $i);
total = total + $i;
i++;
}
print “Item”, $1, “:”,”num is”,total;
}
function mydebug ( message ) {
printf(“DEBUG[%d]>%s\n”, NR, message )
}

dongwm@linux-dongwm:~> awk -f test.awk test.txt
DEBUG[1]>quantity is 112,dongwm,dongwm.com,1120
Item 112,dongwm,dongwm.com,1120 : num is 112
DEBUG[2]>quantity is 113,ailll,ailll.com,1130
Item 113,ailll,ailll.com,1130 : num is 113
DEBUG[3]>quantity is 114,ccc,ccc.com,1140
Item 114,ccc,ccc.com,1140 : num is 114

4 其他语言输出

dongwm@linux-dongwm:~> cat test.awk   # 第一步 建立文件域
BEGIN {
FS=”,”
TEXTDOMAIN = “test”   #这个很重要 最后的.mo文件要一样
bindtextdomain(“.”)
print _”START_TIME:” strftime(“%a %b %d %H:%M:%S %Z%Y”,systime());
printf “%-3s\t”, _”Num”;   #‘_’表示定制,打印时候是不显示的
printf “%-10s\t”, _”Description”
printf “%-10s\t”, _”Domain”
printf “%-5s\t”, _”Price”
printf”—————————————————–\n”
}
{
printf “%-3d\t%-10s\t%-10s\t$%-.2f\n”,
$1,$2,$3,$4
}

dongwm@linux-dongwm:~> awk -f test.awk test.txt  #格式化输出为下面
START_TIME:一  4月 23 10:19:29 CST2012
Num    Description    Domain        Price    —————————————————–
112    dongwm        dongwm.com    $1120.00
113    ailll         ailll.com     $1130.00
114    ccc           ccc.com       $1140.00
dongwm@linux-dongwm:~> gawk –gen-po -f test.awk > test.po  #第二步  创建.po文件(portable object file  便携式对象文件)
dongwm@linux-dongwm:~> cat !$
cat test.po
#: test.awk:5
msgid “START_TIME:”
msgstr “”

#: test.awk:6
msgid “Num”
msgstr “”

#: test.awk:7
msgid “Description”
msgstr “”

#: test.awk:8
msgid “Domain”
msgstr “”

#: test.awk:9
msgid “Price”
msgstr “”
第三步 修改.po文件,比如有一行:msgid “START_TIME:”,假如想显示成“Report Generated On:” 可以这样:

#: test.awk:5
msgid “START_TIME:”
msgstr “Report Generated On:”
dongwm@linux-dongwm:~> msgfmt -v test.po  #第四步 建立消息对象
1 条已翻译消息,4 条未翻译消息.
dongwm@linux-dongwm:~> ll messages.mo   #生成了这个文件
-rw-r–r– 1 dongwm users 89  4月 23 10:25 messages.mo
dongwm@linux-dongwm:~> mkdir -p en_US/LC_MESSAGES   #拷贝这个文件到你想要的语言目录
dongwm@linux-dongwm:~> mv messages.mo en_US/LC_MESSAGES/test.mo
5 双向通讯

dongwm@linux-dongwm:~> cat test.awk
BEGIN {
command = “sed ‘s/Awk/Sed and Awk/’”
print “Awk is Great!” |& command                #“|&”表明它是一个双向的沟通。
close(command,”to”);
command |& getline tmp  #进程完成,按行输出显示
print tmp;
close(command);
}
dongwm@linux-dongwm:~> awk -f test.awk
Sed and Awk is Great!
6使用系统命令方法

dongwm@linux-dongwm:~> awk ‘BEGIN { system(“pwd”) }’
/home/dongwm
dongwm@linux-dongwm:~> awk ‘BEGIN { system(“date”) }’
2012年 04月 23日 星期一 10:49:10 CST
7 时间戳方法

dongwm@linux-dongwm:~> awk ‘BEGIN { print systime() }’
1335149444
dongwm@linux-dongwm:~> awk ‘BEGIN { print strftime(“%c”,systime()) }’
2012年04月23日 星期一 10时50分53秒

分类: awk, linux基础 标签:

关于awk研究(四)

2012年4月20日 15 views 没有评论

1 printf

“print format”, variable1, variable2, etc.
dongwm@linux-dongwm:~> awk ‘BEGIN { printf “Field 1\t\tField 2\tField 3\tField 4\n” }’
Field 1        Field 2    Field 3    Field 4  #格式化输出
dongwm@linux-dongwm:~> awk ‘BEGIN { printf “Field 1\vField 2\vField 3\vField 4\n” }’
Field 1
Field 2
Field 3
Field 4
dongwm@linux-dongwm:~> awk ‘BEGIN \
> { printf “Field 1\bField 2\bField 3\bField 4\n” }’   #\b退格
Field Field Field Field 4

下面2个例子:

dongwm@linux-dongwm:~> awk ‘BEGIN {FS=”,”;OFS=”:”;ORS=”\n–\n”;} {print $2,$3}’ test.txt
dongwm:dongwm.com

ailll:ailll.com

ccc:ccc.com

dongwm@linux-dongwm:~> awk ‘BEGIN {FS=”,”;OFS=”:”;ORS=”\n–\n”;} {printf “%s:%s\n\n”,$2,$3}’ test.txt #这里就是格式化输出,%s就是字符串 ,前台2个%s,就是后面的$2 $3
dongwm:dongwm.com

ailll:ailll.com

ccc:ccc.com

dongwm@linux-dongwm:~> awk ‘BEGIN {FS=”,”;OFS=”:”;ORS=”\n–\n”;} {printf “%10s:%3s:%10s:%-7s\n\n”,$1,$2,$3,$4}’ test.txt
112:dongwm:dongwm.com:1120   #固定列宽,正数是右对其,负数左对齐

113:ailll: ailll.com:1130

114:ccc:   ccc.com:1140
dongwm@linux-dongwm:~> awk ‘BEGIN { printf “%6s\n”, “Good Boy!” }’
Good Boy!
dongwm@linux-dongwm:~> awk ‘BEGIN { printf “%.6s\n”, “Good Boy!” }’  #这个‘.’说明这里只保留6个字符
Good B
dongwm@linux-dongwm:~> awk ‘BEGIN { printf “%6s\n”, substr(“Good Boy!”,1,6) }’  #效果是一样的
Good B
2 内置数值函数
1 int

dongwm@linux-dongwm:~> awk ‘BEGIN{print int(3.534);print int(4);print int(-5.223);print int(-5);}’
3
4
-5
-5
2 log  #对数,不记得了可以翻翻高中数学课本^.^

dongwm@linux-dongwm:~> awk ‘BEGIN{print log(13);print log(0);print log(1);print log(-5);}’ #
2.56495
-inf
0
awk: 警告: log: 收到负数参数 -5
nan
3 sqrt #平方根
dongwm@linux-dongwm:~> awk ‘BEGIN{print sqrt(64);print sqrt(0);print sqrt(-5);}’
8
0
awk: 警告: sqrt: 收到负数参数 -5
-nan
4 exp #e的N次方

dongwm@linux-dongwm:~> awk ‘BEGIN{print exp(123456789);print exp(0);print exp(-12);}’
awk: 警告: exp: 参数 1.23457e+08 超出范围
inf
1
6.14421e-06
3 随机数

1 rand #0-1之间的随机数

cat test.awk
BEGIN {
while(i<1000)
{
n = int(rand()*100);
rnd[n]++;
i++;
}
for(i=0;i<=5;i++) {
print i,”Occured”, rnd[i], “times”;
}
}

dongwm@linux-dongwm:~> awk -f test.awk
0 Occured 11 times
1 Occured 8 times
2 Occured 9 times
3 Occured 15 times
4 Occured 16 times
5 Occured 5 times
2  srand #初始化随机数的开始
dongwm@linux-dongwm:~> cat test.awk
BEGIN {
srand(5);
total=5;
max=50;
count=0;  #随机数从5-50
while(count < total) {
rnd = int(rand() * max);
if ( array[rnd] == 0 ) {
count++;
array[rnd]++;
}
}
for ( i=5; i<=max; i++) {
if ( array[i] )
print i;
}
}

dongwm@linux-dongwm:~> awk -f test.awk
14
16
23
33
35
4 字符串函数
1 index #字符串索引

dongwm@linux-dongwm:~> awk ‘BEGIN {dongwm=”dongwm.com”;print “string start at”,index(dongwm,”wm”);}’  #找到‘wm’的索引
string start at 5

2 length #字符串长度
dongwm@linux-dongwm:~> awk ‘{print length($0)}’ test.txt
26
24
20
3 split  #分隔

语法:split(input-string,output-array,separator)
dongwm@linux-dongwm:~> awk ‘{split($0,qu,”,”);for (x in qu) print qu[x];}’ test.txt
1120
112
dongwm
dongwm.com
1130
113
ailll
ailll.com
1140
114
ccc
ccc.com
4 Substr
语法:substr(input-string, location, length)
dongwm@linux-dongwm:~> awk ‘BEGIN{FS=”,”} {print substr($1,3)}’ test.txt #
2
3
4
dongwm@linux-dongwm:~> awk ‘BEGIN{FS=”,”} {print substr($1,1,3)}’ test.txt  #第一个字段,从第一个字符开始打印3个字符
112
113
114
5 nawk和gwak特有字符串函数:

语法:sub(original-string,replacement-string,string-variable)
dongwm@linux-dongwm:~> awk ‘BEGIN{dongwm=”dongwmDongmcom”;sub(“[Dd]o”,”CC”,dongwm);print dongwm;}’  test.txt #用‘CC’替换‘Do‘或者’do‘
CCngwmDongmcom  #只题换了第一个

语法:gsub(original-string,replacement-string,string-variable)

dongwm@linux-dongwm:~> awk ‘BEGIN{dongwm=”dongwmDongmcom”;gsub(“[Dd]o”,”CC”,dongwm);print dongwm;}’  test.txt
CCngwmCCngmcom  #替换了所有符合的
语法:match(input-string,search-string)
dongwm@linux-dongwm:~> awk ‘BEGIN{dongwm=”dongwmDongmcom”;if(match(dongwm,”dongwm”));{print “match!”}}’  test.txt
match! #搜索发现匹配
dongwm@linux-dongwm:~> awk ‘BEGIN{dongwm=”dongwmDongmcom”;print tolower(dongwm)}’ #转化成小写
dongwmdongmcom
dongwm@linux-dongwm:~> awk ‘BEGIN{dongwm=”dongwmDongmcom”;print toupper(dongwm)}’ #转化成大写
DONGWMDONGMCOM

分类: awk, linux基础 标签:

无觅相关文章插件,快速提升流量