Thursday, July 14, 2016

linux命令记录

1.
  1. $ while true; do date >> date.txt ; sleep 5 ; done &
  2. avi@deb:~$ (cd /home/avi/Downloads/)
  3. ctrl+l 效果等于clear

排序

假设先按第6列数字倒序,再此基础上再按第5列顺序排列:
sort -k6nr -k5n file.txt
注意不能用以下排序方法:
sort -k6 -nr -k5 -n file.txt
这样最后结果还是按第五列排了

linux 如何显示一个文件的某几行(中间几行)

【一】从第3000行开始,显示1000行。即显示3000~3999行
cat filename | tail -n +3000 | head -n 1000

【二】显示1000行到3000行
cat filename| head -n 3000 | tail -n +1000

*注意两种方法的顺序

分解:
    tail -n 1000:显示最后1000行
    tail -n +1000:从1000行开始显示,显示1000行以后的
    head -n 1000:显示前面1000行

【三】用sed命令

 sed -n '5,10p' filename 这样你就可以只查看文件的第5行到第10行


判断是否为空


判断一个脚本中的变量是否为空,我写了一个这样的shell脚本:

  1. #!/bin/sh
  2. #filename: test.sh
  3. para1=
  4. if [ ! -n $para1 ]; then
  5.  echo "IS NULL"
  6. else
  7.  echo "NOT NULL"
  8. fi

然后把该脚本:test.sh通过chmod +x 改为可以执行的脚本,执行后输出的结果为: NOT NULL,很是奇怪,最后,通过查询一些资料发现,可以通过如下方式判断一个shell变量是否为空:
1. 变量通过" "引号引起来
      如下所示:,可以得到结果为 IS NULL.
  1. #!/bin/sh
  2. para1=
  3. if [ ! -n "$para1" ]; then
  4.  echo "IS NULL"
  5. else
  6.  echo "NOT NULL"
  7. fi

2. 直接通过变量判断
      如下所示:得到的结果为: IS NULL
  1. #!/bin/sh
  2. para1=
  3. if [ ! $para1 ]; then
  4.  echo "IS NULL"
  5. else
  6.  echo "NOT NULL"
  7. fi

3. 使用test判断
    得到的结果就是: dmin is not set!  
  1. #!/bin/sh
  2. dmin=
  3. if test -z "$dmin"
  4. then
  5.  echo "dmin is not set!"
  6. else
  7.  echo "dmin is set !"
  8. fi


4. 使用""判断
  1. #!/bin/sh
  2. dmin=
  3. if [ "$dmin" = "" ]
  4. then
  5.  echo "dmin is not set!"
  6. else
  7.  echo "dmin is set !"
  8. fi

#!/bin/bash
a="zzz"
if [[ "$a" = "" ]]
then
echo $a empty
else
echo $a no empty
fi

if [ -z "$VAR" ] ; then
command
fi

先写一些基本语法:

1、字符串判断

str1 = str2       当两个串有相同内容、长度时为真
str1 != str2      当串str1和str2不等时为真
-n str1        当串的长度大于0时为真(串非空)
-z str1        当串的长度为0时为真(空串)
str1           当串str1为非空时为真

2、数字的判断

int1 -eq int2    两数相等为真
int1 -ne int2    两数不等为真
int1 -gt int2    int1大于int2为真
int1 -ge int2    int1大于等于int2为真
int1 -lt int2    int1小于int2为真
int1 -le int2    int1小于等于int2为真

3 文件的判断

-r file     用户可读为真
-w file     用户可写为真
-x file     用户可执行为真
-f file     文件为正规文件为真
-d file     文件为目录为真
-c file     文件为字符特殊文件为真
-b file     文件为块特殊文件为真
-s file     文件大小非0时为真
-t file     当文件描述符(默认为1)指定的设备为终端时为真

3、复杂逻辑判断

-a         与
-o        或
!        非


grep精准匹配


面试时问到一个问题,要精确的找出进程名为abc,判断进程的数量是否在3-5之间,如果不在,就纪录下相关的信息
用ps aux | grep abc | grep –v grep | wc –l 统计出数量再进行判断
但是忽略了一点,如果是有进程名为abcd,abcde等有包含"abc"字符的,那么判断将会不准备,如果精确的匹配到abc呢,当时不知道,只能很诚实的回答,不清楚
其实答案很简单,用grep –w "abc" 或者是grep "\<abc\>"都可以实现
-w, --word-regexp         强制 PATTERN 仅完全匹配字词
纪录一下


隔几秒执行一条命令 ,监视器
watch -dn 'wc -l log.txt'

查看磁盘使用率
df -TH

更改权限
sudo chown -R yonghuming wenjianjia

计算md5
md5sum
查看文件大小
df -Th 用户查看一级目录大小
du -h 查看文件夹大小
du -h --max-depth=1 查看当前文件夹下文件夹大小
ls -lh 查看文件大小

#!/bin/bash
echo `dirname $0`/mac_open_hour.csv
查找文件,并且打印文件并匹配
find 20170611*.log | xargs cat |grep -E '22011716|2285906|1768039' > 1206_20170611.log
解压多个文件到指定目录
find 20170704*.zip| xargs -n1 unzip -d /haha/temp_log

grep使用http://www.cnblogs.com/luojinping/p/3380771.html

stat file 查看文件修改时间等


50
down voteaccepted
What's wrong with the good old,
for pid in $(ps -ef | grep "some search" | awk '{print $2}'); do kill -9 $pid; done
There are ways to make that more efficient,
for pid in $(ps -ef | awk '/some search/ {print $2}'); do kill -9 $pid; done
and other variations, but at the basic level, it's always worked for me.



删除特定的文件
find . -type f -name '* *' -delete
ll | egrep '\(*\)' | xargs -0 rm



Thursday, May 26, 2016

xshell自动登录跳转服务器

工作环境从新回到linux环境,又开始面对hadoop,hive,hbase……
面临的第一个问题,是访问生产环境需要先登录堡垒机,然后再跳转机器,每天打开xshell的第一件事儿就是登录,如果自动登录多好。


1.这个链接给出了具体的方法 http://blog.csdn.net/fly542/article/details/7513571
2.其实就是先配置堡垒机登录,然后配置跳转机器,expect的意思就是对应什么输出的时候,给什么样的输入,我面对的是usmshell的环境,登录方法是堡垒机成功连接后,输入“: ssh ”

3.备忘 sz rz

4.winscp 白名单添加 vim /etc/services_hosts_allow

Wednesday, May 4, 2016

caravel使用踩过的坑

1.创建数据库的时候发现报错,解决的办法是对加密模块进行降级,降级到1.2.1

2. 按照官方配置,创建caravel_config.py,且放到Python搜索路径下面
#---------------------------------------------------------
# Caravel specifix config
#---------------------------------------------------------
ROW_LIMIT = 5000
WEBSERVER_THREADS = 8

CARAVEL_WEBSERVER_PORT = 8088
#---------------------------------------------------------

#---------------------------------------------------------
# Flask App Builder configuration
#---------------------------------------------------------
# Your App secret key
SECRET_KEY = '\2\1thisismyscretkey\1\2\e\y\y\h'

# The SQLAlchemy connection string to your database backend
# This connection defines the path to the database that stores your
# caravel metadata (slices, connections, tables, dashboards, ...).
# Note that the connection information to connect to the datasources
# you want to explore are managed directly in the web UI
SQLALCHEMY_DATABASE_URI = 'sqlite:////tmp/caravel.db'

# Flask-WTF flag for CSRF
CSRF_ENABLED = True


3.针对编码问题,修改viz.py ,目前数据库中文问题还没有解决

Thursday, March 17, 2016

汽车之家新车销售分析

没有进入真正的分析之前,我们先来看几个关于汽车之家车商城的数据(公开发布,咱们只看趋势好了,数据我去掉了):
1.销售趋势
车商城在15年4季度迎来销售高峰
2.买车的热门城市
3.车主偏爱的颜色
4.以北京为例,采购多来自经销商
怎么判断是经销商呢?上面的886买的车其实是由下面的人贡献的
5.卖的最好的车
6.真实的买家,很喜欢用手机号做用户名呀,如果我是卖保险的,那机会来了
7.大家偏爱的购买时间


Wednesday, March 9, 2016

随机森林变量重要性

数据分析或挖掘中,探查事件的驱动因素和特征选择,都牵涉到变量重要性的问题,scikit-learn 树模型相关,都提供了对变量重要性的判定,比如:http://scikit-learn.org/stable/auto_examples/ensemble/plot_forest_importances.html

但是这个变量重要性到底怎么算的呢?


http://scikit-learn.org/stable/modules/tree.html中介绍了gini和信息熵的计算

http://stackoverflow.com/questions/15810339/how-are-feature-importances-in-randomforestclassifier-determined 中提到这些

通过上述描述,找到这个http://papers.nips.cc/paper/4928-understanding-variable-importances-in-forests-of-randomized-trees.pdf



这里给出了例子http://stats.stackexchange.com/questions/92419/relative-importance-of-a-set-of-predictors-in-a-random-forests-classification-in




这下终于搞明白了
再看一个例子
http://blog.datadive.net/selecting-good-features-part-iii-random-forests/