第三周、文本处理工具、shell脚本编程
2019-08-04 23:27
1251 查看
文本处理工具
1、cut
- -d指定分隔符。比如-d: -d' '
- -f指定取第几列。比如-f1,3
- --output-delimiter指定显示的分隔符
tr -s压缩 -d删除 -c除了
2、使用tr和cut取磁盘的百分比
[root@glowing-bliss-1 data]# df -h | tr -s ' ' | cut -d' ' -f5 | tr -dc '[0-9]\n' 0 0 5 0 32 67 32 0
3、取IP
[root@glowing-bliss-1 data]# ifconfig lo | head -2 | tail -1 | tr -s ' ' | cut -d' ' -f3 127.0.0.1 [root@glowing-bliss-1 data]# ifconfig lo | head -2 | tail -1 | tr -s ' ' : | cut -d: -f3 127.0.0.1
4、paste合并文件
合并两个文件.-d指定分割符
下面为合并a.txt和1.txt,分隔符为:
qqq@qqq:~$ cat a.txt a b c d qqq@qqq:~$ cat 1.txt 1 2 3 4 qqq@qqq:~$ paste 1.txt a.txt -d: 1:a 2:b 3:c 4:d
5、wc命令
统计行,单词,字节数
-m字符数 -L文件最宽的一行
[root@k8s-master ~]# wc /etc/passwd 20 28 874 /etc/passwd [root@k8s-master ~]# wc -l /etc/passwd 20 /etc/passwd [root@k8s-master ~]# wc -c /etc/passwd 874 /etc/passwd [root@k8s-master ~]# wc -w /etc/passwd 28 /etc/passwd [root@k8s-master ~]# wc -lwc /etc/passwd 20 28 874 /etc/passwd [root@k8s-master ~]# wc -clw /etc/passwd 20 28 874 /etc/passwd
6、wc显示当前目录下文件名最长的文件的长度
[root@k8s-master ~]# ls -1 /etc/ | wc -L 24
7、sort命令
默认是字母排序
-u删除输出中的重复行;
-t c使用c作为字段界定符(就是分割符,和cut的-d一样);
-k x 使用第x列作为基准进行排序
7.1 基于UID对passwd文件进行排序
[root@k8s-master ~]# 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磁盘利用率排序
[root@glowing-bliss-1 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
从输出中删除前后相接的重复行
-d仅显示重复果的行
-u仅显示不曾重复的行
8.1【面试题】统计两个不同目录中相同的文件列表
qqq@qqq:~$ 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 qqq@qqq:~$ (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【面试题】统计 两个不同目录中不同的文件列表
qqq@qqq:~$ (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文件内容
[root@glowing-bliss-1 data]# lastb -f /var/log/btmp
10、【面试题】写一个脚本进行nginx日志统计,统计访问IP最多的前十
[root@glowing-bliss-1 data]# awk '{print $1}' /var/log/nginx/access.log | sort -nr | uniq -c | sort -nr | head
11、diff比较两个文件的不同
-u显示 统一的diff格式文件
qqq@qqq:~$ 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
复制在其他文件中的改变了的文件(慎用)
-b选项来自动备份改变了的文件
正则表达式(标准和拓展)
一、基本语法
标准正则表达式中需要转义的字符有:(,),{,},|,+,.,
拓展正则表达式下词首,词尾,分组的引用这些的反斜杠不能省
- *表示匹配任意次,包括0次
- .*表示任意长度的任意字符
- ?表示匹配前面字符0次或一次
- +表示匹配前面字符一次或多次
- {n,m}表示匹配前面字符n到m次
- ^表示行首
- $表示行尾
- ^$表示空行^[[:space:]]*$表示空白行
- \<词首 \>行尾
- \<hello\> hello单词(单词的组成:字母数字下划线,其他字符均为分隔符)
- (.*)括号为分组,\1 \2为使用前面括号的内容
2、显示磁盘分区使用率
[root@glowing-bliss-1 data]# df -h | grep '^/dev/sd' | tr -s ' ' | cut -d' ' -f5 | cut -d% -f1 | sort -n 32 67
3、将root替换成rooter
:%s@\(root\)@\1er@g
4、正则表达式获取IP
qqq@qqq:~$ 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后跟任意空白字符的行
qqq@qqq:~$ 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建立的连接数
qqq@qqq:~$ 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的版本号
[root@glowing-bliss-1 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扫描局域网的机器
查看当前局域网哪些IP被使用了
[root@centos7 ~]# 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、基本正则表达式元字符
字符匹配
- . 匹配任意单个字符
- [] 匹配指定范围内的任意单个字符。[.?]表示匹配.或者\或者问号
- [^] 匹配范围之外的任意单个字符
- [:digit:]等等一系列
12、cat -A
可以查看某些看不到的东西:Tab、回车换行等
[root@centos7 ~]# cat -A 1.txt a b^Ic$ dd$ $ d$ $ $
13、程序执行方式
高级语言->编译器->机器代码->执行
14、.vimrc vim配置文件
root@qqq:~# vim ~/.vimrc root@qqq:~# 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路径,直接执行脚本
执行脚本的方式
- 1、bash date.sh
- 2、相对路径法 ./date.sh
- 3、绝对路径法
- 4、PATH变量法,scripts的目录加入PATH变量(写入bashrc或profile,然后source)
- 5、cat date.sh|bash
16、脚本的两种错误
shell与python都属于解释型语法,边解释边执行;C语言,JAVA等是编译型语言,全部代码编译后才能执行
- 1、语法错误,最好使用bash -n date.sh检查以减少语法错误,语法错误后的命令都不会执行,但前面的命令都会执行
- 2、其他错误:比如命令不存在,命令错误等。不影响后续代码的执行。在脚本开始加上set -e可以让脚本出现错误时立即停止执行。
- bash -n检查语法错误,写完脚本务必检查。
- bash -x用于调试脚本,查看脚本的执行过程,执行逻辑。
17、shell计算求和等的几种方法:
17.1、let命令,比较常用
[root@centos7 ~]# x=10 [root@centos7 ~]# y=17 [root@centos7 ~]# let z=$x+$y [root@centos7 ~]# echo $z 27
17.2、$(())比较常用
[root@centos7 ~]# x=100 [root@centos7 ~]# y=302 [root@centos7 ~]# z=$(($x+$y)) [root@centos7 ~]# echo $z 402
17.3、bc求和,较为常用
[root@centos7 ~]# x=123 [root@centos7 ~]# y=986 [root@centos7 ~]# echo $x+$y | bc 1109
18、shell脚本的颜色
echo必须要-e参数才可以使用颜色
[root@glowing-bliss-1 ~]# 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/partitions
20、单引号,双引号,反引号
- 单引号:强引用,不识别命令,不识别变量
- 双引号:不识别命令,但是别变量;
- 反向单引号,都识别
[root@glowing-bliss-1 ~]# echo '$HOSTNAME' $HOSTNAME [root@glowing-bliss-1 ~]# echo "$HOSTNAME" glowing-bliss-1.localdomain [root@glowing-bliss-1 ~]# ls `pwd` virt-sysprep-firstboot.log [root@glowing-bliss-1 ~]# echo `hostname` -bash: echglowing-bliss-1.localdomain: command not found
21、程序有父进程和子进程
21.1、查看进程之间的父子关系
pstree
21.2、查看当前进程ID
echo $BASHPID
echo $$echo $$经常在脚本中使用,查看脚本运行时的进程ID
21.3、查看父进程ID
echo $PPID
22、set
unset删除变量
set查看
set -C执行后无法覆盖已经存在的文件
shell脚本编程基础
[.?]表示. ? 中的某一个
grep -E "(bash|nologin)$" /etc/passwd grep -E "bash$|nologin$" /etc/passwd
1、环境变量的声明
- export EDITOR=vim 定义默认编辑器,要写入profile
- declare -x EDITOR=vim 与上面等价
- declare -r声明为只读变量 等于 readonly name
2、set --
清空所有位置变量
3、变量引用要习惯加上花括号
- ${10} ${DATE} ${PWD}
4、位置变量
相关文章推荐
- Shell脚本编程总结及速查手册
- shell脚本编程-循环(for、while、until)
- Linux命令行和shell脚本编程-函数
- Linux命令与shell脚本编程学习笔记-16创建函数
- 【Shell 编程基础第一部分】第一个Shell脚本HelloShell及一些简单的Shell基础书写与概念;
- Unix整理笔记——基本shell脚本编程——里程碑M14
- Shell 编程--本文结合大量实例阐述如何编写一个shell脚本
- shell编程之 编写renfilex.sh脚本
- shell脚本编程实例(一)
- Linux下的shell脚本编程(一)
- 【Shell 编程基础第二部分】Shell里的流程控制、Shell里的函数及脚本调试方法!
- shell脚本编程之循环语句学习笔记
- shell脚本编程(二)------面试题
- shell脚本编程基础知识
- shell 脚本编程之逻辑运算符
- centos shell脚本编程2 if 判断 case判断 shell脚本中的循环 for while shell中的函数 break continue 第三十六节课
- Shell脚本编程入门(一)
- shell脚本编程(三)
- shell脚本编程:条件判断if语句使用小结
- Linux系统shell脚本编程——生产实战案例