- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Re: Need help with a script
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
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
тАО07-12-2007 09:57 AM
тАО07-12-2007 09:57 AM
could anyone help?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-12-2007 10:18 AM
тАО07-12-2007 10:18 AM
SolutionHere's a Perl script that recursively finds all *text* (not binary) files in the current directory and reports case-insensitive matches to the pattern passed as an argument
# perl -MFile::Find -we 'find(sub{push @f,$File::Find::name if -f $_ && -T _},".");@a=`grep -i $ARGV[0] @f`;print for sort @a'
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-12-2007 10:21 AM
тАО07-12-2007 10:21 AM
Re: Need help with a script
------------------------------------
#!/usr/bin/sh
typeset PROG=${0}
if [[ ${#} -lt 1 ]]
then
echo "Usage: ${PROG} target_string" >&2
exit 255
fi
typeset TARGET=${1}
shift
typeset FNAME=""
typeset -i GSTAT=0
find . -type f -print | while read FNAME
do
file "${FNAME}" | grep -q -i "ascii"
GSTAT=${?}
if [[ ${GSTAT} -eq 0 ]]
then # it's a text file
grep -q "${TARGET}" "${FNAME}"
GSTAT=${?}
if [[ ${GSTAT} -eq 0 ]]
then
echo "${NAME}"
fi
fi
done
exit 0
-------------------------------
Use it like this:
cd to desired starting directory
findit.sh "my target string" > mylist
It leverages the file command which includes "ascii" in its output of anything that is a text file. Those files are then examined with grep (-q -- quiet; we only care if a terget string is found or not; if found grep sets its exit code to 0).
Another aproach would be to use Perl's Find::File module and use the -T test to determine if a file is a text file.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-13-2007 01:40 AM
тАО07-13-2007 01:40 AM
Re: Need help with a script
Ooops; a slight correction to retain the order of matched lines while still sorting the filenames output:
# perl -MFile::Find -e 'find(sub{push @f,$File::Find::name if -f $_ && -T _},".");@a=sort @a;@a=`grep -i $ARGV[0] @f`;print @a' string_to_match
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-18-2007 09:18 AM
тАО07-18-2007 09:18 AM
Re: Need help with a script
CRIPES!
... @f=sort @f ...
This is the REAL correction to retain the order of matched lines while still sorting the filenames output:
# perl -MFile::Find -e 'find(sub{push @f,$File::Find::name if -f $_ && -T _},".");@f=sort @f;@a=`grep -i $ARGV[0] @f`;print @a' string_to_match
Regards!
...JRF...
- Tags:
- Perl
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-18-2007 11:07 AM
тАО07-18-2007 11:07 AM
Re: Need help with a script
find / -type f -exec grep -l
Example 2:
find / -type f -name *.txt -exec grep -l