第三周、文本处理工具、shell脚本编程
文本处理工具
1、cut
tr -s压缩 -d删除 -c除了 2、使用tr和cut取磁盘的百分比[[email?protected] data]# df -h | tr -s ‘ ‘ | cut -d‘ ‘ -f5 | tr -dc ‘[0-9]n‘ 0 0 5 0 32 67 32 0 3、取IP[[email?protected] data]# ifconfig lo | head -2 | tail -1 | tr -s ‘ ‘ | cut -d‘ ‘ -f3 127.0.0.1 [[email?protected] data]# ifconfig lo | head -2 | tail -1 | tr -s ‘ ‘ : | cut -d: -f3 127.0.0.1 4、paste合并文件
[email?protected]:~$ cat a.txt a b c d [email?protected]:~$ cat 1.txt 1 2 3 4 [email?protected]:~$ paste 1.txt a.txt -d: 1:a 2:b 3:c 4:d 5、wc命令
[[email?protected] ~]# wc /etc/passwd 20 28 874 /etc/passwd [[email?protected] ~]# wc -l /etc/passwd 20 /etc/passwd [[email?protected] ~]# wc -c /etc/passwd 874 /etc/passwd [[email?protected] ~]# wc -w /etc/passwd 28 /etc/passwd [[email?protected] ~]# wc -lwc /etc/passwd 20 28 874 /etc/passwd [[email?protected] ~]# wc -clw /etc/passwd 20 28 874 /etc/passwd 6、wc显示当前目录下文件名最长的文件的长度[[email?protected] ~]# ls -1 /etc/ | wc -L 24 7、sort命令
7.1 基于UID对passwd文件进行排序[[email?protected] ~]# cat /etc/passwd | sort -t: -k3 -nr qqq:x:1000:1000:qqq:/home/qqq:/bin/bash polkitd:x:999:998:User for polkitd:/:/sbin/nologin systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin nobody:x:99:99:Nobody:/:/sbin/nologin ... 7.2 df磁盘利用率排序[[email?protected] data]# df -h | tr -s ‘ ‘ | cut -d‘ ‘ -f5 | tr -dc ‘[0-9]n‘ | sort -n 0 0 0 0 5 32 32 67 8、uniq
8.1【面试题】统计两个不同目录中相同的文件列表[email?protected]:~$ ls . /tmp -1 | sort | uniq -d 10.txt 1.txt 2.txt 3.txt 4.txt 5.txt 6.txt 7.txt 8.txt 9.txt [email?protected]:~$ (ls /tmp -1;ls . -1) | sort | uniq -d #这样显示不显示目录 10.txt 1.txt 2.txt 3.txt 4.txt 5.txt 6.txt 7.txt 8.txt 9.txt 8.2【面试题】统计 两个不同目录中不同的文件列表[email?protected]:~$ (ls /tmp -1;ls . -1) | sort | uniq -u #这样显示不显示目录 {1..txt} a.txt gems myblogs systemd-private-b8854b73c0e2479db1d56e43d8995bec-systemd-resolved.service-AcZjbU systemd-private-b8854b73c0e2479db1d56e43d8995bec-systemd-timesyncd.service-sngpw9 vmware-root_643-3979708515 9、lastb显示btmb文件内容[[email?protected] data]# lastb -f /var/log/btmp 10、【面试题】写一个脚本进行nginx日志统计,统计访问IP最多的前十[[email?protected] data]# awk ‘{print $1}‘ /var/log/nginx/access.log | sort -nr | uniq -c | sort -nr | head 11、diff比较两个文件的不同
[email?protected]:~$ diff 1.txt 2.txt -u --- 1.txt 2019-08-04 10:07:47.384154832 +0000 +++ 2.txt 2019-08-04 10:08:01.620224554 +0000 @@ -1,3 +1,4 @@ 1 2 3 +4 13、patch
正则表达式(标准和拓展)一、基本语法标准正则表达式中需要转义的字符有:(,),{,},|,+,., 拓展正则表达式下词首,词尾,分组的引用这些的反斜杠不能省
2、显示磁盘分区使用率[[email?protected] data]# df -h | grep ‘^/dev/sd‘ | tr -s ‘ ‘ | cut -d‘ ‘ -f5 | cut -d% -f1 | sort -n 32 67 3、将root替换成rooter:%[email?protected](root)@[email?protected] 4、正则表达式获取IP[email?protected]:~$ ifconfig ens33 | grep -Eo ‘([0-9]{,3}.){3}([0-9]{,3})‘ 192.168.38.148 255.255.255.0 192.168.38.255 5、找出ntestat -atn输出中LISTEN后跟任意空白字符的行[email?protected]:~$ netstat -atn | grep -E ‘LISTEN[[:space:]]*$‘ tcp 0 0 127.0.0.53:53 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN tcp6 0 0 :::22 :::* LISTEN 6、查看每个IP建立的连接数[email?protected]:~$ cat ss.log | grep ESTAB | tr -s ‘ ‘ : | cut -d: -f6 | sort | uniq -c | sort -nr | head -3 44 127.0.0.1 10 113.234.28.244 8 124.64.18.135 7、取CentOS7的版本号[[email?protected] data]# cat /etc/centos-release | grep -Eo ‘[0-9]+‘ | head -1 7 8、grep-A # 后多少行 -B # 前多少行 - # 前后各多少行 -i 忽略大小写 -e 相当于逻辑中的or,可以多次使用 -w 匹配整个单词 9、misc目录光盘会自动挂载到/misc/cd目录 autofs的作用,没有的话装一个并启动 10、nmap扫描局域网的机器
[[email?protected] ~]# nmap -v -sn 192.168.10.1/24 | grep -B1 "Host is up" | grep report | cut -d‘ ‘ -f5 192.168.10.1 192.168.10.6 192.168.10.12 192.168.10.14 192.168.10.16 192.168.10.19 192.168.10.20 192.168.10.28 ... 11、基本正则表达式元字符字符匹配
12、cat -A可以查看某些看不到的东西:Tab、回车换行等 [[email?protected] ~]# cat -A 1.txt a b^Ic$ dd$ $ d$ $ $ 13、程序执行方式高级语言->编译器->机器代码->执行 14、.vimrc vim配置文件[email?protected]:~# vim ~/.vimrc [email?protected]:~# cat ~/.vimrc set ignorecase set cursorline set autoindent set ai autocmd BufNewFile *.yaml exec ":call SetTitle()" func SetTitle() if expand("%:e") == ‘yaml‘ call setline(1,"#**************************************************************") call setline(2,"#Author: uscwifi") call setline(3,"#QQ: 2*******1") call setline(4,"#Date: ".strftime("%Y-%m-%d")) call setline(5,"#FileName: ".expand("%")) call setline(6,"#URL: http://www.baidu.com") call setline(7,"#Description: The test script") call setline(8,"#Copyright (C): ".strftime("%Y")." Copyright ? 站点名称 版权所有") call setline(9,"#************************************************************") call setline(10,"") endif endfunc autocmd BufNewFile * normal G 15、将脚本的目录路径放入PATH路径,直接执行脚本执行脚本的方式
16、脚本的两种错误shell与python都属于解释型语法,边解释边执行;C语言,JAVA等是编译型语言,全部代码编译后才能执行
17、shell计算求和等的几种方法:17.1、let命令,比较常用 [[email?protected] ~]# x=10 [[email?protected] ~]# y=17 [[email?protected] ~]# let z=$x+$y [[email?protected] ~]# echo $z 27 17.2、$(())比较常用 [[email?protected] ~]# x=100 [[email?protected] ~]# y=302 [[email?protected] ~]# z=$(($x+$y)) [[email?protected] ~]# echo $z 402 17.3、bc求和,较为常用 [[email?protected] ~]# x=123 [[email?protected] ~]# y=986 [[email?protected] ~]# echo $x+$y | bc 1109 18、shell脚本的颜色echo必须要-e参数才可以使用颜色 [[email?protected] ~]# cat /data/docker_stats.sh #!/bin/bash RED="e[31;1m" GREEN="e[32;1m" YELLOW="e[33;1m" END_COLOR="e[0m" # 用法 echo -e "n${YELLOW}################################${END_COLOR}n" 19、cat /proc/partitions20、单引号,双引号,反引号
[[email?protected] ~]# echo ‘$HOSTNAME‘ $HOSTNAME [[email?protected] ~]# echo "$HOSTNAME" glowing-bliss-1.localdomain [[email?protected] ~]# ls `pwd` virt-sysprep-firstboot.log [[email?protected] ~]# echo `hostname` -bash: echglowing-bliss-1.localdomain: command not found 21、程序有父进程和子进程21.1、查看进程之间的父子关系
21.2、查看当前进程ID
21.3、查看父进程ID
22、setunset删除变量 set查看 set -C执行后无法覆盖已经存在的文件 shell脚本编程基础
1、环境变量的声明
2、set --清空所有位置变量 3、变量引用要习惯加上花括号
4、位置变量(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |