grep
-i 忽略大小写
-d recurse 对文件夹进行递归遍历检查,skip 不进行递归检查
# 影响输出参数
-n 显示行号
-m 匹配m行停止
-B -A -C 显示匹配行的前、后、前后n行信息
-c 显示匹配的行数
示例
grep -i xxx -n t.info -A/B/C 3
grep -i xxx -n t.info -c
grep -i xxx -n t.info -m 2
grep -i xxx -n ./* -d recurse/skip
awk
awk '[BEGIN {}] pattern {每行处理过程} [END {}]' file
# 注意 BEGIN END必须大写
# BEGIN:定义FS/OFS/ORS/变量等
# 输出内置变量 NR/NF/NFR
pattern:/匹配条件(正则表达式:^ $ . [abc] s1|s2|s3等)/ 或 条件表达式。
awk 'BEGIN {FS="%%";OFS="^^";ORS=";"} { print $0}' file
awk 'BEGIN {FS=" "} /^aa/ {print NR,$0}' file
awk 'BEGIN {FS=" ";p1=0;p2=0} /^aa/ {p1=p1+1;p2=p1*2;print NR,$0} END {print p1,p2}' file
awk 'BEGIN {FS=" ";p1=0;p2=0} /^aa/ {p1=p1+1;p2=p1*2;print NR,p1,$p1} END {print p1,p2}' file
其中 $p1代表取第p1列的值,假设p1=3则代表$3
awk 'NR==2,NR==10 {print $0}' file 等价于 awk 'NR>=2 && NR<=10 {print $0}' file
awk 'BEGIN {p1=0;p2=0} if (NR%3==0) {p1=p1+1} else {p2=p2+2} {print NR,p1,p2} END {print p1,p2}' file
参考:
http://www.cnblogs.com/chengmo/archive/2010/10/06/1844818.html
http://www.cnblogs.com/chengmo/archive/2010/10/04/1842073.html
sed 行操作
选择/删除 指定行,
替换:部分内容 s/xx/yy/g,替换整行c\,多个dany/s1...sn/d1...dn/)
追加(i\当前匹配行前,a\当前匹配行后)
处理下一行数据(n:例如(sed {n;s/xx/yy/g;} file。)
多个组合逻辑(-e)操作
# 输出指定行 sed -n 'n1,n2p' file
# -n 取消默认输出, -n 与p一般配套使用
sed -n '2p' file -- 只有第2行
sed -n '1,3p' file -- 1至3行
sed -n '10,$p' file -- 10(含)行至结尾
# 删除指定行 sed 'n1,n2d' file,终端不会显示,需要重定向输出到文件
sed '4d' file -- 只删除4行
sed '3,5d' file -- 删除3至5行
# 选取内容输出到文件
(1)sed '/se/w 2.t' file
(2)sed -n '/se/p' file > 3.t
2.t与3.t中的内容一样
参考
http://www.cnblogs.com/edwardlost/archive/2010/09/17/1829145.html
sort