Help Using Grep command to Find the string in the files in the root directory

I want to serch for a string in all the files in the root directory. i want the search to be limited to only the files in the directory i.e the search to be done only in the files not in the sub directory.

the approaches tried are
1)grep "pattern string"
this command was serching the pattern in all the files in the root directory as well as in the sub directories which is not required.
2)grep "pattern string" `find . -type f -print -o -type d -prune`
this was not giving any result
3) grep -| "pattern" *.txt
it says the pattern not found . but the pattern is present in files in the directory

one approach to this i have thought listing all the files in the root directory and not in the sub directory and then passing the output of this in the grep command. tell me if this is possible.

if possible it would be helpful if i am able to get the rite syntax for find command only. :slight_smile:

Can some one please help me with this

ls -l | grep -v '^d' | awk '{print $NF}' | while read i
do
grep "$i" pattern
done

not tested but should do the job

there must be faster ways but I've no time to read man pages now :slight_smile:

also you can do as follows:

ls -l|grep -v "^d"|awk '{print $9}'|xargs grep <pattern>

this way has been tested and should do the search only on search able files and avoidng any special files if there is.

also you can do as follows:

ls -l|grep -v ^[dbc]|awk '{print $9}'|xargs grep <pattern>

this way has been tested and should do the search only on any files in the current directory avoiding any special files that might be there.