Github search using regex - regex

Is there any way to search code in a github repo using regular expressions?
Currently, I clone the repo and search.
But I want to type something like
s/foo/gi
and find all occurances of foo in the code:
foo.create
foo.extend
fooBar
barFoo
bar.foo
Is this possible? Am I missing something in the docs?

Livegrep allows for interactive regex search of ~gigabyte-scale source repositories.

This is now possible with GitHub Code Search.

Since it's a Git repo, why not just clone it locally, and then use git grep with one of it's regex options?:
git grep [-w | --word-regexp]
[-E | --extended-regexp] [-G | --basic-regexp]
[-P | --perl-regexp]
So maybe something along the lines of
$ git clone repo
$ git grep -w foo

Using the keyword "OR"?
foo.create OR foo.extend OR fooBar OR barFoo OR bar.foo
edit: Oops, I answer next to your needs. sorry.

Related

Sed: modifying only some lines

In my /etc/apt/sources.list I want to append to all non-comment lines "contrib non-free" using sed.
Here is my expression:
sed -n -i '/^\([^#].*main\)/{s/main/main contrib non-free/p}' /etc/apt/sources.list
It does what I want but it also removes all other lines.
What am I doing wrong?
Problem is with the option -n in your command that suppresses normal output. You need to remove -n and also remove p option from substitution to avoid double printing of matching lines.
This command should work for you:
sed -i '/^\([^#].*main\)/s/main/& contrib non-free/' /etc/apt/sources.list
The accepted answer from #anubhava (thanks for your code, as it pointed me in the right direction) would add contrib non-free even if it already exists in the line. Try the following:
sed -i '/^\([^#].*main\)*$/s/main/& contrib non-free/' /etc/apt/sources.list

Best way to capture a regex from stdout and pipe match into an xarg command

I am trying to put together a simple script that does a regex on a git clone output and captions the directory that the default clone cmd is cloning too and then captures this into a shell for or something that can then be piped into xargs?
E.g:
git clone git#github.com:thorchain/instaswap-sdk.git |& grep "\'\S*\'" | xargs cd
In this example grep is not the right tool as its output is the matching line and not the match. However, for the life of me, I can't seem to find a simple regex matching tool?
Thanks in adv
Luke
Turns out the missing bit was the grep -o that I needed. What I was trying to do was built a bash function the git cloned and cd into the repos directory.
End solution here:
function gitc() {
cd `git clone "$#" |& grep -o "\'\S*\'" | tr -d "'"`
}

Using grep with negative look-ahead returning no matches

So I am writing a few scripts for migrating SVN to GIT, we have a bunch of "old" branches in SVN that still exist but don't need to be moved to GIT. (Branches which happened to have already been merged to trunk).
After a bit of google-fu I've come up with the following:
$(git for-each-ref --format='%(refname:short)' --merged origin/trunk | grep '(?!origin\/trunk)origin\/.*')
To be passed to
git branch -D --remote _previouscommandgoeshere_
If I run just git for-each-ref --format='%(refname:short)' --merged origin/trunk I get the following output:
origin/IR1091
origin/IR1102
origin/IR1105
...
origin/IR932
origin/Software
origin/trunk
origin/trunk#6792
origin/trunk#6850
When I add the grep command I get 0 values.
However, https://regexr.com/3ot1t has thaught me that my regexp is doing exactly what I want to do. Remove all branches except for the trunk branch.
What is wrong with the regexp/grep? (note I am not a linux/grep guru. This is all done in bash that comes with windows git)
The regexp is right, but grep by default does not support PCRE expression constructs like Negative look-ahead (?!. You need to enable the -P flag to enable the PCRE library, without that it just supports the Basic Regular Expression engine
.. | grep -oP '(?!origin\/trunk)origin\/.*'
Or use a perl regex match on the command line for which no flags need to be set up
.. | perl -ne 'print if /(?!origin\/trunk)origin\/.*/'
grep -P 'origin/(?!trunk)'
just this, can match what your wanted

Renaming files using regular expressions - Linux

I have three files named
file1.txt
file2.txt
file3.txt
I am trying to rename them to
mynewfile-1.txt
mynewfile-2.txt
mynewfile-3.txt
How would I go about this using regular expressions?
Like this :
rename -n 's/^file/mynewfile-/' *.txt
or from comments :
rename -n 's/^file(\d+)/mynewfile-${1}-test/' *.txt
___ ____
^ ^
capturing group |
captured group
Drop -n switch when the output looks good to rename for real.
There are other tools with the same name which may or may not be able to do this, so be careful.
The rename command that is part of the util-linux package, won't.
If you run the following command (GNU)
$ rename
and you see perlexpr, then this seems to be the right tool.
If not, to make it the default (usually already the case) on Debian and derivative like Ubuntu :
$ sudo apt install rename
$ sudo update-alternatives --set rename /usr/bin/file-rename
For archlinux:
pacman -S perl-rename
For RedHat-family distros:
yum install prename
The 'prename' package is in the EPEL repository.
For Gentoo:
emerge dev-perl/rename
For *BSD:
pkg install p5-File-Rename
For Mac users:
brew install rename
If you don't have this command with another distro, search your package manager to install it or do it manually
Or you can use perl CPAN:
cpan -i File::Rename
Old standalone version can be found here
man rename
This tool was originally written by Larry Wall, the Perl's dad.

How do you get Amazon SES working on Debian Squeeze?

All the Perl dependencies for it are met but I'm getting this error:
Can't locate object method "ssl_opts" via package "LWP::UserAgent" at SES.pm line 250.
I just wanted to document what I had to do to get this running on my Debian system. The solution for Ubuntu is probably the same.
First, to let Perl find SES.pm make the directory /usr/local/lib/site_perl and copy SES.pm there. I prefer this solution over what the README recommends.
Your system probably has a lot of the dependencies met already so instead of installing duplicate packages just check first which ones it needs. To do that run these commands. If it gives an error it's not met:
perl -e 'use Crypt::SSLeay'
perl -e 'use Digest::SHA'
perl -e 'use Bundle::LWP'
perl -e 'use LWP::Protocol::https'
perl -e 'use MIME::Base64'
perl -e 'use Crypt::SSLeay'
perl -e 'use XML::LibXML'
I had to remove the package libcrypt-ssleay-perl because it's not compatible with this Amazon script. With it the script produces the error in the question.
I installed these packages from Debian:
libxml-libxml-perl
libssl-dev (needed to compile dependencies)
To find out which package contains the Perl module you need use this page on the Debian site to search the contents of packages:
http://www.debian.org/distrib/packages
Replace the :: in the package with / and put .pm at the end. For example if you need XML::LibXML search for XML/LibXML.pm
I installed these packages from CPAN. It takes a little while though. There are a lot of dependencies.
perl -MCPAN -e 'install LWP::Protocol::https'
perl -MCPAN -e 'install Crypt::SSLeay'
Comment out line 250 in SES.pm, as follows:
# $browser->ssl_opts(verify_hostname => 1);
Then it will run. Probably less secure. But it runs.