Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Unix search techniques.... 2

Status
Not open for further replies.

areza123

Programmer
Jul 4, 2002
60
IN
Using man I'm trying to find the Unix command line options to search a file name recursively. (the file may be present in directories inner to the current directory)

I execute...
man ls | grep "recur"

The output is...
list subdirectories recursively

Now I want to read the full sentence ? Or lets say 10 lines above this and 10 lines below this output. Is there some command ?

man ls | grep "recur" | head

and

man ls | grep "recur" | head

do not work. They give the same output.

Neither do...
head | man ls | grep "recur"
tail | man ls | grep "recur"

Help...
 
find <dir> -name '<filename>'

eg. cd /etc ; find . -name 'hosts'
 
hey jad thanks for the quick response but I was looking for the search technique not the command.

Example:
When I execute man ls ...it gives me about 100 lines output....but I only need to see few lines in between say...


-R, --recursive
list subdirectories recursively

-s, --size
print size of each file, in blocks


Cheers
 
For linux...

man ls | grep -C 10 &quot;recur&quot;

man grep for more options.
 
umm actually it's not just for linux, its for use with GNU grep ... and it give you extra lines of context.
 
Another thing you can do is to just run &quot;man ls&quot; then when the &quot;more&quot; prompt comes up press the / key and enter your search string. This is identical to the search function in vi.

Dennis
 
in 'man' you can type 'v' to enter vi mode on the man page

you will then probably want to:
:g/[_+-]^V^H/s///g

to get rid of the formatting ^V^H is Control-V followed by Control-H
 
Thanks for your help. I really appreciate. Now let me extend the problem statement to a more complicated but useful search technique....

man ls | grep -C 10 &quot;recur&quot;

works perfectly fine i.e. shows the 10 lines close to recur...the point here is that the output of man ls is one stream (or source...sorry I don't know if I'm using the right terminology)

Now consider the command:
grep -R &quot;recur&quot; *
This looks for the occurance of the word &quot;recur&quot; in all files (including files in child directories) i.e. there are multiple sources from where it picks up information. Now if I want to see 2 lines above and below each occurance of the keyword in each file. How do I acheive this

Example : I have 2 files say a.cc and b.cc

a.cc
====
cout << a;
cout << b;
cout << c;

b.cc
====
cout << d;
cout << b;
cout << f;

grep -R &quot;cout << b&quot; *

would yield the output
a.cc:cout << b;
b.cc:cout << b;

But what I am keen is too see is about 3 lines of source code above and below the occurance of &quot;cout << b&quot; in each of the files i.e. a.cc and b.cc ?
 
I am confused why you use -R instead of -C in this case?
 
Ok...actually your confusion created some new ideas with me which actually solved my problem.

The final solution to the above problem is

grep -R -C 1 &quot;ostream_iterator&quot; *

would search files listed in all subdirectories for the string &quot;ostream_iterator&quot; which is what I wanted.

Without the -R options the search was failing because I had only directories and not actual files as children of my current directory. My problem is solved but I hope your confusion is too!


Because I want to recurse inside subdirectories (select all files)
 
More Advances....

I want to modify my search pattern now
Assume I have the file a.txt

a.txt
-----
#ifdef USE_stub_in_nt_dll
...
#ifdef USE_stub_in_nt_dll
...
ifdef USE_stub_in_nt_dll
....
..
#ifdef USE_stub_in_nt_dll

If you observe there is only ONE USE_stub_in_nt_dll which is uncommented. (without #).
If this file contains about 1000 lines and I grep as

grep &quot;USE_stub_in_nt_dll&quot; *

I get the output
#ifdef USE_stub_in_nt_dll
#ifdef USE_stub_in_nt_dll
ifdef USE_stub_in_nt_dll
#ifdef USE_stub_in_nt_dll

But I only want to see the ONE line which is uncommented
i.e. ifdef USE_stub_in_nt_dll

So I issue the command
grep &quot;USE_stub_in_nt_dll&quot; * | grep -v &quot;#&quot;

Is this the correct way of doing it ?
I was wondering that there must be an option of doing it without the pipe ?

Note: grep -n -R -v &quot;# &quot;USE_stub_in_nt_dll&quot; *
will not work in this case. It outputs all the lines that do not have #
 
In your case, simply you can do like as this:

grep '^ifdef' filename
 
Try...

grep -E '^[^#]*USE_stub_in_nt_dll'

Where..

-E = Extended regular-expression
^ = beginning of line
[^#]* = any number of characters other than #

See man regexp for more details.

 
How do I grep for more than 1 word in a line ?
If I have the following file:
a.txt
-----
abc ghi def
abc xyz
abc def ghi

I want to isolate only lines containing words abc & def
How do I acheive this ?
 
If the -w is not available for uoyr grep, try this:
Code:
grep -E '^abc[ \t]|[ \t]abc[ \t]|[ \t]abc$' a.txt |
  grep -E '^def[ \t]|[ \t]def[ \t]|[ \t]def$'
If -w is OK, try this:
Code:
grep -w 'abc' a.txt | grep -w 'def'

Hope This Help
PH.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top