Software Development

How to search a file and print lines containing a specific string using Groovy scripting?

JE Asked by Jeffrey Parker · 22-09-2025
0 upvotes 12,092 views 0 comments
The question

I am writing a Jenkins pipeline script and need to parse a large log file to find specific error codes. I want to iterate through the file and print only the lines that contain a specific substring, similar to how 'grep' works in Linux. Is there a concise, "Groovy-way" to do this using closures or the eachLine method without writing a complex loop? Also, how should I handle case sensitivity during the search?

3 answers

0
KI
Answered on 24-09-2025

Groovy makes this incredibly simple with the eachLine method available on File objects. You can use a closure to check each line: new File('path/to/file.log').eachLine { line -> if (line.contains('searchString')) println line }. If you need to handle case-insensitivity, you can convert the line to lowercase using line.toLowerCase().contains('searchstring'). For more advanced filtering, Groovy also supports the findAll method on collections, which allows you to use a regular expression. This is far more efficient than traditional Java BufferedReader loops because Groovy handles the resource management and closing of the file stream automatically behind the scenes, preventing memory leaks in your automation environment.

0
ST
Answered on 26-09-2025

Does this approach work the same way if I'm trying to filter a list of strings stored in a variable rather than reading directly from a physical file? I'm curious if the performance holds up when dealing with massive arrays of console output in a Groovy-based deployment tool.

CH 27-09-2025

Steven, if you're working with a list, you can use the grep method which is natively available in Groovy. For example, def matches = myList.grep(~/.*specificString.*/). This uses a Regex pattern and is extremely fast. For very large datasets, using .findAll { it =~ /regex/ } is also highly performant. The beauty of Groovy is that these methods are highly optimized for collection processing, making it a favorite for DevOps engineers who need to filter thousands of lines of log data in seconds.

0
LA
Answered on 30-09-2025

The easiest way is using eachLine. It’s short, readable, and handles the file opening/closing for you. It’s perfect for quick Jenkinsfile utility methods.

JE 01-10-2025

I agree with Laura. I use this exact snippet in our production pipelines to filter out "WARNING" messages from our build logs. It keeps the console output clean and focuses only on the critical "ERROR" strings we actually need to see.

Share your thoughts

Your email address will not be published. Required fields are marked (*)

Professional Counselling Session

Still have questions?
Schedule a free counselling session

Our experts are ready to help you with any questions about courses, admissions, or career paths. Get personalized guidance from industry professionals.

Request a Call Back

Search Online

We Accept

We Accept

Follow Us

"PMI®", "PMBOK®", "PMP®", "CAPM®" and "PMI-ACP®" are registered marks of the Project Management Institute, Inc. | "CSM", "CST" are Registered Trade Marks of The Scrum Alliance, USA. | COBIT® is a trademark of ISACA® registered in the United States and other countries.

Book Free Session