Ask a Question

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

SOLVED
LucN
Occasional Contributor

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 ! 

7 REPLIES 7
nmrao
Champion Level 3

Sample content of the file that you are talking about?


Regards,
Rao.
nmrao
Champion Level 3

What is the use case by the way.


Regards,
Rao.
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
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
if this helped answer the post, could you please mark it as 'solved'? Also if you consider whether the title of your post is relevant? Perhaps if the post is solved, it might make sense to update the Subject header field of the post to something more descriptive? This will help people when searching for problems. Ta
LucN
Occasional Contributor

Hello, 

 

But yes, it's indeed the same and that's why I put it. 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

nmrao
Champion Level 3

Not sure of the use case (earlier requested for the same, no reply).
Any ways, the above approach does not seem to be correct.
And the log files roll away to different files based on file size that is configured.


Regards,
Rao.
LucN
Occasional Contributor

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 ! 

cancel
Showing results for 
Search instead for 
Did you mean: