grep sed awk 文本处理 题目及答案

来源:互联网 发布:油性皮肤护肤品知乎 编辑:程序博客网 时间:2024/05/16 11:51


grep 基础部分
1、grep -i  "^s"  /proc/meminfo    
grep "(^s|^S)"  /proc/meminfo
SED -rN '/^s |^S /p'   /proc/meminfo
2、 grep -E'^root|^centos|^user1'/etc/passwd
3、grep -E '\b\w+\b\(' /etc/rc.d/init.d/functions
4、echo /etc/sysconfig/network-scrips/ifconfig-eth0 | grep -E "[^/]+$"
echo /etc/sysconfig/network-scrips/ifconfig-eth0 | grep -E "^/.*/"
5、
6、useradd bash testbash basher nologin -s /sbin/nologin
grep -E"^\b(\w+)\b.*\1$" /etc/passwd 
7、ifconfig | grep -oe '([0-9]{1-3}\.){3}[0-9]{1-3}'

ifconfig | grep -po '(?<=inet >*?<=inet)'

进阶部分
grep "main" . /etc/fstab-r --include *.{php,html}
2、grep "main" . -r --exclude "readme"
grep  "main" . -r --exclude-from filelist
grep "string" . -r -n
grep -l "root" /etc/fstab /etc/passwd
grep -n "^[^a-zA-Z]" /etc/fstab
grep -E "cd{1,2}3" /etc/fstab
grep -Ev '^#|^$' /etc/fstab
grep -P"^\s+\S"
grep "nologin$"
grwep -C 2 -E "^_?[a-zA-Z]+\b" /etc/init/functions 
高级部分
习题四


sed 习题5
1.sed 's/[0-9]//g'
2、echo yy/mm/dd | sed 's#/#:#g'
3、sed -n'1~2p'奇数   偶数'2~2p'
4、sed '' /tmp /test 
5、sed -n '3~3p' /tmp/test  sed -n '3,${p;n;n}'
8、sed  '5~5a\  '  = '5~5G'
9、sed -n '/^[^1]/p'
10、sed '1,20{s/aaa/AAA/g;s/ddd/DDD/}'
习题四
10、sed  -r "s/(.*)(\b[a-zA-Z]+\b)([^a-zA-Z]*)/\1\3/"

awk :


附加题:一、打印以下用户

marry 2143 78 84 77
jack 2321 66 78 45
tom 2122 48 77 71
mike 2537 87 97 95
bob 2415 40 57 62
awk 'BEGIN{printf "%-10s%-8s%-6s%-10s%-10s%-10s%-10s\n","lineno.","name","no.",math","english","computer","total";print"------------------------------"; math=0;english=0;computer=0;total=0;}{printf "%-10s%-8s%-6s%-10s%-10s%-10s%-10s\n",NR,$1,$2,$3,$4,$5,$3+$4+$5;math+=$3;english+=$4;computer+=$5}END{print"---------------";printf "%-24s%-10s%-10s%-10s\n","total:",math,english,computer;printf "%-24s%-10s%-10s%-10s\n","AVG:",math/NR,English/NR,computer/NR;}'  /tmp/test

二、
netstate -tan   :结合awk和netstat统计网络状态连接数
 awk '/^tcp/ {++state[$NF]} END {for(key in state) print key,"\t",state[key]}'

答案:netstat -tan | grep "^tcp" |awk '{++state[$NF]}END{for (i in state)printf "%s:s\n",i,state[i]}'


原创粉丝点击