Operating System - HP-UX
1834130 Members
3094 Online
110064 Solutions
New Discussion

Re: Searching within files for "*"

 
SOLVED
Go to solution
Dave Walley
Frequent Advisor

Searching within files for "*"

Hi.

I have to search through a large directory for all occurences of a string i.e. DAVE and from these I must exclude where there is an asterisk in column 7. i have used the following and am not getting the right answer.

find . -name "*.fil"|xargs grep -l DAVE|xargs grep -l "DAVE *"

The problem I believe is because I am looking for an asterisk in pos 7.

Any clues?.

Thanking you in advance.

Dave
why do i do this to myself
8 REPLIES 8
John Palmer
Honored Contributor

Re: Searching within files for "*"

Try:-

find . -name "*.fil" -exec grep -l "^DAVE \*" \;

That is a backspace before the asterisk in the grep string and a backspace before the ;

Regards

John
John Palmer
Honored Contributor

Re: Searching within files for "*"

Sorry Dave, I misread your original request.
To get a list of files that contain the string "DAVE" but do not have a record with an * in column 7 try:-

find . -name "*.fil" -exec grep -l DAVE {} ;| xargs grep -l "......\*";

Regards,

John

Kofi ARTHIABAH
Honored Contributor

Re: Searching within files for "*"

I believe for the EXCLUDE, you want the -v switch for the grep

find....(blah blah)... grep -l -v "^......\*"

remember the ^
nothing wrong with me that a few lines of code cannot fix!
Dave Walley
Frequent Advisor

Re: Searching within files for "*"

Many thanks for your reply.

My problem requires me to only select files initially with DAVE and excluding those with 'DAVE *'.

I managed to get the right answer with the following script but I hoped someone may had had a more efficient way.

find /dirname -name "*.fil"|xargs fgrep -i DAVE | fgrep -iv
"DAVE *"|cut -d/ -f6|cut -d: -f1|sort -u

The fgrep has less functionality I believe but it accepts and * for an *

Thanks once again.

Dave
why do i do this to myself
John Palmer
Honored Contributor
Solution

Re: Searching within files for "*"

Dave,

Providing the lines always start DAVE and have at least 7 characters (ie none just DAVE) then you could get the find command to do most of the work:-

find /dirname -name "*.fil" -exec grep -l "^DAVE..[^\*]" {} \; | cut...

Here grep will select files that have a line which starts DAVE

Hope this helps...

Wessel Baptist
Advisor

Re: Searching within files for "*"

Try this one:

find / -name "*DAVE*.fil" | grep -v "DAVE*"

Success
logics take you from A to B, imagination takes you anywhere
Wessel Baptist
Advisor

Re: Searching within files for "*"

Sorry,
this one, whith a blackslash before the asterix in the grep part.

find . -name "*DAVE*.fil"|grep -v "DAVE\*"
logics take you from A to B, imagination takes you anywhere
Wessel Baptist
Advisor

Re: Searching within files for "*"

Try this one:

find / -name "*DAVE*.fil" | grep -v "DAVE*"

Success
logics take you from A to B, imagination takes you anywhere