+ Reply to Thread
Results 1 to 5 of 5

Thread: grep: you're my best friend!

 
  1. #1
    Contributing User
    Join Date
    May 2011
    Posts
    166
    Rep Power
    200

    Default grep: you're my best friend!

    Hi there

    To understand the mood in which I'm writing these lines, take a couple of minutes to read the wonderful lyrics of this wonderful song: Queen - You're My Best Friend Lyrics

    This post is not directly related to translating, but anyone working with words in one way or another would find an intimate knowledge of grep most useful (there are versions for Windows too, for those of you that can't give up on it).

    I've known grep for a very long time now, and despite its shy manners, I can say one thing: it has never let me down. At the very start I only asked two kinds of questions:


    • "could you please show me all the lines where this sentence appears in such or such file?".
    • "could you please tell me all the files where this or that appears?"


    Code:
    pabloa$ grep "this sentence" such_or_such.file
    pabloa$ grep -l "this or that" *
    pabloa$ grep -lr "this or that" . > file_for_further_processing
    The flag "-l" is the way of telling grep we want only the name of the files, not the lines inside them. In the third line we are politely asking to do a recursive search in the current directory (that's the meaning of the dot), that is all files in this directory, and in all directories within (and their directories, etc).

    With just these ways of interacting with grep all kind of things can be done. Redirecting the output into a file is particularly useful, especially in the last two cases, where one gets a list of files for further processing, as it is done in the third example.

    I was planning to describe all the abilities of this trusted friend today, but I've realised that it will take me all day to do this, so I'll end this post here, and add to it later on.

    My dear friend grep, "I really love the things that you dooooooo .... "!

    Cheers.
    P.

  2. #2
    Contributing User
    Join Date
    May 2011
    Posts
    166
    Rep Power
    200

    Default Re: grep: you're my best friend!

    Hello all

    As I mentioned in my previous post, at the beginning I was a bit shy myself, and asked just a few kind of questions. But when the confidence between us grew stronger, I started to explore other capabilities. Of course, an invaluable help was using the "man page" for this program (type "man grep" into a terminal).

    The behaviour of "grep" can be affected variously using extra flags, like the ones I've used earlier, "-l" for listing just the name of the files or "-r" to make it recursive. The flags can go one by one, or all of them following a single dash, so "-lr" is equivalent to "-l -r". One flag which is very nice is "--color=auto", which highlights the matching bit within the lines displayed. Actually, this is considered so useful that Ubuntu (and maybe Debian too) come with an alias ready made: alias grep='grep --color=auto'

    One set of flags quite useful in some situations has an easy mnemonic associated. I think of them as ABC and they stand for "A"fter, "B"efore, and "C"ontext. We put a number following them, and then "grep" will display as many lines after, before or surrounding the match when it finds one. The following code is an example of these things.

    Code:
    pabloa$ grep Line lines.txt                                                                                                       
    Line one
    Line two
    Line three
    Line four
    pabloa$ grep -A1 two lines.txt                                                                                                    
    Line two
    Line three
    pabloa$ grep -B1 two lines.txt                                                                                                    
    Line one
    Line two
    pabloa$ grep -C1 two lines.txt                                                                                                    
    Line one
    Line two
    Line three
    Cheers.
    P.

  3. #3
    Contributing User
    Join Date
    May 2011
    Posts
    166
    Rep Power
    200

    Default Re: grep: you're my best friend!

    Hi

    Other flags which I find often useful are "-c", "-i" and "-v". "grep -c" doesn't show the lines matching, but instead just counts them, and prints this number. "grep -i" ignores the case of the expression, so upper and lower case don't count as different letters. The "-v" flag displays the lines which don't match. An example showing the behaviour of these flags:

    Code:
    pabloa$ grep -c Line lines.txt
    4
    pabloa$ grep -ci Two lines.txt
    1 
    pabloa$ grep -civ Two lines.txt
    3 
    pabloa$ grep -cv Two lines.txt
    4
    The other day I wanted to find out whether there was a specific html tag (sup) in a bunch of XML files. But it was ok if the tag appeared inside a field with an attribute 'value="html"'. Then I used grep twice, first to display lines containing the tag, and piped this output into another grep, so it displayed only the lines without this attribute:

    Code:
    grep -i "<sup>" *.xml | grep -v 'value="html"'
    I used the "-i" flag just in case the tag appeared in capitals and put the expression in quotation marks so the '<' and '>' are not picked up by the shell (they are redirection operators). Finally, in the second expression I used the single quotation marks to wrap up the expression, so the double quotation marks are considered part of the string to be matched.

    Cheers.
    P.
    Last edited by pabloa; 09-30-2011 at 10:14 AM.

  4. #4
    Contributing User
    Join Date
    May 2011
    Posts
    166
    Rep Power
    200

    Default Re: grep: you're my best friend!

    Hi there

    All about "grep" is nice, so saying something like "one of the most useful features of grep ..." would be inaccurate. However, there is one flag that opens up a new dimension on the way we can use "grep". "grep -o" displays only the part of the text which matches the search string.

    Code:
    pabloa$ echo 'Hello, hello, hello!' > test
    pabloa$ grep -o hello test
    hello
    hello
    pabloa$ grep -oi hello test
    Hello
    hello
    hello
    This feature combined with regular expressions is extremely powerful. We will have a look at regular expressions next time. In the meantime, the "-o" flag can be used to count the number of occurrences of a specific word in a document (I don't know of another way of doing it, actually). Unfortunately the combination of the "-o" flag with the "-c" flag doesn't work as expected, so we need to do it in two steps, like so:

    Code:
    pabloa$ grep -oi hello test | wc -l
    3
    Here "wc" is a UNIX command for counting (characters, words, lines). The "-l" (by the way, this is a lowercase L, not the number 1!) flag says to report only the number of lines (as you'd expect, it's got corresponding "-c" and "-w" flags for characters and words).

    Cheers.
    P.
    Last edited by pabloa; 10-07-2011 at 09:56 AM.

  5. #5
    Moderator
    Join Date
    Mar 2012
    Age
    37
    Posts
    989
    Rep Power
    1018

    Default Re: grep: you're my best friend!

    Grep is great! "-i" flag slows down the search, however grep is the fastest search in files method that I know.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Hey! I am your latin friend.
    By Genesis1997 in forum Greetings to Newbies
    Replies: 4
    Last Post: 11-23-2017, 07:04 PM
  2. Always your friend
    By chewytoy in forum General English to Spanish Translation
    Replies: 11
    Last Post: 09-11-2015, 10:11 AM
  3. Man's Best Friend
    By chris.r in forum Jokes
    Replies: 1
    Last Post: 08-10-2012, 12:49 PM
  4. introduce a boy friend to her
    By loveisall in forum General English to Spanish Translation
    Replies: 1
    Last Post: 09-27-2010, 06:29 PM
  5. My friend
    By !!!Sonia in forum Jokes
    Replies: 0
    Last Post: 06-09-2010, 05:34 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •