View the Most Wanted LQ Wiki articles.
Go Back > Forums > Non-*NIX Forums > General
User Name
General This forum is for non-technical general discussion which can include both Linux and non-Linux topics. Have fun!


Search this Thread
Old 08-27-2007, 02:54 AM   #1
Registered: Nov 2004
Posts: 101

Rep: Reputation: 15
Shell script: how to find a file containing a string


I'd like to find all files within a directory containing:
("<?" and a line break)

I tried it by using:
find . -name "*.php" | grep -lir "<?\n"
but it doesn't work. Any suggestion?

Thank you very much.
Old 08-27-2007, 03:25 AM   #2
Registered: Aug 2003
Distribution: CentOS, OS X
Posts: 5,131

Rep: Reputation: Disabled
grep -H searchstring
which means "with filename", so it prints the matching line when found, and the filename where it was found. Check out

for grep manpage. You can add some special chars to tell grep that the searchstring is in the end of the line or in the beginning of the line, so maybe adding both tells grep it's the only thing in the line. Note that if the searchstring contains special characters you'll have to escape them so they're not accidentally used by your shell in a special way.

With the use of sed, for example (or awk language), you can then turn the "answer lines" into the form you want to use. Check sed man page also.
Old 08-27-2007, 03:39 AM   #3
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,458

Rep: Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941
There are some issues in the command line you have posted:
1. piping the result from the find command directly into grep, does not look for string inside files, but only in the list of files, that is in their filenames. To actually issue a grep you may use xargs, as in
find . -name "*.php" | xargs grep <options> <pattern>
or the -exec action of the find command, as in
find . -name "*.php" -exec grep <options> <pattern> '{}' \;
2. The option -r is useless in this context, because the find command itself do recursion
3. The option -i is also useless because you are looking for a pattern made of symbols
4. Take in mind that option -l does not return the lines matching the pattern, but only the name of the first file encountered, whose text matches the pattern. If you want to force grep to print the filename before the matching lines you may consider option -H.
So, the command you're looking for maybe is
find . -name "*.php" | xargs grep -H \<\?$
where the $ sign means "at the end of line". For details look carefully at man xargs, man find, man grep

Edit: sorry for redundancy... I didn't see the answer from bOuncer while posting.

Last edited by colucix; 08-27-2007 at 03:41 AM.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell script: Find "\n\t..." to replace a string in a file michael24h7d Programming 8 05-11-2007 03:07 AM
String Manipulations in Shell Script Programming 15 04-19-2007 10:40 AM
How to find out whether a process is dead or alive from within a shell script file walwali Linux - Software 2 12-24-2005 01:39 PM
Shell Script - String Replacement revof11 Programming 7 11-29-2005 06:38 AM
Shell script to find a particular string Prasun1 Linux - General 5 08-30-2005 09:23 AM

All times are GMT -5. The time now is 08:05 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration