I am trying to read a specific line of an html
file in a Jenkins stage
with Groovy
and save its contents to an environment variable. The problem is, File
and readLines()
are not allowed.
I am able to load a file with
env.WORKSPACE = pwd()
def file = readFile "${env.WORKSPACE}/file.html"
Provided in this answer
But how can I access instantly to the contents of line n
? I am using Jenkins 2.32
Just going to leave documented here, but you can also use readLines().
def file = readFile location
def lines = file.readLines()
From this other question
I Tried the suggestion of tim_yates from the comments but System
was also forbidden. What ultimately worked for me was just changing System.getProperty("line.separator")
to new line character "\n"
.
So the full answer was in its simplicity:
file.split("\n")[n]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With