Thursday, December 31, 2009

GREP IN LINUX

GREP

Features:
1. The ability to parse lines based on text and/or RegExes

2. Post-processor

3. Searches case-sensitively, by default

4. Searches for the text anywhere on the line

EXAMPLE:

1. grep 'linux' grep1.txt

2. grep -i 'linux' grep1.txt - case-insensitive search

3. grep '^linux' grep1.txt - uses '^' anchor to anchor searches at the beginning of lines

4. grep -i '^linux' grep1.txt

5. grep -i 'linux$' grep1.txt - uses '$' anchor to anchor searches at the end of lines

Note: Anchors are RegEx characters (meta-characters). They're used to match at the beginning and end of lines

6. grep '[0-9]' grep1.txt - returns lines containing at least 1 number

7. grep '[a-z]' grep1.txt

8. rpm -qa | grep grep - searches the package database for programs named 'grep'

9. rpm -qa | grep -i xorg | wc -l - returns the number of pacakges with 'xorg' in their names

10. grep sshd messages

11. grep -v sshd messages - performs and inverted search (all but 'sshd' entries will be returned)

12. grep -v sshd messages | grep -v gconfd

13. grep -C 2 sshd messages - returns 2 lines, above and below matching line

Note:
Most, if not all, Linux programs log linearly, which means one line after another, from the earliest to the current
Use single or double quotes to specify RegExes
Also, execute 'grep' using 'egrep' when RegExes are being use

No comments:

Post a Comment