Tricks of SED/AWK/GREP
2013-10-21 20:44
295 查看
/PAT1/ or /PAT2/ or /PAT3/
This matches lines that contain any one of a number of patterns (three in this examples). It can be done with many tools.
grep
When using -E, the patterns have to be in ERE syntax, while with -e the BRE syntax has to be used.
awk
Awk uses ERE syntax for its patterns.
sed
One way with sed:
Sed normally uses BRE syntax.
perl
Perl patterns uses PCRE syntax (basically, a much richer superset of ERE).
/PAT1/ and /PAT2/ and /PAT3/
This selects lines that match all the patterns. Order may or may not matter. Again, it can be done with many tools.
grep
If order matters, one can simply do:
(or whatever order one wants).
If order does not matter, then with grep all the possible combinations have to be considered:
grep -e 'PAT1.*PAT2.*PAT3' -e 'PAT1.*PAT3.*PAT2' -e 'PAT2.*PAT1.*PAT3' -e 'PAT2.*PAT3.*PAT1' -e 'PAT3.*PAT1.*PAT2' -e 'PAT3.*PAT2.*PAT1' file.txt
Obviously, this method is not very efficient and does not scale well. This can be improved a little at the price of using multiple processes:
awk
Awk scales better:
This finds also lines where matches overlap.
sed
This looks a bit weird, but it's obvious: if the line doesn't match any of the patterns, we delete it. At the end, the lines that "survive" and are thus printed must be only those that match all the patterns.
perl
This is basically the same as the awk solution:
This matches lines that contain any one of a number of patterns (three in this examples). It can be done with many tools.
grep
grep -E 'PAT1|PAT2|PAT3' file.txt grep -e 'PAT1' -e 'PAT2' -e 'PAT3' file.txt
When using -E, the patterns have to be in ERE syntax, while with -e the BRE syntax has to be used.
awk
awk '/PAT1|PAT2|PAT3/' file.txt awk '/PAT1/ || /PAT2/ || /PAT3/' file.txt
Awk uses ERE syntax for its patterns.
sed
One way with sed:
sed '/PAT1/b; /PAT2/b; /PAT3/b; d' file.txt
Sed normally uses BRE syntax.
perl
perl -ne 'print if /PAT1/ or /PAT2/ or /PAT3/' file.txt
Perl patterns uses PCRE syntax (basically, a much richer superset of ERE).
/PAT1/ and /PAT2/ and /PAT3/
This selects lines that match all the patterns. Order may or may not matter. Again, it can be done with many tools.
grep
If order matters, one can simply do:
grep 'PAT1.*PAT2.*PAT3' file.txt
(or whatever order one wants).
If order does not matter, then with grep all the possible combinations have to be considered:
grep -e 'PAT1.*PAT2.*PAT3' -e 'PAT1.*PAT3.*PAT2' -e 'PAT2.*PAT1.*PAT3' -e 'PAT2.*PAT3.*PAT1' -e 'PAT3.*PAT1.*PAT2' -e 'PAT3.*PAT2.*PAT1' file.txt
Obviously, this method is not very efficient and does not scale well. This can be improved a little at the price of using multiple processes:
grep 'PAT1' file.txt | grep 'PAT2' | grep 'PAT3'
awk
Awk scales better:
awk '/PAT1/ && /PAT2/ && /PAT3/' file.txt # order does not matter
This finds also lines where matches overlap.
sed
sed '/PAT1/!d; /PAT2/!d; /PAT3/!d' file.txt
This looks a bit weird, but it's obvious: if the line doesn't match any of the patterns, we delete it. At the end, the lines that "survive" and are thus printed must be only those that match all the patterns.
perl
This is basically the same as the awk solution:
perl -ne 'print if /PAT1/ and /PAT2/ and /PAT3/' file.txt
相关文章推荐
- 动态清空 nohup 输出文件
- install scrapy with pip and easy_install
- Linux Shell常用技巧
- Shell编程的一些知识集合
- linux Shell学习笔记第五天
- Perl 获取shell命令的执行结果
- Linux shell操作mysql数据库深入解析
- Linux Shell简介
- shell 中数学计算总结
- linux shell 中 2>&1的含义
- shell编程基础 单引号和双引号的区别
- 对Shell 脚本加密的方法
- Do All in Cmd Shell一切在命令行下完成第1/6页
- shell 基本计算、逻辑运算、位运算详解
- linux shell的输出效果修改方法(界面颜色)
- linux shell命令的常用快捷键
- shift妙用之解决shell编程中的入参问题
- Shell 命令执行顺序分析[图]
- shell的比较运算介绍
- Linux SHELL if命令参数说明