3

How do I extract the names from the gitolite info command output, for further piping into a script?

I'm writing a migration script to migrate all my repositories from gitolite to a Gitlab server. Thus, I want to get all the repository names from gitolite and use those in Gitlab. Below is the example output I'm trying to match against.

hello sitaram, this is gitolite v2.1-29-g5a125fa running on git 1.7.4.4
the gitolite config gives you the following access:
     R      SecureBrowse
     R   W  anu-wsd
     R   W  entrans
    @R   W  git-notes
    @R   W  gitolite
     R   W  gitolite-admin
     R   W  indic_web_input
 @C  R   W  private/sitaram/[\w.-]+
     R   W  proxy
 @C @R   W  public/sitaram/[\w.-]+
    @R_ @W_ testing
     R   W  vic

This should output:

SecureBrowse
anu-wsd
entrans
git-notes
gitolite
gitolite-admin
indic_web_input
proxy
testing
vkc

Currently I'm trying to put this in a shell script, so it would be usable for others. My current approach is a grep command, which looks like the following:

grep -Eio "([A-Za-z0-9_-]+)$" gitolite-info-output

However, this also captures the 4 at the end of the first line. I've been trying several approaches, but I can't seem to exclude that properly, without including or excluding other things.

Doing this OS X 10.10.3.

3

Based on the input you show in your question, this should work:

$ grep -oP '^[ @]*R.* \K.*' gitolite-info-output
SecureBrowse
anu-wsd
entrans
git-notes
gitolite
gitolite-admin
indic_web_input
proxy
testing
vic

This is using GNU grep's -P switch to enable Perl Compatible Regular Expressions which give us \K : "Exclude anything matched up to this point". Combined with -o, we can search for lines starting with 0 or more spaces or @ (^[ @]*), then an R, then 0 or more characters until another space. All this is discarded because of the \K so only the last word is printed.


If you don't have GNU grep (on OSX, for example), you can do something like this:

$ grep -E '^[ @]*R' gitolite-info-output | awk '{print $NF}'
SecureBrowse
anu-wsd
entrans
git-notes
gitolite
gitolite-admin
indic_web_input
proxy
testing
vic

Or do the whole thing in awk:

$ awk '/^[ @]*R/{print $NF}' gitolite-info-output 
SecureBrowse
anu-wsd
entrans
git-notes
gitolite
gitolite-admin
indic_web_input
proxy
testing
vic

Or Perl:

$ perl -nle '/^[ @]*R.*\s(.*)/ && print $1' gitolite-info-output 
SecureBrowse
anu-wsd
entrans
git-notes
gitolite
gitolite-admin
indic_web_input
proxy
testing
vic
  • Well, this is awesome. Sadly I can't give you multiple up-votes. For every time you added another correct answer :) You were constantly beating my comments with more helpful edits. Thanks! – SpacyRicochet Apr 19 '15 at 14:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.