- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Search all text files for a string
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-17-2001 12:17 PM
тАО12-17-2001 12:17 PM
I'm trying to search all text files on the system for a specific string. My results haven't been very good so far. I'm having a problem selecting only text files, I'm missing some files I shouldn't, and some lines are too long for vi (when I try to edit the results file).
Anyone got something to help?
Thanks,
Darrell
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-17-2001 12:21 PM
тАО12-17-2001 12:21 PM
Re: Search all text files for a string
find / -type f | xargs grep string
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-17-2001 12:41 PM
тАО12-17-2001 12:41 PM
Re: Search all text files for a string
You might try something close to this:
find . -type f | while read X
do
file ${X} | grep -q -i "ascii text"
STAT=$?
if [ ${STAT} -eq 0 ]
then
grep -q -i "Your target string" ${X}
STAT=$?
if [ ${STAT} -eq 0 ]
then
echo "File: ${X}"
grep -i "Your target string" ${X}
fi
fi
done
You could, of course also pass in "Your target string" as $1
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-17-2001 12:43 PM
тАО12-17-2001 12:43 PM
Solutionthis is not so simple, really!
Usually something like this is working, but "file" tests only for the first 128 bytes of a file...
find / -type f -print |
while read name; do case $(file $name) in
*text*) grep -i "PATTERN" $name /dev/null ;;
esac;done
First, you get only the files (no devices, pipes, directories, links), then you select the *text files* (ascii, C, awk, english...), and then you use "grep" on those. The additional parameter "/dev/null" forces "grep" to show the name of file and the line containing the match...
HTH,
Wodisc
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-17-2001 02:27 PM
тАО12-17-2001 02:27 PM
Re: Search all text files for a string
find $DIR | xargs file |grep "ascii text" | awk '{ print $1}' | sed 's/://g' >file_list
for i in `cat file_list``
do
grep ""yourstuff"" $i
if [ $? -eq 0 ]
then
echo "$i" >>Caught
done
HTH
raj
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-18-2001 08:13 AM
тАО12-18-2001 08:13 AM
Re: Search all text files for a string
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-18-2001 08:16 AM
тАО12-18-2001 08:16 AM
Re: Search all text files for a string
find . -type f -print -exec grep
search for a string inside files.
HTH
Paula
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-18-2001 08:50 AM
тАО12-18-2001 08:50 AM
Re: Search all text files for a string
In the end I simply resorted to using "grep -l" to print the file name rather than the matching lines. ie:
# find / -type f | xargs grep -l "string" > /tmp/log 2>&1
That way you just get a list of files that contain the match and you can decide which ones are binary and which ones arent.
Regards,
Steve
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-18-2001 01:59 PM
тАО12-18-2001 01:59 PM
Re: Search all text files for a string
Thanks for the replies. They are appreciated! Just wanted you to know I'm still testing these suggestions and haven't forgotten to assign points.
Darrell
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-19-2001 01:06 AM
тАО12-19-2001 01:06 AM
Re: Search all text files for a string
Consider the script lookForString.sh with two arguments : arg1 is the directory under which you are looking for string and arg2 is the string you are looking for.
find $1 -type f | while read File
do
file ${File} | grep -q -i "ascii text"
rFile=$?
if [ ${rFile} -eq 0 ]
then
grep -q -i "Your target string" ${File}
rString=$?
if [ ${rString} -eq 0 ]
then
echo "### File '${File}' contains string '$2'"
fi
fi
done
Magdi
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-19-2001 10:33 AM
тАО12-19-2001 10:33 AM
Re: Search all text files for a string
Thanks for everyone's help. Wodisch sums up my testing rather well: "this is not so simple, really!"
My problem was also due to a database upgrade (Progress 83b to 83d). Why do people hard-code something like a version number in scripts? Haven't they ever heard of symlinks? I just finished going thru the resulting list of 415 files that contained 83b and changing them so that they don't reference a version number any more! Thankfully I won't have this problem again!
I tried each of the suggestions above (with some minor, I think, changes). See the attachment for what I ran, timex output, and problems noted with each. Note that each of the 4 scripts in the attachment found the same number of files if I limited my find to /etc. When finding from "/" is where the miscellaneous problems arose.
Steven's suggestion to use "grep -l" to list the filenames without the line that matched was a big help. Filenames were what I really wanted anyway and solves the problem with some lines of my output being too long for vi, awk, etc.
I think another problem is using xargs with the large number of files returned by find. Perhaps this can be avoided by coding a loop on all mounted filesystems and using the -xdev arg of find. I didn't try that. In any event, that problem didn't seem to happen in the following (which is what worked best for me):
find / -type f -exec grep -l 83b {} \; | xargs file | grep text | awk -F: '{print $1}'
Thanks again,
Darrell