Find the answer to your Linux question:
Results 1 to 4 of 4
I am working through the O'Reilly book "Classic Shell Scripting" by Robbins and Beebe, 2005, and have been trying a couple of the example patterns. One of them is to ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Aug 2010
    Posts
    9

    grep search patterns


    I am working through the O'Reilly book "Classic Shell Scripting" by Robbins and Beebe, 2005, and have been trying a couple of the example patterns.

    One of them is to match single- or double-quoted words using a back reference. The pattern is given as (in Section 3.2.2, on page 39):

    \(["']\).*\1

    (In case the above does not display very clearly, the square brackets contain a double quote and a single quote).

    The first problem is that grep expects a matching quote delimiter, which I solved by enclosing the single quote within double quotes and the double quote within single quotes. However, I find that it does not match.

    If I remove the parentheses and the back reference, it will match a line with quotes on it, but I cannot get it match with the back reference, i.e. text enclosed within a pair of similar quotes.

    Does anyone have any idea why it doesn't work with a back reference?

    I am using grep version 2.5.4 under Ubuntu 9.10.

    With thanks

  2. #2
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Slackware, {Free, Open, Net}BSD, Solaris
    Posts
    1,286
    Hi.

    First, note that the page is listing examples of REs, not necessarily their use in any particular program.

    You didn't say, but I suspect you are running this specific RE on a command line, perhaps with grep or sed.

    If so, then you are running into quoting issues with the shell. The shell does not really care about the placement of quotes, only that they match. The double quotes can be escaped within a double-quoted string, but single quotes cannot be escaped within a single-quoted string.

    So one way to avoid this problem is by not having the shell look at the RE. That could be done with a separate file containing a sed script. The sed commands are not scanned by the shell, so we will not need to worry about balanced quotes.

    However, we can also choose judicious quoting, and make allowances for the scan of the shell. We will choose double quotes, and then escape any enclosed double quote.

    Assume that we wish to simply eliminate all quoted duplicated word-strings, like:
    Code:
    "jmvalks any text whatsoever jmvalks"
    Here is a a script that uses both methods described above, with sed as the program that does the work, by deleting the matched strings so that you can see the action:
    Code:
    #!/usr/bin/env bash
    
    # @(#) s1	Demonstrate avoidance of quoting and proper quoting.
    
    pe() { for i;do printf "%s" "$i";done; printf "\n"; }
    pl() { pe;pe "-----" ;pe "$*"; }
    
    cat >data1 <<'EOF'
    1 This is a plain text file.
    2 This is a foo foo sentence.
    3 This is a "foo foo" sentence.
    4 This is a 'bar bar' sentence.
    5 This is a "baz baz' sentence.
    EOF
    
    pl " Contents of data file data1:"
    cat data1
    
    pl " Contents of file sed-script:"
    cat sed-script
    
    pl " Results from operating with sed-script:"
    sed -f sed-script data1
    
    pl " Results from operating with sed command, quoted:"
    sed "s/\([\"']\).*\1//" data1
    
    exit 0
    producing:
    Code:
    % ./s1
    
    -----
     Contents of data file data1:
    1 This is a plain text file.
    2 This is a foo foo sentence.
    3 This is a "foo foo" sentence.
    4 This is a 'bar bar' sentence.
    5 This is a "baz baz' sentence.
    
    -----
     Contents of file sed-script:
    s/\(["']\).*\1//
    
    -----
     Results from operating with sed-script:
    1 This is a plain text file.
    2 This is a foo foo sentence.
    3 This is a  sentence.
    4 This is a  sentence.
    5 This is a "baz baz' sentence.
    
    -----
     Results from operating with sed command, quoted:
    1 This is a plain text file.
    2 This is a foo foo sentence.
    3 This is a  sentence.
    4 This is a  sentence.
    5 This is a "baz baz' sentence.
    Note the extra \ (backslash) that escapes the double quote inside the brackets for the version that has the RE on the command line.

    Best wishes ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

  3. #3
    Just Joined!
    Join Date
    Aug 2010
    Posts
    9

    grep search patterns

    Hi drl

    Many thanks for your thorough response.

    Yes, I guessed that the book contains examples of patterns only. You are right - I am issuing grep from the command line on a file created in emacs. I can see I need to work my way through some more of this book and get a lot more practice with regular expressions.

    I will take the time to study your response carefully.

    Thanks once again,

    jmv

  4. #4
    Just Joined!
    Join Date
    Apr 2005
    Location
    Central Florida
    Posts
    12
    Note the extra \ (backslash)
    Thanks for the info.

Posting Permissions

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