本文为霍格沃兹测试开发学社学员学习笔记分享
原文链接:https://ceshiren.com/t/topic/26282
grep pattern file
grep -o pattern file
grep -A -B -C pattern file
grep pattern -r dir/
grep -H 111 /tmp/1
grep -l 111 /tmp/1
man grep
命令可以找到grep的主流用法grep -i pattern file
grep -v pattern file
grep -E pattern file
grep 111 -r /tmp/demo/ --include "11*"
ps -ef | grep ssh(使用管道连接,两个命令ps、grep都会同时执行,两个进程都会启动,使用grep进行过滤,这个命令也在执行,它里面也有ssh,所以带有ssh的进程都被搜索出来了)
503 2507 1 0 29 821 ?? 0:00.08 /usr/bin/ssh-agent
503 50022 11154 0 8:11下午 ttys002 0:00.00 grep ssh
ps -ef | grep ssh | grep -v grep(通过 -v 过滤掉包含grep的进程)
503 2507 1 0 29 821 ?? 0:00.08 /usr/bin/ssh-agent
awk 'pattern{action}'
$1~/pattern/
/pattern/
$2>2
$1=="b"
开始和结束 awk 'BEGIN{}END{}'
正则匹配
awk '/Running/'
awk '$2~/xxx/'
行数表达式
awk 'NR==2'
awk 'NR>1'
区间选择
awk '/aa/,/bb/'
awk '/1/,NR==2'
{action}
{print $0}
{print $2}
{$1="abc"}
{$1=$1;print $0}
echo 1:2:3 | awk 'BEGIN{RS=":"}{print $0}'
1
2
3
echo '1
2
3' | awk 'BEGIN{RS="";FS="\n";OFS=":"}{$1=$1;print $0}'
1:2:3
echo '1
2
3' | awk 'BEGIN{ORS=":"}{$1=$1;print $0}'
1:2:3:
echo '1,10
2,20
3,30' | awk 'BEGIN{total=0;FS=","}{total+=$2}END{print total/NR}'
20
echo 'a, 1, 10
a, 2, 20
a, 3, 30
b, 1, 5
b, 2, 6
b, 3, 7' | awk '{data[$1]+=$3}
END{for(k in data) print k,data[k]}'
a, 60
b, 18
echo 'a, 1, 10
a, 2, 20
a, 3, 30
b, 1, 5
b, 2, 6
b, 3, 7' | awk '{data[$1]+=$3;count[$1]+=1;}
END{for(k in data) print k,data[k]/count[k]}'
a, 20
b, 6
sed -n 2p
sed $d
echo a:b:c | sed 's/:/123&/'
a123:b:c
echo a:b:c | sed 's/:/&123/'
a:123b:c
echo a:b:c | sed 's#:#|#g'
a|b|c
echo 0 1 2 3 4 | sed -E 's#([1-3]) ([1-3]) ([1-3])#\3 \2 \1#'
0 3 2 1 4