码迷,mamicode.com
首页 > 其他好文 > 详细

grep/awk/sed 或者 并且 否定

时间:2014-07-10 14:44:42      阅读:212      评论:0      收藏:0      [点我收藏+]

标签:style   strong   io   line   amp   re   

Grep ‘OR‘ Operator

Find all the lines in a file, that match any of the following patterns.

Using GREP command :

grep "pattern1\|pattern2" file.txt
grep -E "pattern1|pattern2" file.txt
grep -e pattern1 -e pattern2 file.txt
egrep "pattern1|pattern2" file.txt

Using AWK command :

awk ‘/pattern1|pattern2/‘ file.txt

Using SED command :

sed -e ‘/pattern1/b‘ -e ‘/pattern2/b‘ -e d file.txt



Grep ‘AND‘ Operator

Find and print all the lines in a file, that match multiple patterns.

Using GREP command :

grep -E ‘pattern1.*pattern2‘ file.txt # in that order
grep -E ‘pattern1.*pattern2|pattern2.*pattern1‘ file.txt # in any order
grep ‘pattern1‘ file.txt | grep ‘pattern2‘ # in any order

Using AWK command :

awk ‘/pattern1.*pattern2/‘ file.txt # in that order
awk ‘/pattern1/ && /pattern2/‘ file.txt # in any order

Using SED command :

sed ‘/pattern1.*pattern2/!d‘ file.txt # in that order
sed ‘/pattern1/!d; /pattern2/!d‘ file.txt # in any order



Grep ‘NOT‘ Operator

Find and print all the lines, that do not match a pattern.

Using GREP command :

grep -v ‘pattern1‘ file.txt

Using AWK command :

awk ‘!/pattern1/‘ file.txt

Using SED command :

sed -n ‘/pattern1/!p‘ file.txt

grep/awk/sed 或者 并且 否定,布布扣,bubuko.com

grep/awk/sed 或者 并且 否定

标签:style   strong   io   line   amp   re   

原文地址:http://www.cnblogs.com/emanlee/p/3833702.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!