- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Advanced find of text in files with formatted outp...
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
тАО09-27-2004 03:27 AM
тАО09-27-2004 03:27 AM
Advanced find of text in files with formatted output
I want to search a directory structure for files that contain a text pattern, and for each line containing that pattern print out the name of the file and the line containing the text. I have found this perl snippet:
perl -MFile::Find -le$/=undef;find(sub{-f&&-T or return;local@ARGV=($_);<>=~/pattern/ and print$File::Find::name},"/opt/")
which is great for finding the pattern, but I can't figure out how to print the line of text as well. I also guess that this could be done with awk, but I'm not that great with awk.
Thanks!
James
- Tags:
- Perl
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-27-2004 03:33 AM
тАО09-27-2004 03:33 AM
Re: Advanced find of text in files with formatted output
perl -MFile::Find -le$/=undef;find(sub{-f&&-T or return;local@ARGV=($_);<>=~/(.*pattern.*)/ and print$File::Find::name . " : " . $1},"/opt/")
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-27-2004 03:34 AM
тАО09-27-2004 03:34 AM
Re: Advanced find of text in files with formatted output
Unless you want to stick to perl, I think find and grep would do what you want:
"find /example /new/example -exec grep 'Where are you' {} \;" (from the man page, without the -l)
Pete
Pete
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-27-2004 04:07 AM
тАО09-27-2004 04:07 AM
Re: Advanced find of text in files with formatted output
/home/user/file.txt : line containing pattern
Tried to do it simply in scripts, but couldn't figure it out...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-27-2004 04:16 AM
тАО09-27-2004 04:16 AM
Re: Advanced find of text in files with formatted output
grep isn't a great tool to 'grep' for strings. Just for the heck of it, xargs with find may give you what you want.
find /somewhere -type f |xargs grep "string"
-Sri
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-27-2004 04:45 AM
тАО09-27-2004 04:45 AM
Re: Advanced find of text in files with formatted output
#find
To add line number where string was found:
#find
Rgds.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-27-2004 04:58 AM
тАО09-27-2004 04:58 AM
Re: Advanced find of text in files with formatted output
grep -n 'pattern' `find
Note: Some files are being object files so that grep on that file will make unneed results there on session there.
Check the file type with file command.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-27-2004 05:09 AM
тАО09-27-2004 05:09 AM
Re: Advanced find of text in files with formatted output
#!/usr/bin/sh
# test.sh
pth="$1"
pattern="$2"
for file in `find $pth -type f name "*"`
do
if [[ $(file $file | grep -q "executable") -ne 0 ]]
then
grep -n "$pattern" $file
fi
done
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-28-2004 12:07 AM
тАО09-28-2004 12:07 AM
Re: Advanced find of text in files with formatted output
1. cd MYDIRECTORY
2. find . -type f -print > bigfile
3. vi bigfile
remove logs, proxy pages, other bogus areas you don't want to bother looking at. Remove any OUTPUT FILE or you'll have an infinite loop.
4. cat bigfile | \
while read name;do
case "$(file $name)" in
*text* ) grep -Ei "string" $name 2>>/dev/null ;;
esac
done
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-30-2004 04:38 AM
тАО09-30-2004 04:38 AM
Re: Advanced find of text in files with formatted output
grep "pattern" $(find path -type f | perl -lne 'print if -T;' | xargs grep -l "pattern")
NOTE: path is the top of the subtree you want
to consider
(the perl subscript will consider only text files)