Tech 2 Game - Ultimate Tech and Game Forum
Welcome to Tech 2 Game - Ultimate Tech and Game Forum Guest!    Register | Lost Password?




Tags: Finding, Text, Files, Unix,
Post Reply 
 
Thread Rating:
  • 1 Votes - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Share Post: Digg Delicious Reddit Face Book Stumbleupon
Finding Text in Files in Unix
11-14-2009, 07:03 PM
Post: #1
Finding Text in Files in Unix :
# grep (General Regular Expression Print)

$ grep options pattern files

grep searches the named files (or standard input if no files are named) for lines that match a given pattern. The default behaviour of grep is to print out the matching lines. For example:

$ grep hello *.txt

searches all text files in the current directory for lines containing "hello". Some of the more useful options that grep provides are:
-c (print a count of the number of lines that match), -i (ignore case), -v (print out the lines that don't match the pattern) and -n (printout the line number before printing the matching line). So

$ grep -vi hello *.txt

searches all text files in the current directory for lines that do not contain any form of the word hello (e.g. Hello, HELLO, or hELlO).

If you want to search all files in an entire directory tree for a particular pattern, you can combine grep with find using backward single quotes to pass the output from find into grep. So

$ grep hello `find . -name "*.txt" -print`

will search all text files in the directory tree rooted at the current directory for lines containing the word "hello".

The patterns that grep uses are actually a special type of pattern known as regular expressions. Just like arithemetic expressions, regular expressions are made up of basic subexpressions combined by operators.

The most fundamental expression is a regular expression that matches a single character. Most characters, including all letters and digits, are regular expressions that match themselves. Any other character with special meaning may be quoted by preceding it with a backslash (\). A list of characters enclosed by '[' and ']' matches any single character in that list; if the first character of the list is the caret `^', then it matches any character not in the list. A range of characters can be specified using a dash (-) between the first and last items in the list. So [0-9] matches any digit and [^a-z] matches any character that is not a digit.

The caret `^' and the dollar sign `$' are special characters that
match the beginning and end of a line respectively. The dot '.' matches any character. So

$ grep ^..[l-z]$ hello.txt

matches any line in hello.txt that contains a three character sequence that ends with a lowercase letter from l to z.

egrep (extended grep) is a variant of grep that supports more sophisticated regular expressions. Here two regular expressions may be joined by the operator `|'; the resulting regular expression matches any string matching either subexpression. Brackets '(' and ')' may be used for grouping regular expressions. In addition, a regular expression may be followed by one of several repetition operators:

`?' means the preceding item is optional (matched at most once).
`*' means the preceding item will be matched zero or more times.
`+' means the preceding item will be matched one or more times.
`{N}' means the preceding item is matched exactly N times.
`{N,}' means the preceding item is matched N or more times.
`{N,M}' means the preceding item is matched at least N times, but not more than M times.

For example, if egrep was given the regular expression

'(^[0-9]{1,5}[a-zA-Z ]+$)|none'

it would match any line that either:

* begins with a number up to five digits long, followed by a sequence of one or more letters or spaces, or
* contains the word none
Find all posts by this user
Sponsored by
Post Reply 


Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  For each of ur directories how to Check the disk usage in unix rin2 0 113 12-11-2009 08:20 PM
Last Post: rin2
  How to see the Disk usagein unix rin2 0 61 12-11-2009 08:11 PM
Last Post: rin2
  What is the "Top" command in unix rin2 0 79 12-11-2009 07:53 PM
Last Post: rin2
  Finding Files in Unix rin2 0 106 11-14-2009 07:08 PM
Last Post: rin2
  Handling Removable Media in Unix rin2 0 128 11-14-2009 06:56 PM
Last Post: rin2
  Unix File Compression and Backup rin2 1 97 11-10-2009 08:01 PM
Last Post: Sukanjan.K
  Find UNIX Directory Structure rin2 0 269 11-08-2009 07:11 PM
Last Post: rin2


[-]
Permissions Box
You cannot Post Threads.
You cannot Post Replies.
You cannot Post Attachments.
You cannot Edit Your Posts.
HTML is off
MyCode is on
Smilies is on
[img]-code is on