Operations 4 min read

Processing Multiple Text Files in Jenkins: Shell String Manipulation and Groovy Array Conversion

This article demonstrates how to list fail‑list files on Linux, extract user names via shell string trimming, capture the output in a Jenkins pipeline, convert it into a Groovy array, and send individualized email notifications with the corresponding files attached.

DevOps Engineer
DevOps Engineer
DevOps Engineer
Processing Multiple Text Files in Jenkins: Shell String Manipulation and Groovy Array Conversion

Each article in this series presents a short Jenkins tip with accompanying code examples.

Problem: Send different email notifications from a Jenkins pipeline based on multiple text files on a Linux host, each file representing a different user.

Approach: Use a shell command to list files matching fail-list-*.txt, strip the prefix and suffix to extract usernames, and output them as a space‑separated string. Then, in the Jenkinsfile, capture that string, split it into an array with Groovy, and iterate over the array to send personalized emails.

Shell snippet:

# List all files starting with fail-list and assign to array l
l=$(ls -a fail-list-*)
for f in $l; do
  f=${f#fail-list-}   # remove prefix
  f=${f%.txt}         # remove suffix
  echo $f
done

Result of the shell execution:

$ ls
fail-list-user1.txt  fail-list-user2.txt  fail-list-user3.txt
$ l=$(ls -a fail-list-*) && for f in $l; do f=${f#fail-list-}; f=${f%.txt}; echo $f; done
user1
user2
user3

Groovy snippet in Jenkinsfile:

// Jenkinsfile (simplified)
def owners = sh(script: "l=$(ls -a fail-list-*) && for f in $l; do f=${f#fail-list-}; f=${f%.txt}; echo $f ; done;", returnStdout: true).trim()
if (!owners.isEmpty()) {
    owners.split().each { owner ->
        println "owner is ${owner}"
        email.SendEx([
            'buildStatus'   : currentBuild.currentResult,
            'buildExecutor' : "${owner}",
            'attachment'    : "fail-list-${owner}.txt"
        ])
    }
}

This pipeline extracts the usernames, converts the output into a Groovy array, and sends an email with the corresponding file attached for each user.

The example demonstrates how to combine shell processing with Groovy in Jenkins to achieve flexible, per‑user notifications, and can be adapted for similar requirements.

CI/CDautomationshellGroovyJenkinsEmail
DevOps Engineer
Written by

DevOps Engineer

DevOps engineer, Pythonista and FOSS contributor. Created cpp-linter, commit-check, etc.; contributed to PyPA.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.