最近一直在研究shell脚本这块,刚好闲下来整了下自己手头上比较好的资料中的一些范例,以下是我整理的鸟哥私房菜里面正则表达式里面比较基础的一些语法详解,适合新手查阅。 首先先复制一段范例: # vi re
最近一直在研究shell脚本这块,刚好闲下来整了下自己手头上比较好的资料中的一些范例,以下是我整理的鸟哥私房菜里面正则表达式里面比较基础的一些语法详解,适合新手查阅。
首先先复制一段范例:
# vi 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.
However, this dress is about $ 3183 dollars.
GNU is free air not free beer.
Her hair is very beauty.
I can't finish the test.
Oh! The soup taste good.
motorcycle is cheap than car.
This window is clear.
the symbol '*' is represented as start.
Oh!My god!
The gd software is a library for drafting programs.
You are the best is mean you are the no. 1.
The world <Happy> is the same with "glad".
I like dog.
google is the best tools for search keyWord.
goooooogle yes!
go! go! Let's go.
# I am VBird
--------------------------------
#export LANG=C
grep
1.搜寻特定字符串"the"
注: n为显示行号
# grep -n 'the' regular_express.txt
2.反向搜寻特定字符串"the"
# grep -vn 'the' regular_express.txt
3.取得任意大小写"the"的这个字符串
# grep -in 'the' regular_express.txt
4.利用括号 [] 来搜寻集合字符
搜索test或taste这两个单词时,发现他们有共同的't?st',所以可以这么搜寻
# grep -n 't[ae]st' regular_express.txt
# grep -n 'oo' regular_express.txt
# grep -n '[^g]oo' regular_express.txt
# grep -n '[^a-z]oo' regular_express.txt
# grep -n '[0-9]' regular_express.txt
# grep -n '[^[:lower:]]oo' regular_express.txt
# grep -n '[[:digit:]]' regular_express.txt
5.显示行首为'the'的字符串
# grep -n '^the' regular_express.txt
# grep -n '^[a-z]' regular_express.txt
6.显示行尾为点 . 的那一行
# grep -n '.$' regular_express.txt
7.显示5-9行数据
# cat -An regular_express.txt |head -n 10 |tail -n 6
8.显示空白行
# grep -n '^$' regular_express.txt
9.找出g??d字符串,起头g结束d的四个字符串
# grep -n 'g..d' regular_express.txt
10. o*代表空字符(就是有没有字符都可以)或者一个到N个o字符,所以grep -n 'o*' regular_express.txt就会把所有行全部打印出来,
11.oo*代表o+空字符或者一个到N个o字符,所以grep -n 'oo*' regular_express.txt就会把o,oo,ooo等的行全部打印出来
12."goo*g"代表gog,goog,gooog...等
# grep -n 'goo*g' regular_express.txt
13.找出含g...g字符串的行
注: .代表任意字符, .*则就代表空字符或者一个到N个任意字符
# grep -n 'g.*g' regular_express.txt
14.找出含有数字的行
# grep -n '[0-9][0-9]*' regular_express.txt
15.找出含两个o的字符串
注:{}因为在shell里有特殊意义,所以需要加跳脱符来让其失去意义
# grep -n 'o{2}' regular_express.txt
# grep -n 'go{2,5}g' regular_express.txt
# grep -n 'go{2,}g' regular_express.txt
sed:
插入:
1.将/etc/passwd 的内容列出并打印行号,同时,将2-5行删除显示
# nl /etc/passwd | sed '2,5d'
# nl /etc/passwd | sed '2d'
# nl /etc/passwd | sed '3,$d'
2.在第二行后加上一行test
# nl /etc/passwd | sed '2a test'
# nl /etc/passwd | sed '2i test'
# nl /etc/passwd | sed '2a test
> test'
替换行:
3.将2-5行内容取代为 No 2-5 number
# nl /etc/passwd | sed '2,5c No 2-5 number'
4 列出/etc/passwd 内第5-7行
# nl /etc/passwd |sed -n '5,7p'
替换字符串:
sed 's/被替换字符串/新字符串/g'
1.获取本机IP的行
# /sbin/ifconfig eth0 |grep 'inet addr'
# /sbin/ifconfig eth0 |grep 'inet addr'| sed 's/^.*addr://g'
# /sbin/ifconfig eth0 |grep 'inet addr'| sed 's/^.*addr://g'| sed 's/Bcast:.*$//g'
-------------------
192.168.100.74
-------------------
2.用grep将关键词MAN所在行取出来
# cat /etc/man.config |grep 'MAN'
# cat /etc/man.config |grep 'MAN'| sed 's/^#.*$//g'
# cat /etc/man.config |grep 'MAN'| sed 's/^#.*$//g'| sed '/^$/d'
3.利用sed将regular_express.txt内每一行若为.的换成!
注:-i参数会直接修改文本,而并非直接输出
# sed -i 's/.*.$/!/g' regular_express.txt
4.利用sed在文本最后一行加入 #This is a test
注: $代表最后一行 a代表行后添加
# sed -i '$a #This is a test' regular_express.txt
# sed -i '6,6c SELINUX=disabled' /etc/selinux/config
# grep -v '^$' regular_express.txt |grep -v '^#'
# egrep -v '^$'|'^#' regular_express.txt
1. +表示重复一个或一个以上的前一个RE字符
例如:egrep -n 'go+d' regular_express.txt
普通写法: grep -n 'goo*d' regular_express.txt
2. ?表示重复零个或一个前一个RE字符
例如: egrep -n 'go?d' regular_express.txt
3. |表示用或的方式找出数个字符串
例如: egrep -n 'gd|good' regular_express.txt
4. ()表示找出群组字符串
例如: egrep -n 'g(la|oo)d' regular_express.txt
也就是搜寻(glad)或good这两个字符串
5. ()+多个重复群组判别
例如: echo 'AxyzxyzxyzxyzC'|egrep 'A(xyz)+C'
也就是要找开头是A结尾是C 中间有一个以上的'xyz'字符串的意思
awk:
1.用last取出登陆数据前五行
# last -n 5
# last -n 5 |awk '{print $1 "t" $3}'
# last -n 5 |awk '{print $1 "t lines:" NR "t columes:" NF}'
2.在/etc/passwd中以:来作为分段字符,则我们要查阅第三栏小于10以下的数据,并只列出账号与第三栏
# cat /etc/passwd | awk '{FS=":"} $3<10 {print $1 "t t"$3}'
# cat /etc/passwd | awk 'BEGIN {FS=":"} $3<10 {print $1 "t t"$3}'
df:
比较两个文件的差异:
# diff /etc/rc3.d/ /etc/rc5.d/
-------------------
Only in /etc/rc3.d/: K30spice-vdagentd
Only in /etc/rc5.d/: S70spice-vdagentd
-------------------
实例:
1。统计tcp连接状态
# netstat -na | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}'
/^tcp/
--结束END--
本文标题: Shell正则表达式之grep、sed、awk实操笔记
本文链接: https://lsjlt.com/news/18222.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0