- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Find Command to search only 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
06-12-2007 06:27 AM
06-12-2007 06:27 AM
Find Command to search only text files
Need a help in getting a command or a script
To find all Text files /shell scripts in a file system containing a string "word".
Your help appreciated .
Thanks,
BL .
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-12-2007 06:42 AM
06-12-2007 06:42 AM
Re: Find Command to search only text files
Here's a pure shell approach:
# cat .findit
#!/usr/bin/sh
typeset DIR=$1
typeset PAT=$2
find ${DIR} -xdev -type f | while read FILE
do
[ `file ${FILE} | grep -c ascii` -eq 0 ] && continue
grep "${PAT}" ${FILE} /dev/null
done
exit 0
...run as .findit /path_to_search pattern_to_find
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-12-2007 06:55 AM
06-12-2007 06:55 AM
Re: Find Command to search only text files
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-12-2007 06:55 AM
06-12-2007 06:55 AM
Re: Find Command to search only text files
I already tried type f, it lists jar files etc .
Thanks,
BL
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-12-2007 07:00 AM
06-12-2007 07:00 AM
Re: Find Command to search only text files
> I already tried type f, it lists jar files etc .
Let's not confuse things. Using '-type f' with find limits that which is returned to *files* as opposed to *directories*.
The shell script I suggested skips *binary* files and examines files of type *text*.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-12-2007 07:06 AM
06-12-2007 07:06 AM
Re: Find Command to search only text files
Here's a modified version of JRF's script:
#!/usr/bin/sh
typeset DIR=$1
typeset PAT=$2
find ${DIR} -xdev -type f | while read FILE
do
$(file ${FILE} | cut -d: -f2- | grep -q "text") || continue
grep -l "${PAT}" ${FILE}
done
exit 0
It matches on "text", which means files the 'file' command claims are "ascii text", "awk program text", "c program text", "commands text", "English text", etc. are considered for further investigation by 'grep'. It also filters out the filename from the results of the 'file' command, so that filenames containing "text" do not cause false positives.
PCS
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-12-2007 08:33 AM
06-12-2007 08:33 AM
Re: Find Command to search only text files
Use it like this:
cd /xxx/yyy/zzz # desired starting directory
findgrep.pl Huey Louie Dewey
It will then start a recursive descent from your CWD looking for regular files that are text files and then checking to see if "Huey", "Louie", or "Dewey" occurs anywhere. If so, the file name is printed. Perl's -T test essentially does everything the file xxx | grep "ascii" does.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-12-2007 08:54 AM
06-12-2007 08:54 AM
Re: Find Command to search only text files
there is some thing minor missing in both of these , giving errors .Will you please execute it and see.
Thanks,
BL.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-12-2007 09:10 AM
06-12-2007 09:10 AM
Re: Find Command to search only text files
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-12-2007 05:57 PM
06-12-2007 05:57 PM
Re: Find Command to search only text files
http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=1109119
To solve if Sandman's question is true, you can use grep -w.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-12-2007 11:46 PM
06-12-2007 11:46 PM
Re: Find Command to search only text files
1. in the current directory without including subdirs:
grep -i word $(file *|grep text|cut -d":" -f1)
2. including subdirs:
grep -i word $(find ./ -type f |xargs file|grep text|cut -d ':' -f1)
this run on hpux 11i.
HTH,
Art
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-13-2007 12:14 AM
06-13-2007 12:14 AM
Re: Find Command to search only text files
In the spirit of Perl commandline scripts, this one recursively finds all text 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...