Linux大神养成之正则表达式(grep,sed) 秒速五厘米 2022-08-03 04:27 72阅读 0赞 ## Linux大神养成之正则表达式 ## ### grep命令 ### 参数: * \-A后面加数字,代表after,表示把该行的后续n行也列出来 * \-B后面加数字,代表befer,表示把改行的前面n行也列出来 $ dmesg | grep -n -A3 -B2 --color=auto 'eth' 1. **查找特定字符串** $ cat -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.^M 6 GNU is free air not free beer.^M 7 Her hair is very beauty.^M 8 I can't finish the test.^M 9 Oh! The soup taste good.^M 10 motorcycle is cheap than car. 11 This window is clear. 12 the symbol '*' is represented as start. 13 Oh! My god! 14 The gd software is a library for drafting programs.^M 15 You are the best is mean you are the no. 1. 16 The world <Happy> is the same with "glad". 17 I like dog. 18 google is the best tools for search keyword. 19 goooooogle yes! 20 go! go! Let's go. 21 # I am VBird $ grep -n 'the' regular_express.txt > 8:I can’t finish the test.^M > 12:the symbol ‘\*’ is represented as start. > 15:You are the best is mean you are the no. 1. > 16:The world is the same with “glad”. > 18:google is the best tools for search keyword. 2.**利用中括号来查找集合字符** $ grep -n 't[ae]st' regular_express.txt > 8:I can’t finish the test.^M > 9:Oh! The soup taste good.^M > 查找包含tast或test的字符的句子 $ 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:goooooogle yes! > ^反向选择,即非的意思,就是oo的前面的字母不是g的 $ grep '[^a-z]oo' -n regular_express.txt > 3:Football game is not use feet only. > oo前面是非小写的 3.**行首与行尾符^$** `$ 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 start. > 18:google is the best tools for search keyword. > 19:goooooogle yes! > 20:go! go! Let’s go. > ^放在外面表示行首的意思,表示选出以小写字母开始的行 $ grep -n '^[^a-zA-Z]' regular_express.txt > 1:”Open Source” is a good mechanism to develop programs. > 21:\# I am VBird > 不是以字母开头的行 $ 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. > 10:motorcycle is cheap than car. > 11:This window is clear. > 12:the symbol ‘\*’ is represented as start. > 15:You are the best is mean you are the no. 1. > 16:The world is the same with “glad”. > 17:I like dog. > 18:google is the best tools for search keyword. > 20:go! go! Let’s go. > $表示行尾,表示以 ‘.’ 结尾的行,其中 ‘\\’ 是转义符 $ grep -v '^$' /etc/syslog.conf | grep '^#' > \# Log all kernel messages to the console. > \# Logging much else clutters up the screen. > \#kern.\* /dev/console > \# Log anything (except mail) of level info or higher. > \# Don’t log private authentication messages! > \# The authpriv file has restricted access. > \# Log all the mail messages in one place. > \# Log cron stuff > \# Everybody gets emergency messages > \# Save news errors of level crit and higher in a special file. > \# Save boot messages also to boot.log > 选出非空行的,然后在选出以\#开头的文字 4.**任意一个.和重复的\*** `$ grep -n 'g..d' regular_express.txt` > 1:”Open Source” is a good mechanism to develop programs. > 9:Oh! The soup taste good.^M > 16:The world is the same with “glad”. > ‘g..d’表示g和d中间有两个连续任意字符的都可以匹配 $ grep -n 'goo*g' regular_express.txt > 18:google is the best tools for search keyword. > 19:goooooogle yes! > ‘goo\*g’前一个o必须有,后一个’o \*’表示一个或多个,所以为至少一个o $ grep -n g.*g regular_express.txt > 1:”Open Source” is a good mechanism to develop programs. > 14:The gd software is a library for drafting programs.^M > 18:google is the best tools for search keyword. > 19:goooooogle yes! > 20:go! go! Let’s go. > 表示由g开头g结尾的行 $ grep -n '[0-9][0-9]*' regular_express.txt > 5:However, this dress is about $ 3183 dollars.^M > 15:You are the best is mean you are the no. 1. > 表示以数字开头,后面有0个到任意个数字的行 5.限定连续RE字符的范围\{\} `$ 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.^M > 18:google is the best tools for search keyword. > 19:goooooogle yes! > 表示包含两个‘o’的行 $ grep -n 'go\{2,\}g' regular_express.txt > 18:google is the best tools for search keyword. > 19:goooooogle yes! > g和g中间包含两个以上的o ### sed工具 ### 参数: * \-n 安静模式,只列出经过特殊处理的行 * \-i 直接修改文件内容,而不是屏幕输出 * \-e 直接在命令行模式上sed动作编辑 * \-f sed动作写在一个文件中 * \-r扩展正则语法 函数参数; * a 新增 * c 替换 * d 删除 * i 插入 * p打印 * s 替换(和c的不同看练习) 1.以行为单位进行**新增/删除** $ nl /etc/passwd | sed '2,5d' > 1 root:x:0:0:root:/root:/bin/bash > 6 sync:x:5:0:sync:/sbin:/bin/sync > 7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown > 8 halt:x:7:0:halt:/sbin:/sbin/halt > 9 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin > PS:表示删除第2-5行 $ nl /etc/passwd | sed '2a Drink tea or .....\ > drink bear ?' > 1 root:x:0:0:root:/root:/bin/bash > 2 bin:x:1:1:bin:/bin:/sbin/nologin > Drink tea or ….. > drink bear ? > 3 daemon:x:2:2:daemon:/sbin:/sbin/nologin > 4 adm:x:3:4:adm:/var/adm:/sbin/nologin > 5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin > PS:给第二行之后添加后面的字符串,用’\\’表示新行进行增加 2.以行为单位进行**替换**显示的功能 `$ nl /etc/passwd | sed '2,5c No 2-5 number'` > 1 root:x:0:0:root:/root:/bin/bash > No 2-5 number > 6 sync:x:5:0:sync:/sbin:/bin/sync > 7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown > PS:将2-5行替换成‘No 2-5 number’ $ nl /etc/passwd | sed -n '2,5p' > 2 bin:x:1:1:bin:/bin:/sbin/nologin > 3 daemon:x:2:2:daemon:/sbin:/sbin/nologin > 4 adm:x:3:4:adm:/var/adm:/sbin/nologin > 5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin > PS:注意这里 '-n' 安静模式,只列出特殊处理的行 $ /sbin/ifconfig eth1 > eth1 Link encap:Ethernet HWaddr 6C:AE:8B:21:98:9B > inet addr:10.209.102.43 Bcast:10.209.102.127 Mask:255.255.255.128 > UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 > RX packets:47103902950 errors:0 dropped:7743 overruns:7743 frame:0 > TX packets:11073405019 errors:0 dropped:0 overruns:0 carrier:0 > collisions:0 txqueuelen:1000 > RX bytes:59771891524541 (54.3 TiB) TX bytes:810933823454 (755.2 GiB) $ /sbin/ifconfig eth1 | grep 'inet addr' > inet addr:10.209.102.43 Bcast:10.209.102.127 Mask:255.255.255.128 $ /sbin/ifconfig eth1 | grep 'inet addr' | sed 's/^.*addr://g' > 10.209.102.43 Bcast:10.209.102.127 Mask:255.255.255.128 > PS:”sed ‘s/要替换字符/新字符/g’” $ /sbin/ifconfig eth1 | grep 'inet addr' | sed 's/^.*addr://g' | sed 's/Bcast.*//g' > 10.209.102.43 > PS:其实就相当于hostname -i $ cat /etc/man.config | grep 'MAN' | sed 's/#.*$//g' | sed '/^$/d' > MANPATH /usr/share/man > MANPATH /usr/man > MANPATH /usr/local/share/man > MANPATH /usr/local/man > MANPATH /usr/X11R6/man > MANPATH\_MAP /bin /usr/share/man > MANPATH\_MAP /sbin /usr/share/man > MANPATH\_MAP /usr/bin /usr/share/man > MANPATH\_MAP /usr/sbin /usr/share/man > PS:先选出包含‘MAN’的行,然后替换掉’\#’开头的,即注释,然后把空行删除,注意最后的空行表示是写在一对 **‘/ /’**中 * 直接修改文件(危险操作) $ sed -i 's/\.$/\!/g' regular_express.txt > PS:直接把原文件中的‘.’结尾的全部替换成‘!’ $ sed -i '$a # This is a tese' regular_express.txt > PS:给文章的最后一行添加一句话
还没有评论,来说两句吧...