views:

157

answers:

5

I have a hunch that I should probably be using ack or egrep instead, but what should I use to basically look for

<?

at the start of a file? I'm trying to find all files that contain the php short open tag since I migrated a bunch of legacy scripts to a relatively new server with the latest php 5.

I know the regex would probably be '/^<\?\n/'

+1  A: 

if you worried about windows line endings, just add \r?.

SilentGhost
+2  A: 

If you're looking for all php short tags, use a negative lookahead

/<\?(?!php)/

will match <? but will not match <?php

[meder ~/project]$ grep -rP '<\?(?!php)' .
macek
+1 for thinking of negative lookaheads and specifying -P too.
meder
A: 
grep '^<?$' filename

Don't know if that is showing up correctly. Should be

grep ' ^ < ? $ ' filename

mobrule
+3  A: 

I RTFM and ended up using:

grep -RlIP '^<\?\n' *

the P argument enabled full perl compatible regexes.

meder
mark this question as answered so we can get on with our lives ;)
Justin Johnson
"You can accept your own answer in 2 days." :(
meder
But in the meantime, you can get a Self-Learner badge for actually RTFM. *clicks +1* :-P
Tomalak
A: 

Do you mean a literal "backslash n" or do you mean a newline?

For the former:

grep '^<?\\n' [files]

For the latter:

grep '^<?$' [files]

Note that grep will search all lines, so if you want to find matches just at the beginning of the file, you'll need to either filter each file down to its first line, or ask grep to print out line numbers and then only look for line-1 matches.

Laurence Gonsalves