日期:2014-05-16  浏览次数:20634 次

Linux-常用命令(一)

Linux只所以受到程序员的欢迎,一部分原因是本身自带很多有用的命令,再结合shell等脚本就可以玩出很多花样,这里总结一下工作场景中用到的命令。

?

?1)grep:按照某种匹配规则搜索文件,并将符合匹配条件的行输出

场景1:间隔一定时间段的2次tail日志结果保存到文件a和b,取a、b的差集(b-a)得到增量日志

grep -F -v -f a.log b.log | sort | uniq > data.log

补充说明,取(a∩b):?

grep -F -f aa.log bb.log  | sort | uniq
还有comm命令也能实现上面的需求,
comm:Compare sorted files FILE1 and FILE2 line by line.
       With no options, produce three-column output.  Column one contains lines unique to FILE1, column two contains lines unique to FILE2, and column three contains lines common to both files.
       -1     suppress lines unique to FILE1
       -2     suppress lines unique to FILE2
       -3     suppress lines that appear in both files
取a、b的差集(b-a)命令:comm -1 -3 aa.log bb.log

?

2)查找进程pid,并关闭对应进程:一行简单的shell脚本,将pid赋值给变量aa

pid=`ps -ef | grep 'spirit.py' | grep -v grep | awk '{print $2}'`
kill $pid

?说明:grep -v grep xx.log 表示从xx.log去除包含grep字符串的行,awk '{print $2}'表示取出第2列数据,行数据默认以空格分隔

?

3)

?

?

?

?

?

?

?

?