meder omuraliev
meder omuraliev

Reputation: 186662

grep - search for "<?\n" at start of a file

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/'

Upvotes: 1

Views: 4413

Answers (6)

PD81
PD81

Reputation: 20691

find . -name "*.php" | xargs  grep -nHo "<?[^p^x]"

^x to exclude xml start tag

Upvotes: 2

maček
maček

Reputation: 77786

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)' .

Upvotes: 4

Laurence Gonsalves
Laurence Gonsalves

Reputation: 143304

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.

Upvotes: 0

meder omuraliev
meder omuraliev

Reputation: 186662

I RTFM and ended up using:

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

the P argument enabled full perl compatible regexes.

Upvotes: 6

mob
mob

Reputation: 118665

grep '^<?$' filename

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

grep ' ^ < ? $ ' filename

Upvotes: 0

SilentGhost
SilentGhost

Reputation: 319881

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

Upvotes: 1

Related Questions