1.查找awk
# cat /etc/passwd |awk -F ':' 'BEGIN {print "name,shell"} {print $1","$7} END {print "blue,/bin/nosh"}' name,shell (Begin信息) root,/bin/bash daemon,/bin/sh bin,/bin/sh sys,/bin/sh .... blue,/bin/nosh (End信息)
# awk –F ’,’ ’{print $1,$2}’ ./test.txt (-F文件符切割)
#源文件 There areorange,apple,mongo,xiangjiao,boluo,yezi
#输出后文件 There are orangeapple
1、打印文件的第一列(域) : awk '{print $1}' filename
2、打印文件的前两列(域) : awk '{print $1,$2}' filename
3、打印完第一列,然后打印第二列 :awk '{print $1 $2}' filename
4、打印文本文件的总行数 : awk 'END{print NR}' filename
5、打印文本第一行 :awk 'NR==1{print}' filename
6、打印文本第二行第一列 :sed -n "2, 1p" filename | awk 'print $1'
7、打印文件的最后一行 :awk 'END{print}' 或 tail -1 file
#awk -F: '/root/' /etc/passwd (搜索/etc/passwd有root关键字的所有行)
root:x:0:0:root:/root:/bin/bash
2.查找 grep
[root@localhost ~]grep "logread" /etc -nr
#在/etc文件下查找logread关键字,-n :顺便输出行号,-r 递归处理,将指定目录下的所有文件及子目录一并处理
grep: /etc/fstab: No such file or directory
/etc/init.d/fhlog:8:PROG=/sbin/logread
/etc/init.d/fhlog:10:PID_FILE="/var/run/logread.fhlog.pid"
grep: /etc/ppp/resolv.conf: No such file or directory
/etc/rc.d/K89fhlog:8:PROG=/sbin/logread
/etc/rc.d/K89fhlog:10:PID_FILE="/var/run/logread.fhlog.pid"
/etc/rc.d/S21fhlog:8:PROG=/sbin/logread
/etc/rc.d/S21fhlog:10:PID_FILE="/var/run/logread.fhlog.pid"
[root@localhost ~]tail -f 20190625_access.log | grep "error" --color #实时查找日志中error关键字并显示颜色