一、grep
作用:过滤包含关键字的行。
[root@localhost ~]# grep -n root /etc/passwd #匹配出来的结果显示行号
[root@localhost ~]# grep -ni root /etc/passwd #忽略大小写
[root@localhost ~]# grep '^root' /etc/passwd #过滤以root开头
[root@localhost ~]# grep 'bash$' /etc/passwd #过滤以bash结尾
[root@localhost ~]# grep -nA 3 '^ftp' /etc/passwd #过滤以ftp开头行的之后3行
[root@localhost ~]# grep -nB 3 '^ftp' /etc/passwd #过滤以ftp开头行的之前3行
[root@localhost ~]# grep -nC 3 '^ftp' /etc/passwd #过滤以ftp开头行的前后3行
[root@localhost ~]# grep -vn root /etc/passwd #使用-v参数取反
二、cut
作用:列截取工具,用于列的截取。
[root@localhost ~]# cut -d: -f1,7 /etc/passwd #指定冒号为分割符,截取第1列和第7列
[root@localhost ~]# cut -c1-5 /etc/passwd #截取第1到第5个字符
三、sort
作用:将文件的每一行作为一个单位,从首字符向后,依次按ASCII码值进行比较,最后将他们按升序输出。
[root@localhost ~]# sort -n -t: -k 3 /etc/passwd #-n指定用数字排序,-t指定冒号为分隔符,-k选择第3列开始排序
[root@localhost ~]# sort -nr -t: -k 3 /etc/passwd #-r参数降序排列
[root@localhost ~]# sort -u 3 1.txt #-u参数去重
四、uniq
作用:去除连续的重复的行。
[root@localhost ~]# uniq -cd 1.txt #-c统计重复行的次数,-d只显示重复行
[root@localhost ~]# uniq -i 1.txt #忽略大小写
五、tee
[root@localhost ~]# echo 888|tee 1.txt
[root@localhost ~]# echo 999|tee -a 1.txt #-a参数追加到目标文件
六、diff
作用:用于逐行比较文件的不同。
七、paste
作用:用于合并文件行。
八、tr
作用:用于字符转换,替换和删除;主要用于删除文件中控制字符或进行字符转换。
[root@localhost ~]# tr 'a-z' 'A-Z' < 1.txt #将1.txt里面的小写字母替换成大写字母
[root@localhost ~]# tr '0-9' '@' < 1.txt #将1.txt里面的数字替换成@
[root@localhost ~]# tr -d 'A-Z' < 1.txt #将1.txt里面的大写字母删除
[root@localhost ~]# tr -s 'A-Z' < 1.txt #将1.txt里面的连续大写字符压缩为一个字符
九、shell常用通配符
*:匹配0或多个任意字符。
?:匹配任意单个字符。
[list]:匹配[list]中的任意单个字符。
[!list]:匹配除[list]中的任意单个字符。
{string1,string2,...}:匹配string1,string2或更多字符串。
十、bash中的引号
双引号"":会把引号的内容当成整体来看待,允许通过$符号引用其他变量值。
单引号’':会把引号的内容当成整体来看待,禁止引用其他变量值,shell中特殊符号都被视为普通字符。
反撇号``:反撇号和$()一样,引号和括号里的命令会优先执行,如果存在嵌套,反撇号不能用。