Shell脚本学习笔记001
2017-01-02 15:31
246 查看
cut和sort命令 分段排序: sort -t ':' -k3 -n /etc/passwd |cut -d ':' -f 3 cut -d ':' -f 3 /etc/passwd|sort 按照ASIIC码排序 cut -d ':' -f 3 /etc/passwd|sort -n 按照数字排序 cut -d ':' -f 3 /etc/passwd|sort -nr 按照数字倒序排列 cut -d ':' -f 3 /etc/passwd|sort -nru 去重 cut -d ':' -f 3 /etc/passwd|sort -nr|uniq 去重 cut -d ':' -f 3 /etc/passwd|sort -nr|uniq -c 去重并计数 wc命令 wc /etc/passwd ---> 41 70 2296 /etc/passwd (4行 70个单词 2296个字符) wc -l /etc/passwd (-l line 查看多少行) 将wc得到的结果以" "分割,再赋给变量line line=`wc -l /etc/passwd|cut -d ' ' -f1`;echo $line if [ $line -lt 40 ];then echo '小于40';else echo '大于等于40'; fi uniq命令 uniq -c tee命令 cat 4.txt |tee 1.log ---> cat 4.txt > 1.log (多一个显示在页面的效果) tr命令 (替换) ls |tr 'a-z' 'A-Z' ls |tr 'A' '2' for命令 for i in `seq 1 100`; do cat /etc/passwd >> 1.txt ; done du命令 du -sh 1.txt du -sb 1.txt split命令(分割文件) split -l 100 1.txt 按照行数分割 split -l 100 1.txt my split -b 1M 1.txt split -b 100K 1.txt hh && || 命令连接符 a && b a命令执行成功后在执行b a || b a执行了(无论成功了没有)再执行b a;b 无论a执行了没有,b都可能会走(如if中的) 正则表达式 grep egrep awk sed grep命令(常用于匹配) grep 'wangtao' mypwd.txt grep -c ':x:' mypwd.txt grep -n --color 'root' mypwd.txt grep -v 'root' mypwd.txt 取反 grep -A2 -n 'root' mypwd.txt (After 2行) grep -B3 -n 'wangtao' mypwd.txt (Bafore 3行) grep -C1 -n 'wangtao' mypwd.txt (上下各1行) grep -r 'wangtao' /etc (遍历某目录下所有) grep '^#' mypwd.txt grep '[0,1]' mypwd.txt grep '[0-9]' mypwd.txt grep '[^a-z]' mypwd.txt (中括号里面的尖号,表示非) grep 'ro*t' mypwd.txt (r和t之间有0个或多个o) grep 'ro.t' mypwd.txt (r和t之间有1个任意字符) grep -E 'ro?t' mypwd.txt (r和t之间有0个或1个o) egrep 'ro?t' mypwd.txt (r和t之间有0个或1个o) grep -E 'ro+t' mypwd.txt (r和t之间有1个或多个o) egrep 'ro+t' mypwd.txt (r和t之间有1个或多个o) grep 'r.*t' mypwd.txt (r和t之间有0个或多个任意字符) alias grep='grep --color' sed命令(常用于替换) -r 就不用转义了 grep -n '.*' mypwd.txt |sed '10,$p' -n grep -n '.*' mypwd.txt |sed -r '/(oo)+/p' -n grep -n '.*' mypwd.txt |sed -e '/root/p' -e '/mysql/p' -n grep -n '.*' mypwd.txt |sed '/root/p ; /mysql/p' -n grep -n '.*' mypwd.txt |sed '10,$d' (不显示第10行到最末一行) grep -n '.*' mypwd.txt |sed 's/[0-9]//g' 删除所有数字 grep -n '.*' mypwd.txt |sed 's/[0-9]/ /g' 将所有数字替换成空格 换位置(将一行分成三段,第三段和第一段交换位置) grep '.*' mypwd.txt |sed -r 's/([^:]+)(:.*:)([^:]+$)/\3\2\1/' 示例: 替换前 root:x:0:0:root:/root:/bin/bash 替换后 /bin/bash:x:0:0:root:/root:root (将root和/bin/bash交换位置) awk命令 awk -F ':' '{print $1}' mypwd.txt 全部行的第一列 awk -F ':' '$1~/root/' mypwd.txt 以:分割,第一段为root,默认整行输出 awk -F ':' '$1~/root/ {print $1}' mypwd.txt 以:分割,第一段为root,输出第一段 awk -F ':' '$1~/root/ {OFS="#"; print $1,$2}' mypwd.txt 将中间的分隔符以#号代替 awk -F ':' '$1~/root/ {OFS="#"; print $1,$2,$3}' mypwd.txt awk -F ':' '$1=="root" {print $1,$2}' mypwd.txt 等价于awk -F ':' '$1~/root/ {print $1,$2}' mypwd.txt awk -F ':' 'NF>4 && NF<8' mypwd.txt 分割后段数小于8大于4的所有行 awk -F ':' 'NF>4 && NR<8' mypwd.txt 分割后段数大于4的行号小于8的
相关文章推荐
- shell脚本学习笔记(一)闹钟的源码
- Shell脚本学习笔记-系统管理
- Shell脚本学习笔记(五)--函数
- Shell脚本学习笔记(八)--进程管理和工作控制
- Shell脚本学习笔记(六)--转向与陷阱
- Shell脚本学习笔记(七)--sed和awk
- shell脚本学习笔记(三)mysql与ARP表的绑定源码
- Shell脚本学习笔记(二)--变量
- Shell脚本学习笔记(五)--函数
- Shell脚本学习笔记(四)--流程控制
- shell 脚本学习指南-笔记(二)
- Shell 编程学习笔记:脚本输入参数
- shell shell学习笔记001--什么是shell?
- 学习shell 与shell脚本 学习笔记
- Shell脚本学习笔记-语法
- shell脚本学习笔记
- Shell脚本学习笔记(一)--预备知识
- Shell脚本学习笔记(七)--sed和awk
- Shell脚本学习笔记(六)--转向与陷阱
- Shell脚本学习笔记(四)--流程控制