Forum Discussion

LucN's avatar
LucN
Occasional Contributor
5 years ago
Solved

How to get the next 15 line after the date with groovy script

Hello eveyrone, I have another problem, 

 

I'm trying to get a number of line after a specific date is found in my file. for example, if I found the date in my File, I would like to write, in another file the 15 lines after it. I start to write something such as : 

 

resultFile.withWriter{ 
writer ->
     readFile.withReader{ reader ->
         while (line = reader.readLine()) {
              if(line.startsWith(dateRequest)){
              writer << "${line}" << System.properties.'line.separator'
               } 
           }
       }
}

 

But I don't know how to add the next 15 line each time ? 

 

What's important for me here is the date before the soap service. What I want to do is :

 

  • for each date when a soap service is send
  • write the 15 line after this date in another file
  • Do it again for other soap serice with the same date

 

Also, the thing is that it's log file which is used by a lot of people so a lot of saop service are not from me and it's not possible to differenciate them expect with the date

 

Do you know how ? Thanx in advance ! 

  • I'll anwser myslef since I solved it : 

     

    		writeFile.withWriter { writer ->
    			// read the file using a reader 
    			readFile.withReader{ reader ->
    				while (line = reader.readLine()) {
    					
    					// if line starts with your comment "'"
    					if(line.contains(threadInfo)){
    						def lineCount = 0
    						while(lineCount  < lineCountMax) {
    							def nextLine = reader.readLine()
    							// to the current line, and writeit in the result file
    							writer << "${nextLine}" << System.properties.'line.separator'
    							lineCount ++;
    						}
    					} 
    				}
    			}
    		}

    thx anyaway ! 

7 Replies

  • nmrao's avatar
    nmrao
    Champion Level 3
    Sample content of the file that you are talking about?
    • LucN's avatar
      LucN
      Occasional Contributor

      Hello, I can't show the file but it's a log file with something like :

      Then for each part that I got for example the same date, I want to write the next 15 line (which represent my webservice) in another file

       

      13/03/2019 15:24:48.424 [http-thread-pool-8080(18)] 
      <?xml version='1.0' encoding='UTF-8'?>
      <S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
      <S:Header>
      <OS_Header xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="os.xsd.v1" xsi:nil="true"/>
      </wsse:UsernameToken>
      </wsse:Security>
      </S:Header>
      <S:Body>

      • richie's avatar
        richie
        Community Hero
        Hi LucN

        The sample you've provided is just the soap wrapper (and so everybody's soap wrapper looks pretty much the same!) and doesn't really provide rao the detail he probably needs to answer this. If he's asking for sample content, he'd need the structure of the xml payload contained within the soapbody tag....which is the bit you haven't included!

        Cheers,

        richie