Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / August 2006

Tip: Looking for answers? Try searching our database.

Java Runtime Issue / Text File Searching

Thread view: 
rajatag - 14 Aug 2006 20:39 GMT
Hi folks,

Here's a tricky one (I think):

I need to search for words within a file. To do this, I thought of
using grep and executing from Runtime.getRuntime() instead of writing
something in Java.

Now, when I run the following in linux:

grep -c -x -F 'HOT' filetosearch.txt

The program prints out 1 which is correct as the file specified above
has just one occurrence of the word HOT on a single line. However, when
I put the same thing within Java like this:

Runtime rt = Runtime.getRuntime();
Process pt = rt.exec("grep -c -x -F 'HOT' filetosearch.txt");

Then the Java application always gets a 0 count.

Any ideas why? Also, is there any better way to do searching of text
files within java?
Gordon Beaton - 14 Aug 2006 20:49 GMT
> Runtime rt = Runtime.getRuntime();
> Process pt = rt.exec("grep -c -x -F 'HOT' filetosearch.txt");
[quoted text clipped - 3 lines]
> Any ideas why? Also, is there any better way to do searching of text
> files within java?

When you run the command from a shell, it removes the single quotes
from the search string and they aren't ever seen by grep.

When you run the command from Java, there is no shell and grep is
looking for the 5-characters 'HOT' (i.e. including the 2 single
quotes), which presumably isn't in your file.

/gordon

Signature

[ don't email me support questions or followups ]
g o r d o n  +  n e w s  @  b a l d e r 1 3 . s e

rajatag - 15 Aug 2006 13:57 GMT
Thanks! I removed the quotes and it started to work fine!


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.