ack on solaris – grep recursively.

Some days back I posted an article on ack here. Today I was working on Solaris 9 and the default grep installed on the system does not have the \”-r\” option to search recursively. So, you know for all the time I had to keep using find with grep to work with directories recursively. This was something similar to the one below:

find . -type f -exec grep amit {} \\;

or with useless use of cat command:

find . -type f -exec cat{} \\; | grep amit

This is okay to make one or two searches but not that easy to keep typing and I already have too many aliase\’s to have one more added to the list. So, I tried to use the ack command on the Solaris box, knowing full well that the command is written in perl and BINGO.

There\’s not much you need to do to get ack working on Solaris. Here\’s what you need to do:

Copy the ack command to one of the paths in the PATH.

ack needs App/Ack.pm.

Ack.pm needs File/Next.pm

So, you copy these two files from any installation and then copy them to one of the paths mentioned in the output of \”perl -V\”. Done.

Wasn\’t that easy.


more powerful grep – ack

For last couple of days, I have been using ack instead of grep. Here is description of ack:

Ack is designed as a replacement for grep.

There are couple of reasons I am using ack. First and foremost being it does not require a filename. So, for seaching amit in all files recursively under the current directory I can simply use

ack amit

More reasons to use ack:

It supports pager option and there can be a rc file that can be used to define the default options that needs to be used with ack. Here is the contents of my $HOME/.ackrc file.

–pager=most

–color

–sort-files

-u

-H

–context=3

–all

SO, basically when I do the search, the output is piped to most command and there are 3 context lines displayed for the search. The filename too is displayed for the search result and the output is colored. There are other options used in the rc file, that you can see in the man page for ack.

Now if I had to do that with grep, I would need a function for grep or an alias but this is much cleaner. Ain\’t it. What do you think, leave a comment to tell me.

You need to install ack with the following command:

sudo yum install ack

vim search multiple files

Searching some pattern in multiple files is something that is required very often. There are multiple ways to do it, like grep, ack, find and so on so forth. But if your editor already has the capability to do that, then why leave your favourite vi and go to shell. I will point you to some scripts and some wiki pages on how to do this 🙂

http://vim.wikia.com/wiki/Search_for_current_word_in_multiple_files

http://www.vim.org/scripts/script.php?script_id=2438

http://www.vim.org/scripts/script.php?script_id=311

\"Reblog