Saturday, March 1, 2008

OR and AND in grep


OR

**Search "one" or "eleven" in the file "file.out"

$ cat file.out
1 one
11
eleven
2 two
3 three
13 thirteen
4 four
1
one
40 forty


Using grep:
$ grep "one\|eleven" file.out
1 one
11 eleven
1 one

Using AWK:
$ awk '/one|eleven/' file.out
1 one
11 eleven
1 one

Using SED:
$ sed -e '/one/b' -e '/eleven/b' -e d file.out
1 one
11 eleven
1 one


AND

$ cat experience.out
Good judgment comes from experience,
and often experience comes from bad judgment.
Experience is simply the name we give our mistakes.
Experience keeps a dear school, but fools will learn in no other.



**Search for lines containing pattern "comes" and "from" and "judgement" (in any order)

Using SED:
$ sed '/comes/!d; /from/!d; /judgment/!d' experience.out
Good judgment comes from experience,
and often experience comes from bad judgment.


**Search for lines containing pattern "comes" and "from" and "judgement" (in that order)

Using SED:
$ sed '/comes.*from.*judgment/!d' experience.out
and often experience comes from bad judgment.

Using AWK:
$ awk '/comes.*from.*judgment/' experience.out
and often experience comes from bad judgment.

No comments:

© Jadu Saikia www.UNIXCL.com