1.sed,
批量替换多个文件中的字符串。
①sed -i "s/oldstring/newstring/g" `grep oldstring -rl yourdir`
②重命名一批文件。
#!/bin/bash
for file in `ls *`
do
mv $file `echo $file|sed -n 's/sql/answer/p'`
done
2. find + -exec先查找,然后做动作。
find . -name *.result -exec rm -f {} \;
find . -name *.result -exec ls -l {} \;
3,awk,功能超级强大。
awk --- 算术运算 除了数字运算也支持substr之类的函数。
1>. 普通运算
[gan@localhost tmp]$ awk 'BEGIN { print 13+3 }' #加
16
[gan@localhost tmp]$ awk 'BEGIN { print 13-3 }' #减
10
[gan@localhost tmp]$ awk 'BEGIN { print 13*3 }' #乘
39
[gan@localhost tmp]$ awk 'BEGIN { print 12/3 }' #除
4
[gan@localhost tmp]$ awk 'BEGIN { print 13/3 }' #除
4.33333
[gan@localhost tmp]$ awk 'BEGIN { print 13%3 }' #求余
1
[gan@localhost tmp]$ awk 'BEGIN { print 13%5 }' #求余
3
[gan@localhost tmp]$ awk 'BEGIN { print 13^2 }' #2次方
169
[gan@localhost tmp]$ awk 'BEGIN { print 13**2 }' #2次方
169
[gan@localhost tmp]$ awk 'BEGIN { print -2 }' #普通负数输出
-2
http://gan.cublog.cn
[gan@localhost tmp]$ awk 'BEGIN { print (2+10)/2+((3^2)/100) }' #综合一点的运算
6.09
[gan@localhost tmp]$ awk 'BEGIN { x="123"; print x-2 }' #将字符串转换为数值来运算
121
http://gan.cublog.cn
2>. awk调用数学函数运算
函数名称 返回值
atan2(x,y) y,x范围内的余切
cos(x) 余弦函数
exp(x) 求幂
int(x) 取整
log(x) 自然对数
rand() 随机数
sin(x) 正弦
sqrt(x) 平方根
srand(x) x是rand()函数的种子
[gan@localhost tmp]$ awk 'BEGIN { print atan2(1,2) }'
0.463648
[gan@localhost tmp]$ awk 'BEGIN { print cos(3) }'
-0.989992
[gan@localhost tmp]$ awk 'BEGIN { print cos(0.9) }'
0.62161
[gan@localhost tmp]$ awk 'BEGIN { print int(1.9923) }'
1
[gan@localhost tmp]$ awk 'BEGIN { print int(4.9923) }'
4
[gan@localhost tmp]$ awk 'BEGIN { print exp(3) }'
20.0855
[gan@localhost tmp]$ awk 'BEGIN { print exp(4) }'
54.5982
[gan@localhost tmp]$ awk 'BEGIN { print log(4) }'
1.38629
[gan@localhost tmp]$ awk 'BEGIN { print rand() }'
0.237788
[gan@localhost tmp]$ awk 'BEGIN { print rand() }'
0.237788
[gan@localhost tmp]$ awk 'BEGIN { print rand(32) }'
awk: fatal: 1 is invalid as number of arguments for rand
[gan@localhost tmp]$ awk 'BEGIN { print sin(32) }'
0.551427
[gan@localhost tmp]$ awk 'BEGIN { print sqrt(4) }'
2
[gan@localhost tmp]$ awk 'BEGIN { print srand(4) }'
1
[gan@localhost tmp]$ awk 'BEGIN { print srand(10) }'
1
[gan@localhost tmp]$ awk 'BEGIN { srand(10); print rand()}'
0.255219
[gan@localhost tmp]$ awk 'BEGIN { srand(10); print rand() }'
0.255219
[gan@localhost tmp]$ awk 'BEGIN { srand(1000); print rand() }'
0.524085
[gan@localhost tmp]$ awk 'BEGIN { srand(10); print rand() }'
0.255219
[gan@localhost tmp]$ awk 'BEGIN { srand(10); print rand() }'
0.255219
3>. 利用数组做复杂的运算
将文件中的数据求和
[gan@localhost ftmp]$ cat file.txt
1
2
3
3
44
66
2
3
78
9900
235
[gan@localhost ftmp]$ awk 'BEGIN {sum=0} {sum+=$0} END{print sum}' file.txt
10337
求平均数:
[gan@localhost ftmp]$ awk 'BEGIN {sum=0} {sum+=$0} END{print sum/FNR}' file.txt
939.727
分组求平均:
[gan@localhost ftmp]$ cat gav.txt
1 100 10
1 200 20
1 300 30
1 500 20
2 100 3
2 300 5
[gan@localhost ftmp]$ awk -F" " '{sum2[$1] += $2; sum3[$1] += $3; cnt[$1] += 1} END{for (i in sum2) print i, sum2[i]/cnt[i], sum3[i]/cnt[i]}' gav.txt
1 275 20
2 200 4
vi的一些操作。(总记不住
)
Vi: Search and Replace
Change to normal mode with .
Search (Wraped around at end of file):
Search STRING forward : / STRING.
Search STRING backward: ? STRING.
Repeat search: n
Repeat search in opposite direction: N (SHIFT-n)
Replace: Same as with sed, Replace OLD with NEW:
First occurrence on current line: :s/OLD/NEW
Globally (all) on current line: :s/OLD/NEW/g
Between two lines #,#: :#,#s/OLD/NEW/g
Every occurrence in file: :%s/OLD/NEW/g
1. copy and paste
yy : copy 光标所在的行
nyy: copy n line
yw: copy 光标所在的单词
nyw: copy 光标所在位置到其后的n 个单词(未必是同一行)
y$: copy 光标所在位置到行尾($是行尾的标示)
ny$: copy 光标所在位置之后的n行(包括当前行,当前行=y$)
p: paste 在光标所在位置之右
P: --------------------------------左
2. delete, 和copy 类似
dd : delete current line
ndd: delete n line
dw: delete current word
ndw: delete n word
d$ : delete to the end of line.
nd$ : delete n line. (current line = d$)
x: delete one character(无论是ascii 还是unicode)
nx: delete n characters.
3. block edit
在命令模式下,输入v 进入块编辑状态
a. 移动光标选定操作快
b. c(cut), y(copy)
c. p or P.
4. undo /redo
u: undo
U: 取消最近一行的改动
crtl +r: redo
e!: 放弃所有改动,重新编辑。
H+行号 移动到行号所在的行。
nG移动到第几行
come from :http://space.itpub.net/52719/viewspace-626674
come from :http://space.itpub.net/52719/viewspace-626674
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/90618/viewspace-626866/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/90618/viewspace-626866/