Grep-ing from multiple files - grep

I have a lot of log files with format foo.log.[1-100].gz, and another one detail-20161205-[00-23]. Need to find some string from multiple files.
I'm trying to do the following:
zfgrep String foo.log.[45-64].gz,
but I'm always getting wrong output, not from mentioned files.
Thus, I want to understand how to grep from .gz files and from not .gz (from the second format). Can I use commands other than grep as well?

I believe you should be able to do something like grep String foo.log.{1..100}.gz and grep String detail-20161205-{00..23}. If not all the files exist in that range, you can add the -s option so you don't see all the errors.
grep -s String foo.log.{1..100}.gz
What detail-20161205-[00-23] does for example is expand to 0, 0-2, 3, leading to the wrong files being searched.

Related

Search for files that contain pattern

I have this search - I would like to print out the paths of files that contain the matching text:
grep -r "jasmine" .
and it yields results that look like this:
./app-root/runtime/repo/node_modules/jasmine-core/.github/CONTRIBUTING.md:- [Jasmine Google Group](http://groups.google.com/group/jasmine-js)
./app-root/runtime/repo/node_modules/jasmine-core/.github/CONTRIBUTING.md:- [Jasmine-dev Google Group](http://groups.google.com/group/jasmine-js-dev)
./app-root/runtime/repo/node_modules/jasmine-core/.github/CONTRIBUTING.md:git clone git#github.com:yourUserName/jasmine.git # Clone your fork
./app-root/runtime/repo/node_modules/jasmine-core/.github/CONTRIBUTING.md:cd jasmine # Change directory
./app-root/runtime/repo/node_modules/jasmine-core/.github/CONTRIBUTING.md:git remote add upstream https://github.com/jasmine/jasmine.git # Assign original repository to a remote named 'upstream'
./app-root/runtime/repo/node_modules/jasmine-core/.github/CONTRIBUTING.md:Note that Jasmine tests itself. The files in `lib` are loaded first, defining the reference `jasmine`. Then the files in `src` are loaded, defining the reference `j$`. So there are two copies of the code loaded under test.
./app-root/runtime/repo/node_modules/jasmine-core/.github/CONTRIBUTING.md:The tests should always use `j$` to refer to the objects and functions that are being tested. But the tests can use functions on `jasmine` as needed. _Be careful how you structure any new test code_. Copy the patterns you see in the existing code - this ensures that the code you're testing is not leaking into the `jasmine` reference and vice-versa.
But I just want the file names, I don't want to print out the matching contents, just the file names, how can I do that?
The problem is that the matching text will wrap around in the terminal and make the results basically unreadable.
Did you use the -l flag from grep?
-l, --files-with-matches
Suppress normal output; instead print the name of each input file from which output would normally have been printed. The scanning will stop on the first match.
A simple search on my home directory,
grep -rl 'bash' .
./.bashrc
./.bash_history
./.bash_logout
./.bash_profile
./.profile
./.viminfo
As a matter of fact, -l is a POSIX defined option for grep should be available in almost all distros.

Ruby- how to get specific keyword from files/folder

I need to go through in each and every folders/files in php to find some specific keyword in this case translation format e.g $this-> and $translator->.
I need to get those result and put it on to new files.
Here is what I have tried before using ruby.
this = File.readlines("folder_path.php")
#If I need to get any translation that contain $this-> should I use grep? I tried using grep before but not giving result that I need.
that = File.open("new_file.txt", "w")
that << this
that.close
Hope that I didn't make any confusion. Thanks.
Just use grep:
grep '$this->' -R *.php -n > result.txt

Force genstrings to build the Localizable.strings file in order of appearance rather than alphabetically

I'm new to internationalization and localization for iOS. I'm running genstrings:
find . -name \*.m | xargs genstrings -o en.lproj to generate my Localizable.strings files. It builds the file in alphabetical order (by key).
For ease of translation I'd prefer that the keys and values be ordered by their order of appearance in the .m files. Is this possible with genstrings? I couldn't find the relevant info on its man page.
You could do something like:
find . -name '*.m' -print | xargs -n1 genstrings -a
I'm sure there are more elegant ways. Perhaps just use ls *.m instead of the find. The strings are kept together by file with -a switch but they are still sorted within each file.
It is not possible to change the behavior of genstrings other than what it is allowed within the parameters specified in the manual:
https://developer.apple.com/library/mac/#documentation/Darwin/Reference/Manpages/man1/genstrings.1.html
but if you want to ease the translation you can use Linguan
http://www.cocoanetics.com/apps/linguan/
If you want to stick to genstrings and are having some trouble with it you can try this page, it offers a good explanation:
http://spritebandits.wordpress.com/2012/01/25/ios-iphone-app-localization-genstrings-tips/
But yeah returning to the main question, it is not possible in my knowledge.

extract data between two expression in unix shell script

i have a set of tags, from which i need to extract some data. I knwo this might be simple. I am not able to get to the part exactly. The tag is shown bewlow.
<Response><Result>Success</Result></Response>
I want to extract whatever comes between the tags. In this case, 'Success'.
I tried using the grep command , but couldnt get it done. Any help would be appreciated.
echo "<Response><Result>Success</Result></Response>" | perl -npe 's/.*>([^<]+)<.*/$1/'
If the data is saved in a file:
perl -npe 's/.*>([^<]+)<.*/$1/' infile

ack misses results (vs. grep)

I'm sure I'm misunderstanding something about ack's file/directory ignore defaults, but perhaps somebody could shed some light on this for me:
mbuck$ grep logout -R app/views/
Binary file app/views/shared/._header.html.erb.bak.swp matches
Binary file app/views/shared/._header.html.erb.swp matches
app/views/shared/_header.html.erb.bak: <%= link_to logout_text, logout_path, { :title => logout_text, :class => 'login-menuitem' } %>
mbuck$ ack logout app/views/
mbuck$
Whereas...
mbuck$ ack -u logout app/views/
Binary file app/views/shared/._header.html.erb.bak.swp matches
Binary file app/views/shared/._header.html.erb.swp matches
app/views/shared/_header.html.erb.bak
98:<%= link_to logout_text, logout_path, { :title => logout_text, :class => 'login-menuitem' } %>
Simply calling ack without options can't find the result within a .bak file, but calling with the --unrestricted option can find the result. As far as I can tell, though, ack does not ignore .bak files by default.
UPDATE
Thanks to the helpful comments below, here are the new contents of my ~/.ackrc:
--type-add=ruby=.haml,.rake
--type-add=css=.less
ack is peculiar in that it doesn't have a blacklist of file types to ignore, but rather a whitelist of file types that it will search in.
To quote from the man page:
With no file selections, ack-grep only searches files of types that it recognizes. If you have a file called foo.wango, and ack-grep doesn't know what a .wango file is, ack-grep won't search it.
(Note that I'm using Ubuntu where the binary is called ack-grep due to a naming conflict)
ack --help-types will show a list of types your ack installation supports.
If you are ever confused about what files ack will be searching, simply add the -f option. It will list all the files that it finds to be searchable.
ack --man states:
If you want ack to search every file,
even ones that it always ignores like
coredumps and backup files, use the
"−u" switch.
and
Why does ack ignore unknown files by
default? ack is designed by a
programmer, for programmers, for
searching large trees of code. Most
codebases have a lot files in them
which aren’t source files (like
compiled object files, source control
metadata, etc), and grep wastes a lot
of time searching through all of those
as well and returning matches from
those files.
That’s why ack’s behavior of not
searching things it doesn’t recognize
is one of its greatest strengths: the
speed you get from only searching the
things that you want to be looking at.
EDIT: Also if you look at the source code, bak files are ignored.
Instead of wrestling with ack, you could just use plain old grep, from 1973. Because it uses explicitly blacklisted files, instead of whitelisted filetypes, it never omits correct results, ever. Given a couple of lines of config (which I created in my home directory 'dotfiles' repo back in the 1990s), grep actually matches or surpasses many of ack's claimed advantages - in particular, speed: When searching the same set of files, grep is faster than ack.
The grep config that makes me happy looks like this, in my .bashrc:
# Custom 'grep' behaviour
# Search recursively
# Ignore binary files
# Output in pretty colors
# Exclude a bunch of files and directories by name
# (this both prevents false positives, and speeds it up)
function grp {
grep -rI --color --exclude-dir=node_modules --exclude-dir=\.bzr --exclude-dir=\.git --exclude-dir=\.hg --exclude-dir=\.svn --exclude-dir=build --exclude-dir=dist --exclude-dir=.tox --exclude=tags "$#"
}
function grpy {
grp --include=*.py "$#"
}
The exact list of files and directories to ignore will probably differ for you: I'm mostly a Python dev and these settings work for me.
It's also easy to add sub-customisations, as I show for my 'grpy', that I use to grep Python source.
Defining bash functions like this is preferable to setting GREP_OPTIONS, which will cause ALL executions of grep from your login shell to behave differently, including those invoked by programs you have run. Those programs will probably barf on the unexpectedly different behaviour of grep.
My new functions, 'grp' and 'grpy', deliberately don't shadow 'grep', so that I can still use the original behaviour any time I need that.

Resources