通配符和正则看似一样,实则是不同
通配符
‘’匹配任一个字符
正则:
‘.’匹配一个任意字符
‘’ 星号匹配任意个前面的字符
[] 匹配中括号里面任意一个
[^] 对中括号里面的取反
^标记开头
$标记结尾
例如:
vi a.txt
1 - adad
2- aaaa
3- 153dad
4-
5- ada5135
6- aaaaaaaaa
7- ssssss
8- aaa
9- asa161
10-asa
:wq
用grep -n a* a.txt 会匹配出全文
grep -n aa* a.txt 会匹配出第1,2,3,5,6,8,9,10,行
grep -n ^aa* a.txt 会匹配出第1,2,5,6,8,9,10行
grep -n ^$ a.txt 会匹配出空白行第4行
egrep -n a{6,} a.txt 会匹配出第6行
egrep -n a.a*[0-9]* a.txt 会匹配出第1,2,5,6,8,9,10行
egrep -n a.aa*[0-9][0-9]* a.txt 会匹配出第5,9行
cut 截取字符有用的
-d指定分隔符
-f指定列
如
cut -d : -f 1 /etc/passwd 显示所有用户名
cat /etc/passwd | grep root | cut -d : -f 6,7 显示root的家目录和登录shell
tr ‘a’ ‘A’ < a.txt 显示a.txt中的a为A
tr ‘a-z’ ‘A-Z’ < a.txt 小写变大写
tr ‘aa’ ‘12’ < a.txt 显示a.txt中的aa为12,只是一一对应的
-d 表示不显示指定字符
tr -d ‘aa’ < a.txt 输出时不显示aa
wc文本统计命令
-l 只显示行
-w 显示单词数
-c 显示字母数
wc -l a.txt
10 //10行
sort 文本排序命令
默认升序,以字符的ascll码大小排
-r 降序
-n 以数值大小排序
-t指定分隔符
-k 指定排序字段
sort -nr -t : -k 3 < /etc/passwd
以:为分隔符,第三列用户ID降序排序