grep、sed、awk 三剑客实例

grep实例

  • 查询/var/log/secure 日志排查是否有非法登录的IP,并按数值倒序统计排序并显示前五个:

grep ‘Failed’ /var/log/secure|awk ‘{print $13}’| sort |uniq -c |sort -k1 -nr |head -5

grep ‘Failed’ /var/log/secure|awk ‘{print $(NF -3)}’   打印倒数第四列  NF是倒数第三列

 

  • 过滤文件夹里的目录:ls -p 只给目录添加标识符后面加/  ls -F给不同类型的文件加标识符

ll |grep “^d”     ls-l |sed -n ‘/^d/p’ ls –l |awk  ‘/^d/’

ls -p |grep “/$” 或者 ls -F |grep “/$”

find ./* -maxdepth 0 -type d

find ./ -maxdepth 1 -type d   会多一个当前目录

ll |awk ‘{if($2>1) print $0}’   如果没有硬链接的情况下可使用

 

sed实例

  • ifconfig eth0本机IP地址:

[root@tang ~]# ifconfig eth0

eth0      Link encap:Ethernet  HWaddr 00:0C:29:EC:67:23

inet addr:192.168.10.6  Bcast:192.168.10.255  Mask:255.255.255.0

ifconfig eth0|sed -n ‘2p’|sed ‘s/.*r://g’|sed ‘s/  B.*$//g’

ifconfig eth0|sed -n ‘2s/^.*dr://gp’|sed -n ‘s/  B.*$//gp’

ifconfig eth0|sed -nr ‘s/^.*r:(.*)  B.*$/\1/gp’

ifconfig eth0 |sed -nr ‘2s/^.*r://gp’|awk ‘{print $1}’

ifconfig eth0|sed -n ‘2p’|cat -A|cut -d ‘:’ -f 2|cut  -c 1-13

ifconfig eth0|sed -n ‘2p’|cut -c 21-33

ifconfig eth0|awk ‘NR==2’|cut -c 21-33

 

  • stat /etc/hosts      取出64

File: `/etc/hosts’

Size: 158               Blocks: 8          IO Block: 4096   regular file

Device: 803h/2051d        Inode: 131101      Links: 2

Access: (0644/-rw-r–r–)  Uid: (    0/    root)   Gid: (    0/    root)

stat /etc/hosts|sed -nr ‘s/^.*\(0(.*)4\/.*$/\1/gp’

stat /etc/hosts |sed -nr ‘ s#^.*\(0(.*)4/-.*$#\1#gp’

 

  • cat passwd :调换用户名和用户shell

cat passwd |sed -nr ‘s/([^:]+)(:.*:)([^:]+)/\3\2\1/gp’

 

  • 改变行尾的日期MM/DD/YYYY 格式为 YYYY-MM-DD

sed ‘s/\([0-9]\{2\}\)\/\([0-9]\{2\}\)\/\([0-9]\{4\}\)$/\3-\1-\2/’ time.txt

 

awk实例

  • 取出passwd文件里的用户和用户目录和用户所用的shell

awk -F “:” ‘NR==1 {print $NF,$6,$1}’ passwd   $NF表示最后一列  $0表示一行

 

  • -rw-r–r–  1 root root     1991 9  28 21:11 list.txt中的-rw-r–r–打印为644

ll list.txt |cut -c 2-10|tr ‘rwx-‘ ‘4210’|awk -F “” ‘{print $1+$2+$3″”$4+$5+$6″”$7+$8+$9}’

stat list.txt |sed -nr ‘ s#^.*\(0(.*)/-.*$#\1#gp’

stat list.txt |sed -nr ‘ s/^.*\(0(.*)\/-.*$/\1/gp’

stat list.txt |awk -F “[0/]” ‘NR==4 {print $2}’

stat -c %a list.txt

 

查看文件第20-30行内容的N种方法及命令介绍

seq 100 > /tmp/seq.txt

命令介绍:seq 用于产生从某个数到另外一个数之间的所有整数。

seq [选项]… 尾数 (从1到尾数 增量为1

seq [选项]… 首数 尾数 (从首数到尾数 增量为1

seq [选项]… 首数 增量 尾数

方法1: head tail通过管道组合)

# head -30 ett.txt | tail -11

命令解释:head -n 30 xxx.txt == head -30 xxx.txt 取文件前30行内容

tail -11 xxx.txt 取文件后11行内容

| 管道命令连接 将head取出的30行内容作为tail的输入

方法2: awk命令

# awk ‘NR==20,NR==30’ ett.txt

awk命令中 NR表示行号,直译 取20-30行的内容

awk ‘NR==35’ ett.txt 取第35行内容

方法3sed命令

# sed -n ‘20,30p’ ett.txt

sed命令 中-n 参数搭配p 一起来使用

1.打印文件的第二行

sed -n ‘2p’ file

2.打印13

sed -n ‘1,3p’ file

3.品配单词用/patten/模式,eg/Hello/

sed -n ‘/Hello/’p file

4.使用模式和行号进行品配,在第4行查询Hello

sed -n ‘4,/Hello/’ file

5.配原字符(显示原字符$之前,必须使用\屏蔽其特殊含义)

sed -n ‘/$/’p file

上述命令将把file中含有$的行打印出来

6.显示整个文件(只需将范围设置为1到最后于一行)

$代表最后一行

sed -n ‘1,$p’ file

7.任意字符 ,模式/.*/,/.*ing/匹配任意以ing结尾的单词

sed -n ‘/.*ing/’p file

8.打印首行

sed -n ‘1p’ file

9.打印尾行

sed -n ‘$p’ file

 

常用文件处理实例

  • 查询/var/log/secure 日志排查是否有非法登录的IP,并按数值倒序统计排序并显示前五个:

grep ‘Failed’ /var/log/secure|awk ‘{print $13}’| sort |uniq -c |sort -k1 -nr |head -5

grep ‘Failed’ /var/log/secure|awk ‘{print $(NF -3)}’   打印倒数第四列  NF是倒数第三列

 

  • 监控系统某一个端口的数据流,须打开防火墙端口。

tcpdump -i eth0 -nn port 514   监控eth0网卡的514端口

firewalld -cmd –permanent –add-port=514/udp

firewalld -cmd –reload    

 

  • 找出系统中默认带有SUID的程序

# find /usr/bin /usr/sbin -perm -4000 -ls         //包含set uid

 

  • linux删除录下的所有文件仅仅保留一个指定文件

方法一:find

[root@oldboy xx]# ls

file1  file10  file2  file3  file4  file5  file6  file7  file8  file9

[root@oldboy xx]# find /xx -type f ! -name “file10″|xargs rm -f

[root@oldboy xx]# ls

file10

 

[root@oldboy xx]# find /xx -type f ! -name “file10” -exec rm -f {} \;    

[root@oldboy xx]# ls

file10

方法二:rsync

[root@oldboy xx]# ls

file1  file10  file2  file3  file4  file5  file6  file7  file8  file9

[root@oldboy xx]# rsync -az –delete –exclude “file10” /null/ /xx/

[root@oldboy xx]# ls

file10

方法三:开启bash的extglob功能(此功能的作用就是用rm !(*jpg)这样的方式来删除不包括号内文件的文件)

[root@oldboy xx]# shopt -s extglob

[root@oldboy xx]# ls

file1  file10  file2  file3  file4  file5  file6  file7  file8  file9

[root@oldboy xx]# rm -f !(file10)

[root@oldboy xx]# ls

file10

方法四:

find ./ -type f|grep -v “\boldboy1\b”|xargs rm -f

方法五:

rm -f `ls|grep -v “\boldboy1\b”`

 

  • 过滤文件夹里的目录:ls -p 只给目录添加标识符后面加/  ls -F给不同类型的文件加标识符

ll |grep “^d”     ls-l |sed -n ‘/^d/p’ ls –l |awk  ‘/^d/’

ls -p |grep “/$” 或者 ls -F |grep “/$”

find ./* -maxdepth 0 -type d

find ./ -maxdepth 1 -type d   会多一个当前目录

ll |awk ‘{if($2>1) print $0}’   如果没有硬链接的情况下可使用

 

  • 取出passwd文件里的用户和用户目录和用户所用的shell:

awk -F “:” ‘NR==1 {print $NF,$6,$1}’ passwd   $NF表示最后一列  $0表示一行

 

  • 批量创建指定时间的脚本文件:

for n in `seq 14` ; do date -s “2018/06/$n” ;touch access_www_$(date +%F).log; done

 

  • 查看文件,不显示注释行与空白行:

cat /etc/inittab |awk ‘{if($0 !~ /^$/ && $0 !~ /^#/) print $0}’

 

  • 显示文件内容行号的N种方法:

cat -n 123.txt

nl 123.txt    不显示空行

grep -n “.*” 123.txt

awk ‘{print NR,$0}’ 123.txt

less -N 123.txt

sed ‘=’ 123.txt |sed ‘N;s#\n# #g’

vim 123.txt set nu