This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Linux / UNIX and the fly likes what is the benefit of using the reverse search by pressing CTRL+R? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Engineering » Linux / UNIX
Bookmark "what is the benefit of using the reverse search by pressing CTRL+R?" Watch "what is the benefit of using the reverse search by pressing CTRL+R?" New topic
Author

what is the benefit of using the reverse search by pressing CTRL+R?

Amandeep Singh
Ranch Hand

Joined: Jul 17, 2008
Posts: 844
Normally to see the old commands, i can press up arrow to see the commands.
how different is the reverse search ?

Does it starts from 1st used command rather than to last used command ?


SCJP 1.4, SCWCD 5, SCBCD 5, OCPJWSD 5,SCEA-1, Started Assignment Part 2
My blog- http://rkydesigns.blogspot.com
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11437
    
  85

The reverse search does a search through your last used commands backwards, matching on the text you provide.

Say for instance your command history includes:
Note: the actual commands used here are irrelevant - they are just used for an example

Now let's say that I know that I had previously run a command that told me what the possible outcomes of SPF verification were, and I wanted to re-run that command. I could use the up-arrow to step through the 9 other commands to get to line number 3 - the command I actually want to run.

However I know that I am only interested in commands containing the text SPF, so I could type <ctrl-r>SPF - by the time I have finished typing this, bash will have searched backwards through my history, and would be showing me line number 4 - close, but not quite right. So I press <ctrl-r> once more, and it will go back to line 3 (and if I pressed it again, I would go back to line 1).

Alternatively, since I know that I had used the uniq command in the line I want to re-run, I could type <ctrl-r>uniq, and line 3 will be the first (and only) command retrieved.

This feature alone makes <ctrl-r> valuable. Especially when you have a very large command line history (I have 500 commands in my history at all times).


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Amandeep Singh
Ranch Hand

Joined: Jul 17, 2008
Posts: 844
very good answer . Excellent explanation.
Anand Hariharan
Rancher

Joined: Aug 22, 2006
Posts: 257

Andrew Monkhouse wrote:The reverse search does a search through your last used commands backwards, matching on the text you provide.

Say for instance your command history includes:

<snipped example commands>
Andrew Monkhouse wrote:Note: the actual commands used here are irrelevant - they are just used for an example


Noted. With regards to just thought you may like to know about grep's -l (lower case ell) option.

<snipped useful explanation>

- Anand

"Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away." -- Antoine de Saint-Exupery
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11437
    
  85

Thanks for taking the time to look at what I was doing and suggesting alternatives - it is really appreciated. However in this particular case I couldn't see how to make a single grep statement give me the output I needed, and at the time I was more interested in getting the result now than playing around with trying to get it to work.

Normally you would use the -l option to list files where grep has found a match. However I wanted to find the files where there were zero matches, but that is not so simple. The -v option will find lines that don't match, however for the files in question there would not have been a single file without at least one non-matching line, so that didn't help.

I could probably also have done something like:

Or even:

Hmm - I could probably do this as a single line awk statement (or Perl for that mater).

The Perl slogan "There's More Than One Way To Do It" applies everywhere!

Regards, Andrew
Anand Hariharan
Rancher

Joined: Aug 22, 2006
Posts: 257

Methinks you may have the wrong idea of grep's exit status. It returns 0 if matches are found. Check for the exit status for commands executed with a for loop is most likely not going to give you what you want.

GNU grep (not in POSIX) has a -L option that may do what you want. [NB: I am writing this from a M$ box, and I do not know this for certain.]

best,
- Anand
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11437
    
  85

Anand Hariharan wrote:Methinks you may have the wrong idea of grep's exit status. It returns 0 if matches are found.


Correct - but what I wanted was to find the files that did not contain the specified string, in which case checking for an exit status of 1 is correct.

On further thinking, doing a count in that statement was overkill - it would have been better to use the -quiet option so it exits immediately if a match is found, which I can then ignore.

Anand Hariharan wrote:Check for the exit status for commands executed with a for loop is most likely not going to give you what you want.


Can you please elaborate? The $? should contain the exit status of the most recently executed foreground command, so on each loop it should contain the exit status of the grep statement that has just been run, which I can then use to determine whether the current file is one I care about or not. Checking the exit status outside of the loop does not make sense - it would only tell me the exit status of the very last file checked.

Anand Hariharan wrote:GNU grep (not in POSIX) has a -L option that may do what you want. [NB: I am writing this from a M$ box, and I do not know this for certain.]


Doh! I have spent far too many years on non-GNU based systems, so I tend to go with POSIX options. I am currently using a Macintosh, and I didn't think to check what version of grep I am using - it is GNU grep. So I could have used that.

Thanks!

Regards, Andrew
Anand Hariharan
Rancher

Joined: Aug 22, 2006
Posts: 257

Andrew Monkhouse wrote:
Anand Hariharan wrote:Methinks you may have the wrong idea of grep's exit status. It returns 0 if matches are found.


Correct - but what I wanted was to find the files that did not contain the specified string, in which case checking for an exit status of 1 is correct.

Your original post (where you explained what CTRL+R does) used a comparison against 0. It was for this that I pointed out the -l option, for which you replied saying that you were seeking files that didn't match.

Andrew Monkhouse wrote:
Anand Hariharan wrote:Check for the exit status for commands executed with a for loop is most likely not going to give you what you want.


Can you please elaborate? The $? should contain the exit status of the most recently executed foreground command, so on each loop it should contain the exit status of the grep statement that has just been run, which I can then use to determine whether the current file is one I care about or not. Checking the exit status outside of the loop does not make sense - it would only tell me the exit status of the very last file checked.

Mea culpa. I misread the one-liner and (incorrectly) parsed it as checking for the exit status outside the for loop.

Andrew Monkhouse wrote:
Anand Hariharan wrote:GNU grep (not in POSIX) has a -L option that may do what you want. [NB: I am writing this from a M$ box, and I do not know this for certain.]


Doh! I have spent far too many years on non-GNU based systems, so I tend to go with POSIX options. I am currently using a Macintosh, and I didn't think to check what version of grep I am using - it is GNU grep. So I could have used that.

Thanks!

You are welcome.

- Anand
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: what is the benefit of using the reverse search by pressing CTRL+R?