bash scripting - search a match in a string

Posted on 2004-11-18
Last Modified: 2008-01-09
I have a string on a variable, I want to search for two matches.. something like this:

var="Take the red pill, or the blue pill"

if [ "red pill" isin $var ]; <-- this I don't know
    echo "Somebody mentioned the red pill"
    echo "Somebody said something but not about the red pill"

I googled for "search string" and stuff but I get awk and expr funtions that I don't seem to understand (or indeed it isn't me and the result of my search aren't right). Maybe I'm looking wrong. :S

Thaaanks experts :)
Question by:mmartha
    LVL 6

    Accepted Solution

    Someone with better regexp skills will undoubtedly jump in here but here's a quicky:

    if echo $var | egrep -e '(^| )red( |$)'; then
       echo "Somebody mentioned the red pill"
       echo "Somebody said something but not about the red pill"

    We're looking for:

    beginning of the line or a space, followed by the letters r e d followed by either a space or end-of-line.

    Of course, punctuation will cause a problem after the word red in this example, so you could allow alter the regex slightly to allow non-character trailers after r e d:

    '(^| )red([^:alpha:]| |$)'

    Is this what you were looking for?
    LVL 48

    Assisted Solution

    if echo "$var" | grep -qw "red pill'
        echo "Somebody mentioned the red pill"
       echo "Somebody said something but not about the red pill"

    The -w flag says to search for whole words and the -q flag is for quiet (ie. no output)
    LVL 2

    Author Comment

    thank you both. :)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Suggested Solutions

    Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
    The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now