• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 856
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ?
 
author and jackaroo
Posts: 12200
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
 
Amandeep Singh
Ranch Hand
Posts: 856
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
very good answer . Excellent explanation.
 
Rancher
Posts: 280
VI Editor C++ Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Andrew Monkhouse
author and jackaroo
Posts: 12200
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 280
VI Editor C++ Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 12200
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 280
VI Editor C++ Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Yes, my master! Here is the tiny ad you asked for:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic