Cooked Tech

Linux

How to Use Grep

July 31, 2012 by admin in Linux

GREP is a command that is mostly used by UNIX users. It is helpful when searching for patterns of characters in a file or standard input. By using GREP, a search is carried out to match the given pattern and then the results are printed in standard output via the screen. To use the GREP command, a regular expression or a simple string called the pattern is typed and the search is carried out through one or multiple files. Expressions or patterns are enclosed within quotation marks, double or single, in order for the search to take place. When you type a GREP command, it searches the given file for lines that match the typed words. It may sound difficult if you are new to GREP but once you learn how to use it and practice, you will know how to utilize the GREP command. To help you get familiar with the GREP command, here are some examples you start out with:

Required Tools

  • Computer (UNIX or LINUX)

Instructions

  1. To search for a given string in just one file:
    • Type grep “œliteral string” filename.
  2. To check for a string in more than one file:
    • Type grep “œstring” FILE_PATTERN”.
  3. To perform case insensitive search using grep-i:
    • Type grep “” I “œstring” FILE
      1. Note: This searches for the string that you typed in insensitive case, therefore, it will match all the words, both uppercase and lowercase.
  4. To match regular expressions in files. Lets say you want to search for all patterns that begin with lines and ends with empty having anything between.
    • Type grep “œlines.*empty” filename.
  5. To search for complete words while preventing it from matching the substrings use the
    “”w feature. If you use just a normal search, all the lines will be shown. For example, if you type the command to searc for “œis” without using the “”w feature you will end up with results showing all words with “œis” such as “œthis” and “œhis”. To search for the word “œis”:

    • Type: grep “” IW “œis” demo file.
  6. To search for lines before, after or around the match using grep “”A, -B and “”C. This is used mainly when using grep to search a huge file and you want to see additional lines along with the match.
    • To display line after the match:
      1. Type grep “”A “œstring” FILENAME.
    • To display lines before the match:
      1. Type grep “”B “œstring” FILENAME.
    • To display lines around the match:
      1. Type grep “”C 2 “œExample” demo text.
  7. To search all files under a current directory along with its sub-directory “”r is the option to be used. To search for the string “œsmallville” in all files in the current directory and all its sub-directory:
    Type grep “”r “œsmallville’.

Tips and warning

  • Do not use single quotations if the expression you are typing already contains a single quotation. For example, “œgrep “˜Sam O’Neal’ customer.lst”. The command will fail. Instead, “œgrep “œSam O’Neal” customer.lst” is the correct way to type patterns that already contain single quotation mark.

Tagged , , ,

Related Posts

Popular Articles
Get Our Articles to Your Inbox
Powered by
Easy Automatic Newsletter Lite v2.7.2
Recent Comments
    No recent comment found.
Advertising
Advertising
About Cooked Tech

Cooked Tech is a website dedicated to computer technologies and issues that might come up around it. The site features tips and tricks on various topics relating to computers and internet, thereby helping people get things done easily. Keep yourself updated by subscribing so you don't miss any tip!

Random Posts
  • The Role of DHCP Agents
  • Protecting Your Email Account from Hackers
  • What is a Data Center?
  • Web Hosting Coupon
  • What is a Motherboard?
  • What are Quantum Computers?
Follow Cooked Tech