Solved

bash scripting - Using pipe character in grep in scripts

Posted on 2006-07-19
3
1,347 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 40

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
iSeries FTP Exit Program 8 163
how to monitor remote shell execution on linux 9 107
wait notify demo infinite loop 3 137
How to convert MFC::CString to UTF8 wchar_t* 10 387
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

726 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