- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Search entire system for word(s) in all text files
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
Forums
Discussions
Discussions
Discussions
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
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
08-19-2003 10:01 AM
08-19-2003 10:01 AM
I need to search my entire system for a number of key words. I only want to search text files. When I run the following script, it runs for a while, but errors out with grep running out of memory.
I separated the grep statements into a few key words each statement.
Any help would be greatly appreciated.
#!/usr/bin/ksh
set -x
> /tmp/out_file1
cd /
for mp in `find . -type d ! -name . -prune | sed -e "s/^..//"`
do
for i in `find /${mp} -type f | xargs file | egrep 'commands|script' | awk -F
'{print $1}'`
do
QUESTION=FALSE
egrep 'devservice|h50app|devpriv' $i > /dev/null
if [ "$?" -eq 0 ]
then
QUESTION=TRUE
fi
egrep 'appswervice|apppriv|s70app' $i > /dev/null
if [ "$?" -eq 0 ]
then
QUESTION=TRUE
fi
egrep 'dbservice|sbpriv|s70db' $i > /dev/null
if [ "$?" -eq 0 ]
then
QUESTION=TRUE
fi
egrep 'syncservic|h50sync|uname' $i > /dev/null
if [ "$?" -eq 0 ]
then
QUESTION=TRUE
fi
egrep 'ftp|rcp|rsh' $i > /dev/null
if [ "$?" -eq 0 ]
then
QUESTION=TRUE
fi
#egrep 'devservice|h50app|devpriv|appservice|apppriv|s70app|dbservice|sbpriv|s70
db|syncservic|h50sync|uname|ftp|rcp|rsh' $i > /dev/null
if [ "$QUESTION" = "TRUE" ]
then
echo $i >> /tmp/out_file1
fi
done
done
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-19-2003 10:18 AM
08-19-2003 10:18 AM
Re: Search entire system for word(s) in all text files
A few comments:
Instead of 'grep'ing for your regular expressions and then invoking 'awk' to print the first field, create a single 'awk' script to do both.
The "for X in `command`" sequences are consuming memory constructing an argument list. You should consider building a temporary file of arguments to be read as input, or use a pipeline.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-19-2003 10:54 AM
08-19-2003 10:54 AM
Re: Search entire system for word(s) in all text files
Thanks for the pointers. How do you make a pipeline for the commands, or can you point me to a resource to find the information.
Thanks again
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-19-2003 10:59 AM
08-19-2003 10:59 AM
Re: Search entire system for word(s) in all text files
Consider:
#!/usr/bin/sh
find /tmp -type f -name "*.log" |
while read LINE
do
echo "processing ${LINE}"
done
exit 0
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-19-2003 11:04 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-19-2003 11:53 AM
08-19-2003 11:53 AM
Re: Search entire system for word(s) in all text files
Thank you for your response. Because I am not familiar with perl... do you mind go over how your script functions for me. Thanks again for all your help.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-19-2003 12:58 PM
08-19-2003 12:58 PM
Re: Search entire system for word(s) in all text files
# perl
?? use standard module File::Find
-MFile::Find
?? end all print's in perl with a newline
-l
?? and process the following expression
-e
The expression broke down in multiple lines:
?? Undefine the input record seprarator (default newline), so a read on the file causes the file to be read completely
$/ = undef;
?? Call the find function (use 'man File::Find for the specifics)
find (
?? Pass it two arguments: a subroutine reference to be called on every entry found, and a list of folders to search
sub {
?? I only want *files* (not pipes, folders/directories, or special devices) that are *plain text (not binary), as your original quest states
-f && -T or return;
?? magic open :) put the filename teporary in the arg list, so <> (the read operator) reads from the current file without having to explicitely open it. Very efficient. Remember that <> will read the complete file in one single string, because we undef'ed the input record separator
local @ARGV= ($_);
?? Read one line (the complete file in our case) and match it to pattern
<> =~ /pattern/
It might actually have been better to write this as /pattern/s, but that depends on what kind of pattern you use (see perldoc perlre)
?? If it matches, print the full name of the file it currenty parses, followed by a newline (remember the -l option)
and print $File::Find::name
?? end of closure (subroutine ref)
}
?? and the list of folders/directories to scan
, "/"
)'
HTH Enjoy, have FUN! H.Merijn
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-19-2003 11:31 PM
08-19-2003 11:31 PM
Re: Search entire system for word(s) in all text files
nice find / -type f | xargs grep -il -f kfile
The -i option on grep means ignore case, you may not need this.
Note also that this will search any NFS mounted filesystems, you may not want this. To avoid, see my post in this thread:
http://forums.itrc.hp.com/cm/QuestionAnswer/1,,0x015bcf29a397ca4db25109ab3743467e,00.html
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-20-2003 12:30 AM
08-20-2003 12:30 AM
Re: Search entire system for word(s) in all text files
for i in file1 file2 file3
do
echo "searching for $i"
find . -type f -exec grep -il $i {} \;
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-20-2003 09:42 AM
08-20-2003 09:42 AM
Re: Search entire system for word(s) in all text files
fyi about looping. I know of three ways.
1.
for I in $List
do
echo $I
done
2.
I=10
while [ $I -gt 0 ]
do
echo $I
I=`expr $I -1`
done
3. and here is the file loop.
exec 3
do
echo $Line
done
3<&-
steve
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-21-2003 01:37 AM
08-21-2003 01:37 AM
Re: Search entire system for word(s) in all text files
want t o search and name it as keyfile.
for i in `cat keyfile`
do
find . -name "*.txt" -exec grep $i {} \; -print 2>/dev/null
done
If you want to search all regular files then
for i in `cat keyfile`
do
find . -type f -exec grep $i {} \; -print 2>/dev/null
done
if you still want to save the no of finds,
find . -type f > filelist
for i in `cat filelist`
do
for j in `cat keyfile`
do
grep $j $i
done
done
Depending on what you want as output, use the return value of grep and print accordingly
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-21-2003 05:20 AM
08-21-2003 05:20 AM
Re: Search entire system for word(s) in all text files
try changing the line:
`for i in `find ./${mp} -type f | xargs file |egrep 'commands|script' | awk -F '{print $1}'`
to:
`for i in `find ./${mp} -type f | xargs file | awk -F: '$2 ~ /text$/ {print $1}'`
regards,
John K.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-21-2003 05:46 AM
08-21-2003 05:46 AM
Re: Search entire system for word(s) in all text files
#!/bin/ksh
DIR=$1
TEXT=$2
find $DIR -type f -exec file {} \;|
grep text|
while read line
do
fname=`echo $line|cut -f1 -d':'`
grep "$TEXT" "$fname" /dev/null
done
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-22-2003 06:59 AM
08-22-2003 06:59 AM
Re: Search entire system for word(s) in all text files
Thanks for the break down on the perl script.
I ran the line and it does seem to work, only I am getting an "out of memory" error shortly into the run. Any ideas or thoughts on this.
Thanks again for all your help.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-22-2003 07:08 AM
08-22-2003 07:08 AM
Re: Search entire system for word(s) in all text files
And don't forget to award all those fine answers above with points. Bear to heart that none of the above answers will work correctly with NFS (well, some might behave, but don't expect any speed :)
Enjoy, have FUN! H.Merijn
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-26-2003 06:33 AM
08-26-2003 06:33 AM
Re: Search entire system for word(s) in all text files
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-26-2003 06:56 AM
08-26-2003 06:56 AM
Re: Search entire system for word(s) in all text files
OTOH I think that my snippet should run OK on any perl5.
FWIW if you wanna upgrade, I've got a bunch of precompiled binaries laying around on https://www.beepz.com/personal/merijn/ or http://www.cmve.net/~merijn/
Enjoy, have FUN! H.Merijn