grep for multiple strings in a single line

39,081

Solution 1

To used alternation you need Extended Regexp:

grep -qE 'Added|Changed|Fixed|Deleted'

Or:

egrep -q 'Added|Changed|Fixed|Deleted'

Solution 2

Use grep -e option (multiple times) like this:

grep -e Added -e Changed -e Fixed -e Deleted

otherwise go to the regex route:

grep --regexp=Added|Changed|Fixed|Deleted

Solution 3

Remove the backslashes and use egrep I also recommend -i for case insensitive matching:

egrep -q -i "added|changed|fixed|deleted"
Share:
39,081
ramz
Author by

ramz

Updated on August 28, 2020

Comments

  • ramz
    ramz over 3 years

    I need to check if any of the strings "Added/Changed/Fixed/Deleted" are in a commit log message. I am writing an svn precommit hook, and the expected commit comment should have one of these 4 strings in the message.

    The code I am using is as below

    REPOS=$1
    TXN=$2
    
    SVN="/usr/bin/svn";
    SVNLOOK="/usr/bin/svnlook";
    
    $SVNLOOK log "$REPOS" -t "$TXN" | \
    grep "[a-zA-Z0-9]" > /dev/null
    
    GREP_STATUS=$?
    if [ $GREP_STATUS -ne 0 ]
    then
      "${ECHO}" "No Log comments present" >> "${LOG}"
       echo "Your commit has been blocked because you didn't give any log message" 1>&2
       echo "Please write a log message describing the purpose of your changes and" 1>&2
       echo "then try committing again. -- Thank you" 1>&2
    exit 1
    fi
    

    In the above code,

    $SVNLOOK log "$REPOS" -t "$TXN"
    

    will give me the commit message that the user has entered. Now I have to check for the presence of any of the strings "Added, Changed, Fixed, Deleted" in the message. That is,

    if (any of the above 4 strings are not present),
     exit 1
    

    I tried with

    $($SVNLOOK log -t "$TXN" "$REPOS" | grep -q "Added\|Changed\|Fixed\|Deleted"|)
    

    but it doesnt seem to be working.