Solved

bash scripting - Using pipe character in grep in scripts

Posted on 2006-07-19
3
1,291 Views
Last Modified: 2013-12-26
Hello,

I am trying to write a little shell wrapper script for a few commonly used SVN commands.  The problem is that I have a regex in my final executed command that contains the pipe or "|" character as an OR in my regex.  I have tried MANY forms of quoting, backslashign, etc to get this to parse properly when run but the best I have gotten so far is shown below and when it is run I get an error because the shell tries to recognize the "|" in the regex as a further pipe.  Im sure that the solution is simple but My best attempts as hacking and searching for a solution have been for naught.  The command I am trying to get the script to run in the end is something like:

svn log file:///var/svn/repos/trunk | grep -P '^r\d+\s|^FILE|^MERGE|^-------'

which runs fine at the command line.
================= Begin Inserted Code =================
#! /bin/sh

CMDROOT=$1
shift;

bindir='/usr/bin'
cmd="${bindir}/svn $CMDROOT"

help="\
mysvn is a wrapper which pipes some SVN commands through grep to simplify the output.

Usage:
  mysvn log [-D][-r VALID_REVISION_STRING] VALID_REPOSITORY_PATH
  mysvn diff [-D][-r VALID_REVISION_STRING] VALID_REPOSITORY_PATH
 
Common options:
  -D   Show More Detail
  -r   Specify which Revision(s) to examine, defaults to HEAD
"
if [ $CMDROOT = 'log' ]; then
      grepString="grep -P '^r\\d+\\s|^FILE|^MERGE|^-------'"
else
      grepString="grep '^Index:'"
fi

while [ "$#" -gt 1 ]
do
    case "$1" in
      -h|--help|-\?)
         echo "$help"
         exit 0
         ;;
      -D|-d)
            if [ $CMDROOT = 'log' ]; then
                  grepString="grep -P '^r\\d+\\s|^DEVELOPER|^FILE|^MERGE|^-------'"
            else
                  grepString="grep -P '^Index:|^@@'"
            fi
          ;;
      -r)
            shift
            revisionString="-r $1"
            ;;
      -*)
         echo "mysvn: invalid option: $1" 1>&2
         echo "$advice" 1>&2
         exit 1
         ;;
    esac
    shift
done

finalCmd="$cmd $revisionString file:///var/svn/repos/$1 | $grepString"
echo $finalCmd

#`$finalCmd`
`$cmd $revisionString file:///var/svn/repos/$1 | $grepString`

exit 0
================= End Inserted Code =================

P.S. Any helps or suggestions on the script would be greatly appreciated.  I am only a very part-time UNIX scripter.
0
Comment
Question by:swymer
  • 2
3 Comments
 

Author Comment

by:swymer
ID: 17138004
Ooops sorry, I just realized that I left a little debugging in the very end of the file, that shows some of how I have tried to run things.  The echo was just for debugging and I had tried to just run `$finalCmd` but that was not very successful so I was trying to exec the contents of $finalCmd directly and this is where I left off.
0
 
LVL 39

Accepted Solution

by:
noci earned 250 total points
ID: 17142098
Have you allready tried

eval $finalCmd

this is because you need to reparse the contents of that string as a command, otherwise the variable is expected to be the command end the |
as part of it... If you still have problems a single \ should be before every | within the grep expression.

this works (giving one line with TEST on it).
 
$ echo -e 'test\nTEST\ntest' | egrep '^TEST|^FAILED'
TEST
$ x="echo -e 'test\nTEST\ntest' | egrep '^TEST|^FAILED'"
$ echo $x
echo -e 'test\nTEST\ntest' | egrep '^TEST|^FAILED'
$ $x
'test
TEST
test' | egrep '^TEST|^FAILED'
$ eval $x
TEST
$ (exec $x)
'test
TEST
test' | egrep '^TEST|^FAILED'

(this was done on linux with a bash shell & gnu grep)
0
 

Author Comment

by:swymer
ID: 17145918
Thanks,

That did the trick.  As I had said I only dabble in shell scripting occaisionly and I had forgotten about the eval command.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This video discusses moving either the default database or any database to a new volume.

708 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

17 Experts available now in Live!

Get 1:1 Help Now