TUCoPS :: Unix :: General :: grep.txt



 grep
 
 All I will say is that it depends on your definition of 'hacking'. The
 following will increase your power in working with a Unix based system.
 
 Grep is from a family of commands: grep, egrep, and fgrep. They all search 
 the named input files (or standard input if no files are named) for lines
 containing a match to the given pattern. Each of the grep commands are
 basically the same, the only real difference is that egrep uses a slightly
 different syntax for its pattern matching, whereas fgrep uses fixed
 strings. There is also another member to the grep family, and that is
 zgrep. Zgrep is used to search compressed files and is invoked the same way
 as grep. In this text I will be detailing grep, and I feel that it is easier
 to learn and understand by seeing examples, so I hope to provide alot of
 usefull ones :)
 
 For examples I will be using a list of Bauhaus songs. Just cut and paste
 the following to a file and name it bauhaus.txt
 
 ----cut here----
 The passion of lovers
 Bela Lugosi's dead
 She's in parties
 Ziggy stardust
 Wasp
 Hope
 King Volcano
 The sanity assassin
 Terror couple hill colonel
 ----cut here----
 
 The syntax for grep is as follows:
 
 grep [options] pattern [file]
 
 Usefull options:
 
 -c counts number of matching lines
 -i ignore caps
 -n includes the line number
 -s suppress error messages
 -v lines NOT mattching the pattern
 
 A simple example:
 
 #grep -c Z bauhaus.txt
 1
 
 The above statement counts how many lines contain the letter Z (case
 sensitive) and displays the result. If I typed the following, it will
 display the lines:
 
 #grep Z bauhaus.txt
 Ziggy stardust
 With the added option -v, lines NOT matching will be counted:
 
 #grep -vc Z bauhaus.txt
 8
 
 and displayed:
 
 #grep -v Z bauhaus.txt
 The passion of lovers
 Bela Lugosi's dead
 She's in parties
 Wasp
 Hope
 King Volcano
 The sanity assassin
 Terror couple hill colonel
 
 displayed and line numbered:
 
 #grep -vn Z bauhaus.txt
 1:The passion of lovers
 2:Bela Lugosi's dead
 3:She's in parties
 5:Wasp
 6:Hope
 7:King Volcano
 8:The sanity assassin
 9:Terror couple hill colonel
 
 Options can be mixed like any other command.
 
 Regular expressions are used to provide grep with expressions whcih set
 locations of patterns and ranges of characters (all regular expressions
 must be quoted). The hat (^) means start of line, and the dollar ($) means
 the end of the line.
 
 To display lines ending with 's'
 
 #grep 's$' bauhaus.txt
 The passion of lovers
 She's in parties
 
 To display lines not ending in 's' and also number them:
 
 #grep -vn 's$' bauhaus.txt
 2:Bela Lugosi's dead
 4:Ziggy stardust
 5:Wasp
 6:Hope
 7:King Volcano
 8:The sanity assassin
 9:Terror couple hill colonel
 
 The full stop (.) represents a single character wildcard. eg the following
 will display any line that has any character before the 'e':
 
 #grep '.e' bauhaus.txt
 The passion of lovers
 Bela Lugosi's dead
 She's in parties
 Hope
 The sanity assassin
 Terror couple hill colonel
 
 More examples:
 
 #grep -i '.L' bauhaus.txt - any case, with any character/s before 'L'
 #grep 'V.....o' bauhaus.txt - V, any 7 characters, then o
 
 The square brackets ([]) specify any one of the characters enclosed. eg, to
 display the lines beginning with 'T', 'W' or 'Z':
 
 #grep '^[TWZ]' bauhaus.txt
 The passion of lovers
 Ziggy stardust
 Wasp
 The sanity assassin
 Terror couple hill colonel
 
 For a range of characters, use a hyphen:
 
 #grep '^[A-J] bauhaus.txt
 Bela Lugosi's dead
 Hope
 
 More examples:
 
 #grep '^[A-Za-z0-9] bauhaus.txt - all letters / numbers
 #grep '[0-9]$' bauhaus.txt - ending with a number
 #grep -v '[a-m]$' bauhaus.txt - lines that dont end with a-m
 
 When the hat (^) is used in the square brackets it means 'not'. eg the 
 following will show lines not beginning with 'A' to 'G':
 
 #grep '^[^A-G]' bauhaus.txt
 The passion of lovers
 She's in parties
 Ziggy stardust
 Wasp
 King Volcano
 The sanity assassin
 Terror couple hill colonel
 
 A wildcard can also be used (*). eg the following will display lines
 beginning with 'T' and ending with 's'
 
 #grep '^T.*s$' bauhaus.txt
 The passion of lovers
 The following will display lines beginning with 'M' to 'Z' and ending
 in 's' or 't':
 
 #grep '^[M-Z].*[st]$' bauhaus.txt
 The passion of lovers
 She's in parties
 Ziggy stardust
 The above was just an introduction to grep, there is a myrid of other 
 statements, redirections (>>) and piping (|) that can be done using it.
 From the above, you should now be able to do alot of sorting, extracting,
 and removing from logs ALOT easier now ;) 
 (grep -v <ip> /var/log/messages >> /var/log/messages.2)

TUCoPS is optimized to look best in Firefox® on a widescreen monitor (1440x900 or better).
Site design & layout copyright © 1986-2025 AOH

AltStyle によって変換されたページ (->オリジナル) /