linux 常用命令

2018-07-04  本文已影响0人  小小_糊涂虫

linux常用命令

文件内容查看命令

文件查看命令主要为cat 、more、less。关于这几个命令在文件命令写得比较详细,大家可以参考。

个人对以上几个命令进行了基本实践操作

cat

➜  Desktop cat >>test1
test1
hello  test

test 3
➜  Desktop cat >> test1
test 4
➜  Desktop cat test1
test1
hello  test

test 3
test 4
➜  Desktop
➜  Desktop cat >test2
test11
测试
➜  Desktop cat >test2
新增
➜  Desktop cat test2
新增

所有 cat >>与cat >的区别是对文件原有内容的处理不一样,如果是不想要原有内容则可以直接用cat > ,如果想要原有内容则用cat >>.为了保险起见还是建议用cat >>命令,大不了可以对原有内容进行删除

➜  Desktop cat test1 test2 >>test3
➜  Desktop cat test3
test1
hello  test

test 3
test 4
新增
➜  Desktop cat >> test3
我再式下
➜  Desktop cat test1 test2 >>test3
➜  Desktop cat test3
test1
hello  test

test 3
test 4
新增
我再式下
test1
hello  test

test 3
test 4
新增

同样我们可以用 cat file1 file2 > file3 ,合并时如果file3中有内容,则会被清除,与新建文件是类似。前面都是在一个目录下面进行文章的合并,如果是不同目录,则只需要加上目录路径就可以

➜  study cat /Users/huxy/Desktop/test1 /Users/huxy/code/test4 > test5
➜  study cat test5
test1
hello  test

test 3
test 4
件下

其他

➜  Desktop cat -n test3
     1  test1
     2  hello  test
     3
     4  test 3
     5  test 4
     6  新增
     7  我再式下
     8  test1
     9  hello  test
    10
    11  test 3
    12  test 4
    13  新增
➜  Desktop cat -b test3
     1  test1
     2  hello  test

     3  test 3
     4  test 4
     5  新增
     6  我再式下
     7  test1
     8  hello  test

     9  test 3
    10  test 4
    11  新增
➜  Desktop cat -s test3
test1
hello  test

test 3
test 4
新增
我再式下
test1
hello  test

test 3
test 4
新增

nl

nl 与cat -n很类似

➜  Desktop nl test3
     1  test1
     2  hello  test

     3  test 3
     4  test 4
     5  新增
     6  我再式下
     7  test1
     8  hello  test

     9  test 3
    10  test 4
    11  新增



    12  test11
➜  Desktop nl -b test3
➜  Desktop nl -b a test3
     1  test1
     2  hello  test
     3
     4  test 3
     5  test 4
     6  新增
     7  我再式下
     8  test1
     9  hello  test
    10
    11  test 3
    12  test 4
    13  新增
    14
    15
    16
    17  test11
➜  Desktop  nl -b a -n rz test3
000001  test1
000002  hello  test
000003
000004  test 3
000005  test 4
000006  新增
000007  我再式下
000008  test1
000009  hello  test
000010
000011  test 3
000012  test 4
000013  新增
000014
000015
000016
000017  test11
➜  Desktop  nl -b a -n rz -w 3 test3
001 test1
002 hello  test
003
004 test 3
005 test 4
006 新增
007 我再式下
008 test1
009 hello  test
010
011 test 3
012 test 4
013 新增
014
015
016
017 test11

more

more命令与cat类似,cat是展示整个文件,而more则是以一页一页的显示方式来显示内容。在网上有一篇比较好的介绍more命令的文章more

less

more命令是一次性加载完所有的记录,二less在查看之前是不会加载整个文件。

这么多命令有时候压根就记不住,所以就需要按下h来查看具体的帮助

head

head 命令显示开头的几行

tail

与head对应的命令就是tail ,查看文章最后几行

现在head 与tail的命令都用了,所以可以联合一起使用。比如

grep

grep为一个强查询命令,是查询日志常用命令

单个简单查询

多个查询

或查询

grep -E 'xx1|xx2' filename 在filename 中查询包含关键字xx1或是xx2的记录,grep -c -E 'activityId=1929|"branchId":8' citybuy.log。

或者直接是使用egrep -c 'activityId=1929|"branchId":8' citybuy.log

或者使用grep -e activityId=1929 -e "branchId":8 filename

与查询

grep XX1 filename |gep XX2 查找同时包含关键字xx1与xx2的行
例如 grep activityId=1929 citybuy.log|grep "branchId":8'找出日志中含有branchId为8且activityId为 1929的日志

grep -E 'pattern1.pattern2' filename grep -E 'activityId=1929."branchId":8' citybuy.log 这个是有顺序的,先pattern1然后才是pattern2.

grep -E 'pattern1.pattern2|pattern2.pattern1' filename可以实现查询包含pattern1,pattern2的文件。

正则表达式

在使用grep命令时经常用到的就是正则表达式,在网上找到了一篇文章对此进行了说明grep 正则表达式

pattern正则表达式主要参数:


    \: 忽略正则表达式中特殊字符的原有含义。
    
    ^:匹配正则表达式的开始行。
    
    $: 匹配正则表达式的结束行。
    
    \<:从匹配正则表达式的行开始。
    
    \>:到匹配正则表达式的行结束。
    
    [ ]:单个字符,如[A]即A符合要求 。
    
    [ - ]:范围,如[A-Z],即A、B、C一直到Z都符合要求 。
    
    .:所有的单个字符。
    
    * :有字符,长度可以为0。
其他操作

以上都是对grep的搜索功能进行说明,有的时候我们是需要对日志进行二次分析的,所以会将过滤数据进行存储

cat -n info.log |grep "XX" >xxx.txt 将过滤出的日志进行存储

cat -n info.log |grep "XX" |more 内容过多时进行可以加more进行分页查看

awk

awk 命令是行处理器,对行的定义是/n来区分。可以对行进行分割取值,然后获取对应的值。通过查阅相关资料,有篇文章对此描述还不错awk 命令awk 入门

分割打印基本使用

默认是空格进行分割,可用 -F‘ XX’进行分割,0是全部,1是分割后得到的第一个参数,$2是分割后得到的第二个参数,以此类推,当然还可以按一定格式进行打印

➜  ~ cat test3
2 this is a tes
3 Are you like awk
This's a test
10 there are orange,apple ,mongo
测试
我们不一样
哈哈哈   ,我去
测试,哈哈
test , test
➜  ~ awk '{print $0,$1, $3}' test3
2 this is a tes 2 is
3 Are you like awk 3 you
This's a test This's test
10 there are orange,apple ,mongo 10 are
测试 测试
我们不一样 我们不一样
哈哈哈   ,我去 哈哈哈
测试,哈哈 测试,哈哈
test , test test test
➜  ~ awk '{printf"%-8s %-10s\n", $1,$4}' test3
2        a
3        like
This's
10       orange,apple
测试
我们不一样
哈哈哈
测试,哈哈
test
➜  ~ awk -F ',' '{print $1,$2}' test3
2 this is a tes
3 Are you like awk
This's a test
10 there are orange apple
测试
我们不一样
哈哈哈   ,我去
测试,哈哈
test   test

内建变量

内建变量有多种,下面就些最基本的进行测试。

awk 'BEGIN{printf "%4s %4s %4s %4s %4s \n","FILENAME","FNR","FS","NF","NR";printf "---------------------------------------------\n"} {printf "%4s %4s %4s %4s %4s  \n",FILENAME,FNR,FS,NF,NR}'  test3
FILENAME  FNR   FS   NF   NR
---------------------------------------------
test3    1         5    1
test3    2         5    2
test3    3         3    3
test3    4         5    4
test3    5         1    5
test3    6         1    6
test3    7         2    7
test3    8         1    8
test3    9         3    9

正则匹配

字符匹配有2种

➜  ~ awk '/are/' test3
10 there are orange,apple ,mongo
➜  ~ awk !'/are/' test3
2 this is a tes
3 Are you like awk
This's a test
测试
我们不一样
哈哈哈   ,我去
测试,哈哈
test , test
➜  ~ awk '$2 ~/Are|this/ {print $0}' test3
2 this is a tes
3 Are you like awk
➜  ~ awk '$2 !~/Are|this/ {print $0}' test3
This's a test
10 there are orange,apple ,mongo
测试
我们不一样
哈哈哈   ,我去
测试,哈哈
test , test

条件表达式

== != > >=用于过滤

~ awk '$1==3||$1=="测试"' test3
3 Are you like awk
测试
➜  ~ awk '$1!=3||$1!="测试"' test3
2 this is a tes
3 Are you like awk
This's a test
10 there are orange,apple ,mongo
测试
我们不一样
哈哈哈   ,我去
测试,哈哈
test , test
➜  ~ awk '$1>3' test3
This's a test
10 there are orange,apple ,mongo
测试
我们不一样
哈哈哈   ,我去
测试,哈哈
test , test

逻辑运算符

&& || 多条件过滤

➜  ~ cat test3
2 this is a tes
3 Are you like awk
This's a test
10 there are orange,apple ,mongo
测试
我们不一样
哈哈哈   ,我去
测试,哈哈
test , test
test1  test2 hahah
test1  test3 go
test   test
➜  ~ awk '$1 ~/test/||$2~/test2/' test3
test , test
test1  test2 hahah
test1  test3 go
test   test
➜  ~ awk '$1 ~/test/&&$2!~/test2/' test3
test , test
test1  test3 go
test   test

条件语句
if语句与while 语句


➜  ~ awk 'BEGIN {A=1}{while (A<NF) print NF,$0 ,A++}' test3
5 2 this is a tes 1
5 2 this is a tes 2
5 2 this is a tes 3
5 2 this is a tes 4
➜  ~ awk 'BEGIN {A=0;B=0}{if ($1>10){ A++} else{B++}} END {print A,"\t"B}' test3
9   3
➜  ~ awk 'BEGIN {A=1}{while (A<NF) print NF,$0 ,A++}' test3
5 2 this is a tes 1
5 2 this is a tes 2
5 2 this is a tes 3
5 2 this is a tes 4

之所以学习以上几个命令,其实是源于日志查询的需要。
对某个字段出现次数进行统计

~ awk -F ' ' '{sum[$1]++} END{for (i in sum) print i "\t" sum[i]}' test3
2   1
3   1
哈哈哈 1
This's  1
测试  1
10  1
我们不一样   1
测试,哈哈   1
test1   2
test    2

 sum[$i] 为对第一列中字符出现的次数进行统计

grep与awk 结合使用

zgrep 'com.gexin.rp.base.log.LogManager.slow' rp-slow-20171212-0.log.gz |awk -F':' '{print($1)}'|sort|uniq -c|sort -t' ' -k1n

以上命令的意思就是包含 查找包含com.gexin.rp.base.log.LogManager.slow每行日志,按照: 分域后取第一个域的值,并计算对应的的总数按照统计值从大到小排列

zgrep 'save_list_body|QX3GgC1oXf9IYWX1LCPVE8' *rp-message-20171211* | awk -F'|' '{print($6)}' | awk -F'=' '{print($5)}'|sort| uniq -c | more



zgrep 'QX3GgC1oXf9IYWX1LCPVE8' *rp-bi-20180105* |grep 'RASL'|grep '2018-01-05 '|wc -l

sort 命令补充

cat file1.txt file2.txt | sort | uniq > file.txt 并集
cat file1.txt file2.txt | sort | uniq -d >file.txt 交集
 差集:求file1.txt相对于file2.txt的差集,可先求出两者的交集temp.txt,然后在file1.txt中除去temp.txt即可。

     cat file1.txt file2.txt | sort | uniq -d >temp.txt

     cat file1.txt temp.txt | sort | uniq -u >file.txt

自己在网上查到一篇关于sort命令sort命令

上一篇 下一篇

猜你喜欢

热点阅读