hsy75的个人空间 https://blog.eetop.cn/vivilife [收藏] [复制] [分享] [RSS]

空间首页 动态 记录 日志 相册 主题 分享 留言板 个人资料

日志

[o][LINUX utility 001] 常用的shell命令工具

已有 1486 次阅读| 2012-10-24 10:34 |个人分类:linux

01 tee:

tee 的使用经常用来解决你想在屏幕上看到输出,同时却把输出存储下来的情况

没有用tee之前,你可能需要写下面这些

# echo something > something.log 2>&1 然后就结束了操作,如果你需要redirected 的话那么最好还是用tee

#echo something | tee something.log | less

ref:http://en.wikipedia.org/wiki/Tee_(command)


http://www.cnblogs.com/orez88/articles/1889781.html

grep

    -c 只打印匹配模式的行编号记数

    -I 在匹配文本时忽略大小写

    -n 在每行前显示其行编号

    -v 逆向输出,打印不匹配的行

    -f file 要匹配的字符串列表在filezhong

grep ‘[Tt]his’ file1.txt 设置大小写

grep ‘^[^#]‘ file2.txt 不匹配行首


grep “s…n” file3.txt 匹配任意字符


sed 行编辑命令
    s 替代

    i 插入

    a 附加

    d    删除全部匹配的行

    D    删除首次匹配的行

sed可以做什么(”行”为基础)

    删除

    改变

    添加

    插入

    替换

示例

sed –n ’1,4p’ /etc/passwd p是打印 -n 不显示原文件 打印1-4行

sed ‘/80/D’ file.txt 首次出现80的行进行删除

sed ‘s/var/usr/g’ file.txt 将所有的var替换成usr g代表所有

sed ’50,$s/help/man/g’ file.txt 替换从50行到最后一行的help替换为man 前49不管

awk 可以处理列,也可以处理行,可以定位到第几行,第几列

awk [选项] ‘awk脚本’ 输入文件

    -F fs 使用fs作为输入记录的字段分隔符 = sort –t

    -f filename 从文件filename读取awk_script(awk脚本)

    -v var=value 为awk_script. 设置变量

awk的内置变量

变量功能默认
FS =大写F输入字段分隔符空格或tab
RS输入记录分隔符换行
OFS =out FS输出字段分隔符空格或tab
ORS输出记录分隔符换行
NF 常用当前记录非空字段的编号 
NR 常用从所有文件读入的记录号 

 

示例

awk –F : ‘{print NR,$1,$2}’ /etc/passwd


awk –F : ‘{print NR,$1,$NF}’ /etc/passwd $NF 提取最后一行

awk –F : ‘NR%10==5{PRINT nr,$0}’ /etc/passwd NR= 当前处理的行数 除10求余=5打印当前的行数,进行输出 $0 整行输出,比如5、15、25 打印出来

awk –F : ‘NR==8,NR==13 {print NR,$0}’ /etc/passwd 指定8行和13行。

awk工作原理(工具)


$NF $NR $1 第一列 $2 第二列 $0 一整行

实际测试

awk –F: ‘{print $1,$3}’/etc/passwd


awk –F: ‘{print $1,$NF}’/etc/passwd


awk –F: ‘{print NR,$1,$NF}’/etc/passwd NR行号


awk –F: ‘NR==5,NR==8{print NR,$1,$NF}’/etc/passwd 指定5-8行


awk –F: ‘NR%10==5{print NR,$1,$NF}’/etc/passwd 除10求余


课程示例测试

1、grep –v “-” file.txt 去除所有带负号的数据 -v显示没有的 没有的话则显示只有-的

2、wc -l file.txt显示所有行数 grep –v “-” file.txt |wc –l 统计所有没有负数的

3、sort –n –r –k4 file.txt |sed –n ’1p’ 排序第四列 没有分隔符(即空格),-n数字 –r 从大到小 sed管道 显示第一行 sed –n不显示原文件

    sort –n –r –k4 file.txt |head -1 读取第一个数据 head 第一行

4、awk ‘{if($NF>3){print $0}}’ file.txt NF 最后一列大于3 则打印一整行

5、awk ‘{if($4>0,&&$4<15){print $0}}’ file.txt 列出第四列 大于0小于15 输出整行

    awk ‘{if($4>0,&&$4<15){print $0}}’ file.txt |wc –l 显示行数

练习:


  1. grep和awk

    awk ‘{if($1>200801011){[print $0}}’file.txt

2、

处理命令回顾:

统计文本                wc

文本排序                sort

文本/目录对比            diff

在文件中查找关键行        grep / sed

在行文本中添、删、改    sed

在列文本中显示指定列    awk

在列文本中进行计算        awk

在列文本进行条件选择    awk



grep与正则表达式

首先要记住的是: 正则表达式与通配符不一样,它们表示的含义并不相同!
正则表达式只是一种表示法,只要工具支持这种表示法, 那么该工具就可以处理正则表达式的字符串。vim、grep、awk 、sed 都支持正则表达式,也正是因为由于它们支持正则,才显得它们强大;在以前上班的公司里,由于公司是基于web的服务型网站(nginx),对正则的需求比 较大,所以也花了点时间研究正则,特与大家分享下:

1基础正则表达式

grep 工具,以前介绍过。
grep -[acinv] '搜索内容串' filename
-a 以文本文件方式搜索
-c 计算找到的符合行的次数
-i 忽略大小写
-n 顺便输出行号
-v 反向选择,即找 没有搜索字符串的行

其中搜索串可以是正则表达式!

1
搜索有the的行,并输出行号
$grep -n 'the' regular_express.txt
搜 索没有the的行,并输出行号
$grep -nv 'the' regular_express.txt


2
利 用[]搜索集合字符
[] 表示其中的某一个字符 ,例如[ade] 表示a或d或e

woody@xiaoc:~/tmp$ grep -n 't[ae]st' regular_express.txt
8:I can't finish the test.
9:Oh! the soup taste good!

可以用^符号做[]内的前缀,表示除[]内的字符之外的字 符。
比如搜索oo前没有g的字符串所在的行. 使用 '[^g]oo' 作搜索字符串

woody@xiaoc:~/tmp$ grep -n '[^g]oo' regular_express.txt
2:apple is my favorite
food.
3:
Football game is not use feet only.
18:google is the best
tools for search keyword.
19:go
ooooogle yes!

[] 内可以用范围表示,比如[a-z] 表示小写字母,[0-9] 表示0~9的数字, [A-Z] 则是大写字母们。[a-zA-Z0-9]表示所有数字与英文字符。 当然也可以配合^来排除字符。
搜索包含数字的行
woody@xiaoc:~/tmp$ grep -n '[0-9]' regular_express.txt

5:However ,this dress is about $ 3183 dollars.
15:You are the best is menu you are the no.1.

行首与行尾字符 ^ $. ^ 表示行的开头,$表示行的结尾( 不是字符,是位置)那么‘^$’ 就表示空行,因为只有
行首和行尾。

这里^与[]里面使用的^意义不同。它表示^后面的串是在行的开头。
比如搜索the在开头的行
woody@xiaoc:~/tmp$ grep -n '^the' regular_express.txt
12:the symbol '*' is represented as star.


搜索以小写字母开头的行
woody@xiaoc:~/tmp$ grep -n '^[a-z]' regular_express.txt

2:apple is my favorite food.
4:this dress doesn't fit me.
10:motorcycle is cheap than car.
12:the symbol '*' is represented as star.
18:google is the best tools for search keyword.
19:goooooogle yes!
20:go! go! Let's go.
woody@xiaoc:~/tmp$

搜索开头不是英文字母的行
woody@xiaoc:~/tmp$ grep -n
'^[^a-zA-Z]' regular_express.txt
1:"Open Source" is a good mechanism to develop programs.
21:#I am VBird
woody@xiaoc:~/tmp$

$表示它前面的串是在行的结尾,比如 '/.' 表示 . 在一行的结尾
搜索末尾是.的行
woody@xiaoc:~/tmp$ grep -n
'/.$' regular_express.txt //. 是正则表达式的特殊符号,所以要用/转义
1:"Open Source" is a good mechanism to develop programs.
2:apple is my favorite food.
3:Football game is not use feet only.
4:this dress doesn't fit me.
5:However ,this dress is about $ 3183 dollars.
6:GNU is free air not free beer.
.....

注意在MS的系统下生成的文本文件,换行会加上一个 ^M 字符。所以最后的字符会是隐藏的^M ,在处理Windows
下面的文本时要特别注意!
可以用cat dos_file | tr -d '/r' > unix_file 来删除^M符号。 ^M==/r


那么'^$' 就表示只有行首行尾的空行拉!
搜索空行
woody@xiaoc:~/tmp$ grep -n '^$' regular_express.txt

22:
23:
woody@xiaoc:~/tmp$

搜索非空行
woody@xiaoc:~/tmp$ grep -vn '^$' regular_express.txt

1:"Open Source" is a good mechanism to develop programs.
2:apple is my favorite food.
3:Football game is not use feet only.
4:this dress doesn't fit me.
..........

任意一个字符. 与重复字符 *

在bash中*代表通配符,用来代表任意个 字符,但是在正则表达式中,他含义不同,*表示有0个或多个 某个字符。
例如 oo*, 表示第一个o一定存在,第二个o可以有一个或多个,也可以没有,因此代表至少一个o.

点. 代表一个任意字符,必须存在。 g??d 可以用 'g..d' 表示。 good ,gxxd ,gabd .....都符合。


woody@xiaoc:~/tmp$ grep -n 'g..d' regular_express.txt
1:"Open Source" is a good mechanism to develop programs.
9:Oh! the soup taste good!
16:The world is the same with 'glad'.
woody@xiaoc:~/tmp$

搜索两个o以上的字符串
woody@xiaoc:~/tmp$ grep -n 'ooo*' regular_express.txt //前两个o一定存在,第三个o可没有,也可有多个。

1:"Open Source" is a good mechanism to develop programs.
2:apple is my favorite food.
3:Football game is not use feet only.
9:Oh! the soup taste good!
18:google is the best tools for search keyword.
19:goooooogle yes!

搜索g开头和结尾,中间是至少一个o的字符串,即gog, goog....gooog...等
woody@xiaoc:~/tmp$ grep -n 'goo*g' regular_express.txt
18:google is the best tools for search keyword.
19:goooooogle yes!

搜索g开头和结尾的字符串在的行
woody@xiaoc:~/tmp$ grep -n 'g.*g' regular_express.txt //
.*表示 0个或多个任意字符
1:"Open Source" is a good mechanism to develop programs.
14:The
gd software is a library for drafting programs.
18:
google is the best tools for search keyword.
19:
goooooogle yes!
20:
go! go! Let's go.


限定连续重复字符的范围 { }

. * 只能限制0个或多个, 如果要确切的限制字符重复数量,就用{范围} 。范围是数字用,隔开 2,5 表示2~5个,
2表示2个,2, 表示2到更多个
注意,由于{ }在SHELL中有特殊意义,因此作为正则表达式用的时候要用/转义一下。


搜索包含两个o的字符串的行。
woody@xiaoc:~/tmp$ grep -n 'o/{2/}' regular_express.txt

1:"Open Source" is a good mechanism to develop programs.
2:apple is my favorite food.
3:Football game is not use feet only.
9:Oh! the soup taste good!
18:google is the best tools for search keyword.
19:goooooogle yes!

搜索g后面跟2~5个o,后面再跟一个g的字符串的行。
woody@xiaoc:~/tmp$ grep -n 'go/{2,5/}g' regular_express.txt
18:google is the best tools for search keyword.


搜索包含g后面跟2个以上o,后面再跟g的行。。
woody@xiaoc:~/tmp$ grep -n 'go/{2,/}g' regular_express.txt

18:google is the best tools for search keyword.
19:goooooogle yes!


注意,相让[]中的^ - 不表现特殊意义,可以放在[]里面内容的后面。
'[^a-z/.!^ -]' 表示没有小写字母,没有. 没有!, 没有空格,没有- 的 串,注意[]里面有个小空格。


另外shell 里面的反向选择为[!range], 正则里面是 [^range]



2扩展正则表达式

扩展正则表达式是对基础正则表达式添加了几个特殊构成的。
它令某些操作更加方便。
比如我们要去除 空白行和行首为 #的行, 会这样用:
woody@xiaoc:~/tmp$ grep -v '^$' regular_express.txt | grep -v '^#'
"Open Source" is a good mechanism to develop programs.
apple is my favorite food.
Football game is not use feet only.
this dress doesn't fit me.
............

然而使用支持扩展正则表达式的 egrep 与扩展特殊符号 | ,会方便许多。
注意grep只支持基础表达式, 而egrep 支持扩展的, 其实 egrep 是 grep -E 的别名而已。因此grep -E 支持扩展正则。
那么:
woody@xiaoc:~/tmp$ egrep -v '^$|^#' regular_express.txt
"Open Source" is a good mechanism to develop programs.
apple is my favorite food.
Football game is not use feet only.
this dress doesn't fit me.
....................
这里| 表示或的关系。 即满足 ^$ 或者 ^# 的字符串。

这里列出几个扩展特殊符号:
+, 于 . * 作用类似,表示 一个或多个重复字符。
?, 于 . * 作用类似,表示0个或一个字符。
|,表示或关系,比如 'gd|good|dog' 表示有gd,good或dog的串
(),将部分内容合成一个单元组。 比如 要搜索 glad 或 good 可以这样
'g(la|oo)d'
()的好处是可以对小组使用 + ? * 等。
比如要搜索A和C开头结尾,中间有至少一个(xyz) 的串,可以这样 :
'A(xyz)+C'

ref:

http://blog.csdn.net/tenfyguo/article/details/6387786

 

 

 

 


点赞

评论 (0 个评论)

facelist

您需要登录后才可以评论 登录 | 注册

  • 关注TA
  • 加好友
  • 联系TA
  • 0

    周排名
  • 0

    月排名
  • 0

    总排名
  • 0

    关注
  • 2

    粉丝
  • 1

    好友
  • 2

    获赞
  • 14

    评论
  • 3241

    访问数
关闭

站长推荐 上一条 /2 下一条

小黑屋| 关于我们| 联系我们| 在线咨询| 隐私声明| EETOP 创芯网
( 京ICP备:10050787号 京公网安备:11010502037710 )

GMT+8, 2024-4-18 23:09 , Processed in 0.028321 second(s), 14 queries , Gzip On, Redis On.

eetop公众号 创芯大讲堂 创芯人才网
返回顶部