Notices
Computer & Technology Related Post here for help and discussion of computing and related technology. Internet, TVs, phones, consoles, computers, tablets and any other gadgets.

UNIX Expert needed - grep question

Thread Tools
 
Search this Thread
 
Old Jul 8, 2002 | 08:25 AM
  #1  
Fast_Blue_Scooby's Avatar
Fast_Blue_Scooby
Thread Starter
Scooby Regular
 
Joined: Mar 2001
Posts: 454
Likes: 0
Post

I am trying to do a search on our server to find out all the occurences of the statement:

document.location

in all of our html and javascript source code.

I am using the following:

find . -type f -exec grep -l 'document.location' '{}' \; > $HOME/location1.txt

However the results returned in my output file are also bringing back any statements within the code such as document.write etc.

Can anyone help and provide me with what I should be searching for so that only files are brought back where they match document.location only.

Many thanks in advance.
Reply
Old Jul 8, 2002 | 08:49 AM
  #2  
Fast_Blue_Scooby's Avatar
Fast_Blue_Scooby
Thread Starter
Scooby Regular
 
Joined: Mar 2001
Posts: 454
Likes: 0
Post

Thanks, I've had to take the -e out to get it to run. So far it appears to have worked.

I need to find all the pages that have a server re-direct on and the best method I have found is to look through the source code looking for document.location.

So far from the above search, all the pages that it has found, appear to re-direct to another page when viewing them, so it looks like it is working.

Thanks.
Reply
Old Jul 8, 2002 | 09:25 AM
  #3  
Fast_Blue_Scooby's Avatar
Fast_Blue_Scooby
Thread Starter
Scooby Regular
 
Joined: Mar 2001
Posts: 454
Likes: 0
Post

I am getting an error in the command now, missing quote when it came across a gif file in the directory.

Anyone shed any light?
Reply
Old Jul 8, 2002 | 12:01 PM
  #4  
Fast_Blue_Scooby's Avatar
Fast_Blue_Scooby
Thread Starter
Scooby Regular
 
Joined: Mar 2001
Posts: 454
Likes: 0
Post

Thanks - I got it all sorted!

I modified the unix command just to look for .html and .shtml files and it worked a treat.

Cheers.
Reply
Old Jul 8, 2002 | 03:10 PM
  #5  
Fast_Blue_Scooby's Avatar
Fast_Blue_Scooby
Thread Starter
Scooby Regular
 
Joined: Mar 2001
Posts: 454
Likes: 0
Post

Mind you the search does bring back the matches AND also if the keyword is in comments. I am looking just for the pages that will execute the statement. Don't suppose there is some clever way in the grep statement to not bring back matches where the keyword is in comments. Probably impossible as the comments begin in different places in the code from one file to another.

I suppose I need to check the returned results one by one manually to see if the source code does indeed contain the keyword in comments and if it does then just ignore them.

Unless of course someone knows something different?!?!
Reply
Old Jul 8, 2002 | 03:42 PM
  #6  
stevencotton's Avatar
stevencotton
Scooby Regular
 
Joined: Jan 2001
Posts: 2,710
Likes: 1
From: behind twin turbos
Post

Well it isn't worth writing a regular expression to filter them all out. You could pipe the output through ``grep -v "\""'' before redirecting to a file but for instances where document.location isn't itself within quotes, but quotes exist elsewhere on the line, it wont pick it up.

Steve.
Reply
Old Jul 9, 2002 | 09:00 PM
  #7  
Hanslow's Avatar
Hanslow
Scooby Regular
 
Joined: Mar 2001
Posts: 4,496
Likes: 0
From: Derbyshire
Smile

Just do what any self respecting developer would do....remove all the comments and never put any more in

Provided all commented lines begin with the comment characters shouldn't be too hard. Could be a bit of a pain though if you need to work out if it is within a comment block.
Reply
Old Jul 9, 2002 | 10:38 PM
  #8  
snowcrash's Avatar
snowcrash
Scooby Regular
 
Joined: Mar 2002
Posts: 2,082
Likes: 0
From: www.scoobyzone.co.uk
Post

might be an idea to use a perl script...
Reply
Old Jul 17, 2002 | 06:42 PM
  #9  
Fast_Blue_Scooby's Avatar
Fast_Blue_Scooby
Thread Starter
Scooby Regular
 
Joined: Mar 2001
Posts: 454
Likes: 0
Post

Thanks for the help on the above.

How would I go about searching within HTML files for say 4 words using the grep command.

eg look for "Credit" AND "Card" AND "Payment" AND "American".

All the above 4 words must appear within the HTML file for it to be brought back.

Thanks in advance.
Reply
Old Jul 17, 2002 | 11:41 PM
  #10  
stevem2k's Avatar
stevem2k
Scooby Regular
 
Joined: Sep 2001
Posts: 4,670
Likes: 0
From: Kingston ( Surrey, not Jamaica )
Post

STFW!

grep 'Credit' *.html | grep 'Card' |grep 'American'|grep 'Payment'

Easier to OR than AND with grep.

Steve

[Edited by stevem2k - 7/17/2002 11:47:35 PM]
Reply
Old Jul 18, 2002 | 10:01 AM
  #11  
stevencotton's Avatar
stevencotton
Scooby Regular
 
Joined: Jan 2001
Posts: 2,710
Likes: 1
From: behind twin turbos
Post

You obviously didn't



for logical OR:



grep -e "(Credit|Card|Payment|American)" file



If you don't know the order in which those searchterms will be in the file(s) you have to make multiple passes and check each searched term.



I'd do this in Perl:



perl -0ne '/Credit/ && /Card/ && /Payment/ && /American/' file


Let's edit this and put it in a format you can use:

find . -name "*.html" -exec perl -0ne '/Credit/ && /Card/ && /Payment/ && /American/ && print "$ARGV\n"' {} \;

There's probably a far better way.

Steve.

[Edited by stevencotton - 7/18/2002 10:09:01 AM]
Reply
Old Jul 18, 2002 | 10:24 AM
  #12  
stevem2k's Avatar
stevem2k
Scooby Regular
 
Joined: Sep 2001
Posts: 4,670
Likes: 0
From: Kingston ( Surrey, not Jamaica )
Post

But he wants to AND , not OR
Reply
Old Jul 18, 2002 | 10:31 AM
  #13  
stevencotton's Avatar
stevencotton
Scooby Regular
 
Joined: Jan 2001
Posts: 2,710
Likes: 1
From: behind twin turbos
Post

Yeah that's what && means.

Steve.
Reply
Old Aug 7, 2002 | 08:34 AM
  #14  
Hanslow's Avatar
Hanslow
Scooby Regular
 
Joined: Mar 2001
Posts: 4,496
Likes: 0
From: Derbyshire
Question

Try

find . -type f | xargs grep -l -e 'document.location' > $HOME/location1.txt

Probably not much help


[Edited by Hanslow - 7/8/2002 8:37:03 AM]
Reply
Old Aug 7, 2002 | 09:51 AM
  #15  
stevencotton's Avatar
stevencotton
Scooby Regular
 
Joined: Jan 2001
Posts: 2,710
Likes: 1
From: behind twin turbos
Post

Paste the error.

find . -type f -exec grep -il 'document.location' {} \; > output


[Edited by stevencotton - 7/8/2002 10:01:09 AM]
Reply
Related Topics
Thread
Thread Starter
Forum
Replies
Last Post
taylor85
Wanted
2
Sep 13, 2015 04:57 PM
AzzDSM
Engine Management and ECU Remapping
4
Sep 13, 2015 03:59 PM
The Joshua Tree
Computer & Technology Related
18
Sep 11, 2015 09:24 PM
Scooby-Doo 2
Wheels And Tyres For Sale
1
Sep 9, 2015 06:51 PM




All times are GMT +1. The time now is 01:13 AM.