企业网站 html模板,如何做单页网站,平台网站建设设计,网站整合推广目录
一#xff0c;正则表达式
1#xff0c;基础正则
2#xff0c;扩展正则
3#xff0c;常见正则表达式
二#xff0c;文件操作四剑客
1#xff0c;grep进阶
2.egrep
3#xff0c;find进阶
4#xff0c;sed
5#xff0c;awk 一#xff0c;正则表达式
1正则表达式
1基础正则
2扩展正则
3常见正则表达式
二文件操作四剑客
1grep进阶
2.egrep
3find进阶
4sed
5awk 一正则表达式
1基础正则 a查找特定字符 cat test.txt | grep -n was b利用[]查找集合字符 cat test.txt | grep -n sh[io]rt 匹配i或者o cat test.txt | grep -n [^w] 排除w cat test.txt | grep -n [a-h]oo cat test.txt | grep -n [0-9] c查找行首^与行尾$ cat test.txt | grep -n ^[A-Z] cat test.txt | grep \.$ \ 为转义符 d查找任意一个字符.与重复字符* cat test.txt | grep -n w..d cat test.txt | grep -n ooo* e查找连续的字符范围{},需要使用转义符\{\} cat test.txt | grep -n o\{2\} cat test.txt | grep -n wo\{2,5\}d cat test.txt | grep -n wo\{2,\}d
2扩展正则 a重复一个或一个以上的前一个字符 cat test.txt | grep -nE wod 或者 cat test.txt | egrep -n wod b?零个或者一个前一个字符 cat test.txt | egrep -n bes?t c|使用或者的方式找出多个字符 cat test.txt | egrep -n of|is|on d()查找组字符串 cat test.txt | egrep -n t(a|e)st e()辨别多个重复的组 cat test.txt | egrep -n A(xyz)C
3常见正则表达式
3.1数字 “^[0-9]*[1-9][0-9]*$” //正整数 “^((-\d)|(0))$” //非正整数负整数 0 “^-[0-9]*[1-9][0-9]*$” //负整数 “^-?\d$” //整数 “^\d(\.\d)?$” //非负浮点数正浮点数 0 “^(([0-9]\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9])|([0-9]*[1-9][0-9]*))$” //正浮点数 “^((-\d(\.\d)?)|(0(\.0)?))$” //非正浮点数负浮点数 0 “^(-?\d)(\.\d)?$” //浮点数
3.2字符串 “^[A-Z]$” //由26个英文字母的大写组成的字符串 “^[a-z]$” //由26个英文字母的小写组成的字符串 “^[A-Za-z0-9]$” //由数字和26个英文字母组成的字符串 “^\w$” //由数字、26个英文字母或者下划线组成的字符串
3.3Email “^[\w-](\.[\w-])*[\w-](\.[\w-])$” //email地址 “^([w-.])(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.)|(([w-].)))([a-zA-Z]{2,4}|[0-9]{1,3})(]?)$” //Email
3.4Url “^[a-zA-z]://(\w(-\w)*)(\.(\w(-\w)*))*(\?\S*)?$” //url
3.5IP “^(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5])$” //IP地址
3.6Tel /^((\?[0-9]{2,4}\-[0-9]{3,4}\-)|([0-9]{3,4}\-))?([0-9]{7,8})(\-[0-9])?$/ //电话号码
3.7日期校验 /^(d{2}|d{4})-((0([1-9]{1}))|(1[1|2]))-(([0-2]([1-9]{1}))|(3[0|1]))$/ // 年-月-日 yyyy-MM-dd / yy-MM-dd 格式 ^[0-9]{4}-((0([1-9]{1}))|(1[1|2]))-(([0-2]([0-9]{1}))|(3[0|1]))$ // 年-月- 日 yyyy-MM-dd 格式 /^((0([1-9]{1}))|(1[1|2]))/(([0-2]([1-9]{1}))|(3[0|1]))/(d{2}|d{4})$/ // 月/日/年
二文件操作四剑客
1grep进阶 选项 -r 递归扫描指定目录下的每一个文件 -l 只显示匹配到指定关键字的文件名而不是文件内容 案例 查看/etc目录下所有包含bash的文件名 grep -rl bash /etc
2.egrep 完美支持正则表达式
3find进阶 3.1按照权限查找 -perm 3.2按照时间戳查找 -atime -mtime -ctime 3.4 -exec find /var/spool/mail -type f -exec rm -rf {} \; 3.5 xargs find /var/spool/mail -type f | xargs rm -rf
4sed
4.1语法 sed [选项] 操作 参数 sed [选项] -f scriptfile 参数
4.2选项 -e表示用指定命令或脚本处理 -f指定脚本文件 -h帮助 -n表示仅显示处理后的结果 -i直接编辑文本文件 -r支持扩展正则 4.3操作
a增加在当前行下面以行增加指定内容 c替换将选定行替换 d删除删除指定行 i插入在选定行的上面插入一行 p打印 s替换替换指定字符 y字符转换
5awk
5.1语法 awk 选项 模式或条件{编辑命令} 文件1 文件2 ... awk -f 脚本文件 文件1 文件2 ...
5.2选项
-F 指定每行的分隔符 默认分隔符为空格
5.3内建变量
FS指定每行的分隔符 NF指定当前处理行的字段个数 NR当前处理行的行号 $0当前处理行的整行内容 $n当前处理的第n个字段 FILENAME处理文件名 RS数据记录分隔默认是\n
5.4案例
a按行输出 awk {print} test.txt #等同cat awk NR1NR3{print} test.txt awk NR1,NR3{print} test.txt #打印1到3行 awk NR%20{print} test.txt #打印偶数行 b按段输出 默认以空格分段 ifconfig ens33 |awk /netmask/{print $2} #筛选IP地址 cat /etc/shadow | awk -F : $2!!{print $1} #打印不能登录系统的用户 c调用shell命令 cat /etc/passwd | awk -F : /bash$/{print | wc -l} /etc/passwd #统计能够登录系统的用户个数