web analytics

How to use Grep – A Unix Based Command-Line Search Tool

Grep (global / regular expression / print) is a unix based command-line text search utility. It was first developed way back in 1973 (yes they had computers back then) and to this day it remains an excellent tool for printing data to the screen.

In the past i used Grep for pulling data out of huge text files. A good example of its use can be seen when you dealing with a lot of data in one file and you want to only display specific information. A few years back i was harvesting emails to send spam (yup.. i was one of those annoying spammers – business is business). I used Google and Yahoo search strings to pull data from commercial servers that was unintentionally listed on search engines. I managed to find several text files containing around half a million emails. I wanted to pull out for example only the addresses served by hotmail. Using the standard find app and cut and paste was a huge nightmare in text files. Using Grep i was able to have my computer do the tedious work for me instead and i was able to process those lists in days rather then months. Grep can also be used for piping specific data from command-line applications. For example piping specific data from hacking tools to the screen to save us from searching through log files for the information we are trying to find.

Below are some nice examples to get you started with using Grep

grep apple fruitlist.txt

The above code prints all the lines from the file “fruitlist.txt” containing the sequence of characters specified (in this case “apple”). This also means that lines containing the word “pineapple” for example are also printed because the line contains the specified sequence of characters as well as additional characters. By default Grep searches for the data as case sensitive so in this example it won’t print the sequence “Apple” (not the capital A) to the command line. The only case where Grep will print the sequence is if if you have a word such as “Aapple” or “appleA”.

grep -i apple fruitlist.txt

In the above example the “-i” request kills the default search for case sensitive characters in the request. What this means is that if the word “Apple” (with a capital A) is written in the file fruitlist.txt then it will print it to the screen as well as the word “apple” (without a capital a).

grep -w apple fruitlist.txt

In this example the “-w- request insists that Grep searches for the exact phrase. Words in the fruitlist.txt file such as apples or pineapples won’t be printed to the screen. Only lines containing the exact phrase “apple” will be printed to the screen. This does not apply to cases as seen under the “-i” example.

grep -w -i apple fruitlist.txt

Using what we learned in the other examples the above code will search for the word “apple” with either capital or non capital letters. It will also only search for those words which match the sequence of characters exactly meaning only “apple” or “Apple” and not “pineapple” or “Pineapple”.

grep -v apple fruitlist.txt

The above code reverses the sense of the match and prints all lines that do not contain “apple”. So if we have the words seen below in our fruitlist.txt file, grep won’t display any word containing the characters “apple” but it will display all the other words in the text file. This means the words “Apple” and “Pineapple” won’t be displayed.


grep -c "Error" logfile.txt

Another way we can use Grep is to count words. Lets say for the above example you have a log file and you want to count the amount of errors per line. Using “-c” we can use the count feature which will have grep search the file “logfile.txt” and print the amount of times it sees the word “error” in the document.

grep AppleTalk /etc/services</code>

The above sequence will print will request for Grep to look for AppleTalk in the services file located in the directory /etc on Mac OSX (change the sequence to correspond with your operating system like Linux or for Windows using Grep32 or DOS Grep) The command displays each line that contains the string requested. On Mac OSX it prints the following data to the screen:

echo              4/ddp    #AppleTalk Echo Protocol
at-rtmp         201/udp     # AppleTalk Routing Maintenance       
at-rtmp         201/tcp     # AppleTalk Routing Maintenance       
at-nbp          202/udp     # AppleTalk Name Binding              
at-nbp          202/tcp     # AppleTalk Name Binding              
at-3            203/udp     # AppleTalk Unused                    
at-3            203/tcp     # AppleTalk Unused                    
at-echo         204/udp     # AppleTalk Echo                      
at-echo         204/tcp     # AppleTalk Echo                      
at-5            205/udp     # AppleTalk Unused                    
at-5            205/tcp     # AppleTalk Unused                    
at-zis          206/udp     # AppleTalk Zone Information          
at-zis          206/tcp     # AppleTalk Zone Information          
at-7            207/udp     # AppleTalk Unused                    
at-7            207/tcp     # AppleTalk Unused                    
at-8            208/udp     # AppleTalk Unused                    
at-8            208/tcp     # AppleTalk Unused

So now that you have the basics of grep down i suggest you play around in it and use some of the above strings to search for data on your own computer. In some future tutorials i will be using grep for piping data from scripts and log files so if you plan to follow me and engulf yourself in a world of hacking fun then read over this article again and try to get familiar with Grep. If you don’t use it along with my tutorials then I’m sure some time in the future it will become quite a handy tool for you when your trying to search through a large amount of files. It beats downloading spyware infested shareware that does the same job with a crappy GUI.

Tags: , , , , , , , , , , , ,

31 Responses to “How to use Grep – A Unix Based Command-Line Search Tool”

  1. best animal clipper April 11, 2013 at 2:40 am #

    Hi would you mind letting me know which web host
    you’re working with? I’ve loaded your blog in 2 completely different internet browsers and I have
    to say this blog loads a lot quicker then others.
    Can you recommend a good hosting company
    at a fair price? Kudos, I appreciate it! Please also excuse if my english is not good.

  2. atypical reflux symptoms. June 5, 2013 at 11:54 pm #

    Greetings! Very useful advice within this post!
    It is the little changes that make the greatest changes.
    Thanks for sharing!

  3. Garcinia Cambogia Trim Review July 15, 2013 at 11:30 pm #

    Ahaa, its nice conversation about this post at this place at this blog, I have read all that, so now me also commenting here.

  4. 60 binary options July 16, 2013 at 11:07 am #

    Sweet blog! I found it while surfing around on Yahoo News.

    Do you have any suggestions on how to get listed in Yahoo News?
    I’ve been trying for a while but I never seem to get there! Thank you

  5. making money with binary options trading July 21, 2013 at 8:42 am #

    Does your site have a contact page? I’m having a tough time locating it but, I’d like to
    shoot you an email. I’ve got some ideas for your blog you might be interested in hearing. Either way, great site and I look forward to seeing it develop over time.

  6. cambogia trim dr oz February 12, 2014 at 10:40 am #

    Hi there tto every one, as I am actually keen of readingg this webpage’s ppost to be updated regularly.
    It includes pleasant stuff.

    Also visit mmy webloig :: cambogia trim dr oz

  7. commercial condominiums in nyc March 12, 2014 at 11:06 am #

    Spot on with this write-up, I truly think this site needs a lot more attention.
    I’ll probably be returning to read through more, thanks
    for the advice!

  8. Super Mario Birthday Invitations June 18, 2014 at 3:01 am #

    Leaders also make commitments and establish expectations
    very early in a new project. The developers have mapped both the Wii remote and Nunchuk into the action of catching an interception, so you have to simulate
    a catch by raising both at the same time. * Breakfast, lunch & dinner every day
    (Vegetarian offered). This is because such an application is powerful enough to cover most of your paper work.
    People want to do business with those that they believe, like and trust.

    Thus boot camps are the best solution for your weight requirements.
    If you are not so self-disciplined, learning it from the fitness
    boot camp training will do you good. They will visit a
    turtle hatchery, explore a salt marsh, dissect a
    fish and more. So, depending on what you are bringing with you, you will need to plan your meals accordingly.

    My daughter plays hard at day camp and spends long
    hours in the heat.

    Feel free to surf to my blog post – Super Mario Birthday Invitations

  9. garcinia cambogia supplements reviews June 26, 2014 at 11:32 am #

    The popular health expert told his audience that the fruit
    is a “dual action fat-burner” that can double or triple a user’s weight loss.

    insulin is a hormone produced by the pancreas, which facilitates the uptake of glucose by the liver and muscles
    to convert it to glycogen and energy. 10 every morning for 3 to 4 months
    can help with weight loss for obese people. As it is always good to seek for
    reviews before starting on health related products, you
    need to read or hear the reviews about Nutra – Slim also, before
    trying it out. According to reports, Garcinia Cambogia contains Hydroxycitric acid, HCA.
    Fat burners are one of the oldest and best known categories of weight loss aids.

    But if you want to lose pounds the right way, you
    should avoid these types of diets. Obesity can be measured
    through their body mass index (BMI) or your height and weight.

    You can utilize this cheap complement and improve complete wellness free of problems.
    This fruit is pale yellow to green in color and looks like a small pumpkin and is sour in taste.

    Feel free to visit my page: garcinia cambogia supplements reviews

  10. tankless water heater butterfly world pompano beach July 3, 2014 at 7:41 pm #

    Replacing the wax ring is a lot less difficult and expensive than replacing the toilet, but if you have wanted a new
    commode, this is your chance. Perhaps each toilet flush is becoming a
    little slower, or the tub isn’t draining
    after a shower, or an ominous noise is emanating from the sink.
    Sometimes whenever a Berkeley plumbing predicament happens, people make an effort
    to fix the plumbing Berkeley problems with no help.

    My web site – tankless water heater butterfly world pompano beach

  11. tankless water heater pompano beach florida July 27, 2014 at 5:59 am #

    The combined elimination of emotional trauma and the nutritional support is an incredibly powerful package that promotes health and well being faster than any other method currently available for treating arthritis.
    Flat paint is the best choice for hiding rough
    spots and surface imperfections, which makes it
    perfect for ceilings and walls. When you pull the toilet handle, the flapper rises and allows the water in the
    tank to empty into the toilet.

    Feel free to surf to my web-site: tankless water heater pompano beach florida

  12. repair faucet pompano beach seafood festival 2014 July 28, 2014 at 5:54 am #

    How many ways are there of going green in beauty schools.
    Keeping your home plumbing running smoothly is not a difficult job.
    Do not just ask any plumber to come over and do some immediate repairs.

    Feel free to surf to my website: repair faucet pompano beach seafood festival 2014

  13. plus size bandage dress August 8, 2014 at 6:57 am #

    These Maude Mustard Flats embody everything I love: bow-tie detailing, cute patterns, vegan leather, and not to mention, it’s honey yellow.
    Once again this course is conducted via email at one email per week over a
    period of eight weeks. Trends in glasses may seem to change more often than your prescription.

    my web-site … plus size bandage dress

  14. pelvic floor exercises men in spanish August 8, 2014 at 8:55 am #

    The surgeon will recommend the style that is medically proper for your condition. Women are especially susceptible
    to a leaky bladder after. So, Long Yoga Pants are highly flexible meant for various poses and exercises.

    my webpage: pelvic floor exercises men in spanish

  15. clogged toilet city of pompano beach August 8, 2014 at 11:27 am #

    Not to mention a job in the trades is much more interesting than filling the coffee maker at the 7-11.
    ” I asked my friend who’s got a busy family and a full-time people-oriented job.
    This article looks at the importance of choosing the right plumbing courses.

    Feel free to surf to my web page :: clogged toilet city of pompano beach

  16. Popular gym most expensive clothing brands August 9, 2014 at 1:25 pm #

    A lot of these symptoms also mirror another deadly
    virus – the Ebola virus. Tantra teaches that lovemaking between a man and woman, when entered into with Awareness, is a gateway to both sexual and spiritual ecstasy.
    They key is finding the one that will relieve your migraine.

    Feel free to surf to my site :: Popular gym most expensive clothing brands

  17. spray tanning booth August 11, 2014 at 7:01 am #

    You can try several things to help speed up the process and maybe even lighten the tan up a little bit.
    It is important to do any waxing at least twenty four hours or
    so in advance of applying any tan (fortunately, you will likely know about any big days or nights
    in advance so you will be able to do so) as not only will waxing afterwards remove the tan you have applied, waxing can also lead to dark dots on the skin which can collect the tan in the follicles: needless to say.
    Of course, a fresh coconut cake is always a special treat
    at Christmastime, just as using coconut in the favorite holiday
    fruit salad of the south, Ambrosia.

    Feel free to surf to my site spray tanning booth

  18. Rene August 14, 2014 at 12:05 pm #

    Nice post. I was checking continuously this blog and
    I am impressed! Extremely helpful information specially the closing phase :) I maintain such information a lot.
    I was looking for this particular info for a very long time.
    Thanks and god luck.

  19. cockpit refrigerator wakeboard tower for sea ray August 19, 2014 at 6:23 pm #

    Yet he chose to ask me so many questions about how and why I do things,
    because he could see the profound insight I had built up due to going my own way with my own research and testing
    over the past 7 years. Be sure to go with fresh bait, and stay away from the baits that are based on chicken or cat
    food. Freshwater fish can be placed in the live well on the boat, or
    on a stringer to keep it alive until you are able to clean it.

    My website :: cockpit refrigerator wakeboard tower for sea ray

  20. fix ferguson plumbing pompano beach August 23, 2014 at 5:22 pm #

    In the final analysis, a particularly good way so you can get a reliable price estimate for your kitchen remodel
    San Diego project is to clearly specify the details discussed above and then obtain 3
    different bids from legitimate general contractors.
    When the bathroom walls are crack or have been damaged the sink, the
    bathroom racks are no longer helpful it is time to think about bathroom remodeling.
    Investment in flooring can be really useful as it would enhance the
    lifespan of your floor.

    Look at my web blog: fix ferguson plumbing pompano beach

  21. natural tanning lotion August 24, 2014 at 9:36 am #

    With just few dollars, you can achieve a golden-bronze tan and also manage to look gorgeous and hot.
    - Oxybenzone – carcinogen and contributor to
    vascular disease, may affect the brain and nervous system in animals.
    Actually, once you get used to working with these tools, you’ll probably wonder how
    you ever lived without them.

    My blog … natural tanning lotion

  22. cheap storage units milwaukee Wi August 29, 2014 at 6:14 pm #

    Customers handle their own anti-freeze, flsh bulbs, and lighter fluid.

    Compare free instant online moving quotes and pick the best moving company based
    on services & price. You should ask relatives, colleagues, friends, neighbors or anyone in your surroundings.

    Feel free to visit my web page – cheap storage units milwaukee Wi

  23. weight loss surgery September 2, 2014 at 11:19 am #

    The HCG hormone stimulates the hypothalamus gland, which regulates your metabolism.
    The target for the five hundred calories is high-protein foods.
    You can consult your neighbors and family members for references.

    Feel free to visit my web page … weight loss surgery

  24. Weight loss hcg atlanta September 4, 2014 at 2:54 pm #

    It is very common to experience 2 or 3 pounds of weight loss after
    the first day. These supplements, accompanied by some determination and
    discipline, will truly make the difference in your life that you are looking to gain. Crazy, unchecked, and typically unplanned eating occurs during evening
    for numerous individuals struggling to lose fat.

    My website … Weight loss hcg atlanta

  25. weight loss hcg charlotte nc September 4, 2014 at 3:15 pm #

    The exact same low sodium reduced fat diet plan philosophy is abided by.
    Failure to Diagnose an Ectopic Pregnancy – Is This Medical Negligence.
    s better not to test instead of wasting your health insurance and money on these fake, choose nothing diets.

    Look at my webpage … weight loss hcg charlotte nc

  26. social media marketing definition September 10, 2014 at 9:30 am #

    Make sure the content is grammatically perfect, has a simple and easy to understand style, entices the
    reader, and remains grounded in facts instead of hyperbole.
    In addition to this tactic, a good SEO business and its professionals give individualistic attention to each and every project.

    SEO (search engine optimization) is a term that is freely bandied about by people who like to make the art of internet marketing seem mysterious.

    My web-site: social media marketing definition

  27. movers in atlanta georgia September 14, 2014 at 6:28 pm #

    Two weeks ago our employer announced that we would be moving our office to a new location;
    this was great news considering our company had grown in number quite drastically over the last year.
    Compile Local Menus – We all have our favorite local restaurants that took us months to
    find and fall in love with. The proficient companies of movers
    and packers in Ghaziabad are always ready to
    offer all essential resettlement services at very reasonable cost.
    Shifting vehicles to far away destinations is
    also very hectic and risk oriented task. The Nissan Altima is a
    good choice among midsize cars. The Movers are also the right
    choice for expats who are ordered a deputation and will soon be
    moving this beautiful city called Los Angeles.
    When you are in search of a new work place for you company it
    is important to view a couple of different office spaces so that you have lots of selection. Ceiling bags in conjunction with storage boxes will help you to
    store as much toys as possible. The World is
    a small place now and they can keep in touch with old friends should they choose to.
    If this does not fix the problem, you may consider replacing the garage door
    opener with a new unit.

    Look at my blog :: movers in atlanta georgia

  28. interstate moving companies rating September 15, 2014 at 3:57 am #

    Envision him happy and fulfilled experiencing with his team a victory this Sunday.
    The Illinois Movers will give you with an end-to-end solution at a
    cheap basis. Getty Center is renowned for its architecture, gardens, and views overlooking Los Angeles.
    Safe-keeping facilities are yet another wonderful advancement in this day and
    age. It is important for the crew to be aware of the handling procedures
    when it comes to high tech gadgets and furniture.
    The Movers are also the right choice for expats who are ordered a deputation and will soon be moving this beautiful city called Los Angeles.
    Data storage is expensive on such a large scale and has a direct relationship to costs
    associated with risk management, especially for companies that are exposed to litigation or government regulation. Our yarmulkes and Kippah Los Angeles show the distinguishing stamp of quality, and a half-century of practice and proficiency.
    Implement document management software system in your home, office and storage today to
    assist you with de-cluttering not only your paper filing system, but any item that you can put
    a number onto to get organized and be more productive.
    If this does not fix the problem, you may consider replacing the garage
    door opener with a new unit.

    my site :: interstate moving companies rating

  29. need for speed pro Street September 20, 2014 at 8:05 am #

    But now, with the prevalence of social media accounts, caretakers must now tackle the
    hurdle that is their loved one’s online presence.
    This checklist will refer to implementing optimizations in a Word – Press Blog.
    There are many different features like your own profile,
    blogging updates, Street Talk, the ability to comment and like Street Articles Facebook page,
    Street Articles Google + page, Street Articles Twitter Page, and the ability to interact with the owners (not to a great extent,
    they are quite busy).

    Feel free to visit my webpage: need for speed pro Street

  30. Weight Loss Pills September 20, 2014 at 2:26 pm #

    If you or someone you know thinks they may be pregnant or is
    experiencing any symptoms of pregnancy and acquires a negative test result, its best to test again in around a
    week. No one has to live with the discomfort of nasal congestion. Could the HCG shot be the secret to losing weight and
    keeping it off.

    my web blog; Weight Loss Pills

  31. www.facebook.com/supercrossanaheim2015livestreaming January 13, 2015 at 6:07 am #

    January 8, 2000: Jeremy Mc – Grath.
    Noticed I was being tempted bby an attitude of victimization to indulge in a long term
    pity- party and I chos too be the master of my attitude.
    Once you’ve went down the whole list, you go back to the tops,
    and start over again.

Leave a Reply