[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

search text in files and grab only selected text

Posted on 2004-11-22
5
Medium Priority
?
175 Views
Last Modified: 2010-04-01
i want to search text in files text.txt..
i only need to grab text start with TEXT and the text below untill 18th lines. the code should grab all 18 lines of text.. is it possible?

TEXT  
  8
1
  6
CONTINUOUS
 62
3
 10
3.8291557E+002
 20
4.0378163E+001
 40
2.0
  1
APPROVED
 41
5.0E-001
  0




### text.txt
LINE
  8
1
  6
CONTINUOUS
 62
6
 10
3.9918501E+002
 20
1.6406581E+001
 11
3.9918501E+002
 21
2.8280413E+002
  0
TEXT
  8
1
  6
CONTINUOUS
 62
3
 10
3.8291557E+002
 20
4.0378163E+001
 40
2.0
  1
APPROVED
 41
5.0E-001
  0
LINE
  8
0
  6
CONTINUOUS
 62
0
 10
-1.0
 20
1.66667E-001
 11
0.0
 21
0.0
  0
ENDBLK
  0
BLOCK
  2
WEDGE
 10
0.0
 20
0.0
 70
0
  0
TEXT
  8
1
  6
CONTINUOUS
 62
3
 10
3.20272E+002
 20
4.660511E+001
 40
3.0
  1
MM
 41
6.0E-001
  0
0
Comment
Question by:nazon
  • 2
  • 2
5 Comments
 
LVL 2

Expert Comment

by:sujithkumars123
ID: 12643151
I hope your problem will be solved if you are reading the file using BufferedReader.
Read line by line and check whether the string starts with TEXT.
ie
BufferedReader br = new BufferedReader(new FileReader("filename"));
br.readLine().startsWith("TEXT)
0
 

Author Comment

by:nazon
ID: 12650642
now i used BufferedReader but i dont know how to only select the 18 lines..

      out.println("nameFile = "+request.getParameter("nameFile"));
      if(request.getParameter("nameFile") != null)      {
      String line;
      BufferedReader input = new BufferedReader (new FileReader (inF));
      //where inF is a file name string
      while ((line = input.readLine()) != null) {
            // the process to reas text start with TEXT n 18 lines below.
      }
      input.close ();
0
 

Author Comment

by:nazon
ID: 12650821
found error on

      while ((line = input.readLine().startsWith("TEXT")) != null) {
            out.println("<br>"+line);
      }


C:\Program Files\Apache Software Foundation\Tomcat 5.0\work\Catalina\localhost\WTMIS\org\apache\jsp\PLMS\user\extract_jsp.java:79: incompatible types
found   : boolean
required: java.lang.String
      while ((line = input.readLine().startsWith("TEXT")) != null) {
                                                  ^
1 error
0
 
LVL 2

Expert Comment

by:sujithkumars123
ID: 12654103
out.println("nameFile = "+request.getParameter("nameFile"));
     if(request.getParameter("nameFile") != null)     {
     String line;
     BufferedReader input = new BufferedReader (new FileReader (inF));
     //where inF is a file name string
     while ((line = input.readLine()) != null) {
          // the process to reas text start with TEXT n 18 lines below.
          if (line.startsWith("Text")) {
                 //These are the lines which you need
          }
     }
     input.close ();
0
 

Accepted Solution

by:
or3o earned 200 total points
ID: 12663126
while ((line = input.readLine()) != null) {
      if(line.startsWith("TEXT")){
            int grab=1;
            int grab2=0;                  
      }      
      if( (grab2 >= 0) && (grab2 <= 18) ){
            if(grab==1){      
                  //now u get all 18 lines            
            
            }
            if(grab2==18){
                  grab=0;
            }
      }
}
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This shares a stored procedure to retrieve permissions for a given user on the current database or across all databases on a server.
Moore’s Law has proven itself time and time again since it was first introduced. So what’s next? Will Moore’s law continue to remain relevant, or will new technology take over and bring us the next big advancement in computing?
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question