正则表达式grep sed awk总结

简介:

1.grep命令总结

[root@cacti ~]# cat zh888.txt  //查看zh888.txt内容
123
456
abc
def
def
[root@cacti ~]# grep -c "123" zh888.txt //打印123的行数为1行
1
[root@cacti ~]# echo "123456">>zh888.txt //在追加123456到zh888.txt文本中

[root@cacti ~]# grep -c "123" zh888.txt  //打印123的行数变为2
2
[root@cacti ~]# grep -v "123" zh888.txt //把123行不打印出来
456
abc
def
def
[root@cacti ~]# grep -n "123" zh888.txt //输出内容和行数
1:123
6:123456
[root@cacti ~]# grep [0-9] zh888.txt //grep0-9的数字
123
456
123456
[root@cacti ~]# grep -v [0-9] zh888.txt //去掉0-9
abc
def
def
[root@cacti ~]# grep '^1' zh888.txt   //,”^”表示行的开始,”$”表示行的结尾,那么空行则表示”^$”,
123
123456
[root@cacti ~]# grep 'f$' zh888.txt //过滤f结尾的内容
def
def
[root@cacti ~]# grep '^[^a-zA-Z]' zh888.txt //把数字全部过滤出来
123
456
123456
456789
234567
[root@cacti ~]# grep '1..4' zh888.txt    //“.”表示任意一个字符,上例中,就是把符合r与o之间有两个任意字符的行过滤出来。
123456


[root@cacti ~]# grep '123*' zh888.txt     //“*”表示零个或多个前面的字符。
123
123456


[root@cacti ~]# grep '.*' zh888.txt   //‘.*’表示零个或多个任意字符,空行也包含在内。
123
456
abc
def
def
123456
456789
234567


2.sed的命令总结
[root@cacti ~]# sed -n '1'p zh888.txt  //打印zh888.txt第一行内容
123

[root@cacti ~]# sed -n '1,3'p zh888.txt //打印1-3行的内容
123
456
abc

[root@cacti ~]# sed -n '/abc/'p zh888.txt //打印包含abc的字符串内容
abc

[root@cacti ~]# sed -n '/^1/'p zh888.txt //打印1开头的内容
123
123456

[root@cacti ~]# sed -n '/6$/'p zh888.txt //打印6结尾的内容
456
123456

[root@cacti ~]# sed -n '/1..4/'p zh888.txt //打印1..4的任意内容
123456


[root@cacti ~]# sed -n '/23*/'p zh888.txt  //大衣呢23结尾的任意内容
123
123456
234567

[root@cacti ~]# sed -e '1'p -e '/123/'p -n zh888.txt //打印123开头的内容
123
123
123456

[root@cacti ~]# sed '1'd zh888.txt //删除第一行内容
456
abc
def
def
123456
456789
234567

[root@cacti ~]# sed '2,$'d zh888.txt //删除2到结尾的所有内容
123

[root@cacti ~]# sed '1,3'd zh888.txt //删除1到3的内容
def
def
123456
456789
234567

[root@cacti ~]# sed '/123/'d zh888.txt //删除123的内容
456
abc
def
def
456789
234567

[root@cacti ~]# sed '1,2s/123/888/g' zh888.txt //1到2行的123替换成888以全局替换,/可以换成#或者@都是可以的。
888
456
abc
def
def
123456
456789
234567


[root@cacti ~]# sed 's/[0-9]//g' zh888.txt //把0到9数字全部过滤掉


abc
def
def

[root@cacti ~]# sed 's/[a-zA-Z]//g' zh888.txt //吧a-zA-Z的字母全部替换掉
123
456

 

123456
456789
234567


3.awk命令总结


[root@cacti ~]# echo "1:2:3">>zh888.txt //添加内容到zh888中去


[root@cacti ~]# echo "4:5:6:7">>zh888.txt //添加内容到zh888中区


[root@cacti ~]# tail -n2 zh888.txt |awk -F':' '{print $1}' //查看末尾2行利用管道和分隔符打印第一段字段内容。
1
4

[root@cacti ~]# tail -n2 zh888.txt |awk -F':' '{print $1"zh888"$2"zh888"}' //在第一字段和第二字段末尾加上zh888
1zh8882zh888
4zh8885zh888

[root@cacti ~]# tail -n2 zh888.txt |awk -F':' '{print "zh888"$1}' //在第一字段前面添加zh888内容
zh8881
zh8884

[root@cacti ~]# awk '/123/' zh888.txt //查找123内容
123
123456

[root@cacti ~]# awk -F':' '$1~/7/' zh888.txt  //让awk来匹配第一段7开头的数字
456789
234567

 


[root@cacti ~]# awk -F':' '/1/ {print $2} /2/ {print $3}' zh888.txt //利用awk连续匹配


2
3

[root@cacti ~]# awk -F':' '$2=="5"' zh888.txt
4:5:6:7
//利用逻辑判断等于来判断第二字段是否是5然后打印出来,awk中是可以用逻辑符号判断的,比如’==’就是等于,也可以理解为“精确匹配”。另外也有’>’, ‘>=’, ‘<’, ‘<=’, ‘!=’ 等等,值得注意的是,即使$3为数字,awk也不会把它当数字看待,它会认为是一个字符。所以不要妄图去拿$3当数字去和数字做比较。


[root@cacti ~]# awk '{print NR}' zh888.txt //利用分隔符分割一共多少段
1
2
3
4
5
6
7
8
9
10

[root@cacti ~]# awk '{print NF}' zh888.txt //一共有多少行
1
1
1
1
1
1
1
1
1
1

[root@cacti ~]# awk 'NR>=5' zh888.txt //大于5的行数打印出来
def
123456
456789
234567
1:2:3
4:5:6:7




本文转自zh888 51CTO博客,原文链接:http://blog.51cto.com/zh888/1401355,如需转载请自行联系原作者
相关文章
|
4月前
|
Linux Perl
sed删除匹配正则表达式的行
sed删除匹配正则表达式的行
271 1
|
3月前
|
监控 Unix Linux
强大的文本处理工具组合:egrep、正则表达式、awk、cut、xargs
了解Linux和Unix文本处理的关键工具:egrep(扩展正则表达式搜索)、正则表达式、awk(文本分析)、cut(剪切文本)和xargs(传递参数给命令)。这些工具组合使用可高效处理、分析大量数据,尤其在日志分析和文本查询中。例如,从Web服务器日志中查找404错误,先用egrep筛选,再用awk或cut提取IP和URL,最后用xargs配合其他命令执行操作。掌握这些工具能提升工作效率。
|
4月前
|
机器学习/深度学习 Shell 开发工具
正则表达式 与文本三剑客(sed grep awk)
正则表达式 与文本三剑客(sed grep awk)
|
4月前
|
Perl
sed删除不匹配正则表达式的行(保留匹配行)
sed删除不匹配正则表达式的行(保留匹配行)
473 2
|
4月前
|
运维 Unix Linux
grep正则表达式搜索
grep正则表达式搜索
43 3
|
4月前
|
Linux Perl
使用awk和正则表达式过滤文本或字符串 - 详细指南和示例
使用awk和正则表达式过滤文本或字符串 - 详细指南和示例
126 0
|
11月前
|
Linux Perl
[笔记]linux grep之正则表达式
[笔记]linux grep之正则表达式
|
3月前
|
数据库 Python
Python网络数据抓取(8):正则表达式
Python网络数据抓取(8):正则表达式
36 2
|
3月前
|
自然语言处理 JavaScript 前端开发
Python高级语法与正则表达式(二)
正则表达式描述了一种字符串匹配的模式,可以用来检查一个串是否含有某种子串、将匹配的子串做替换或者从某个串中取出符合某个条件的子串等。
|
3月前
|
安全 算法 Python
Python高级语法与正则表达式(一)
Python提供了 with 语句的写法,既简单又安全。 文件操作的时候使用with语句可以自动调用关闭文件操作,即使出现异常也会自动关闭文件操作。