linux之 grep cut sed tr等 简介和使用

来源:互联网 发布:linux读法 编辑:程序博客网 时间:2024/05/18 06:28

grep命令  egrep  和 fgrep

 

先来个例子grep [option] ‘pattern’ file     # grep --color=auto 'mode' /proc/cpuinfo

pattern 元字符 所表达的是不同字面本身的含义

grep 里的元字符

       *     次数匹配元字符

      /b 单词牟定符  /<   />

基本正则表达式 扩展正则                                                                                                 

 grep 仅支持基本

grep 选项

       -i 忽略大小写

       -n 显示匹配行在文中所处位置

       -A -B -C 后加数字 以上下文的方式显示匹配到得串

       A2后两行 B C上下都有

 /(/) 标记匹配并引用

# export GREP_CLOLR='01;34;43' 改颜色 字体 神马的

 egrep = grep -E

egrep p|Pig|[Dd]dog)’ filename

 cut [option]

       -d  分隔符 默认为tab

       -f  第几个字段 -f1-3

       #cut -d : -f1,3 filename

# ifconfig |grep 'inet addr'| grep -v '127.0.0.1'|cut -d: -f2 |cut -d' ' -f1

192.168.0.173

 

# grep -n --color=auto '[[:alnum:]]*:[[:alnum:]]*:/<1[0-9]/{3/}' /etc/passwd | cut -d: -f2,4

 

wc

       -l

       -w

       -c 字节

       -m 字符

 

sort 排序 字符

       sort -n 数值比较

              -r 逆序

              -f 忽略字符大小写

              -u 忽略重复行

              -t 指定分隔符

              -k 指定字段

uniq 消除重复

 uniq -u 显示从没显示的,-d 只显示重复的 -c 重复几次列出来

#sort s.txt | uniq -c

 

# cut -d: -f7 /etc/passwd | sort | grep -v "^$"|uniq -c

diff

 path  见图

 

 

aspell

 拼写检查  #aspell check cpu

              #aspell list < cpu

 

 

tr 小写转大写

       tr -d 删除字符      #echo redhat|  rt -d ea

 

sed

 

sed 1,13s///g