5

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

Community
  • 1
  • 1
vkopio
  • 914
  • 1
  • 11
  • 28
  • would it be sufficient to grep for something on the line you want instead of referencing it via line number? – burnettk May 02 '17 at 15:39
  • 3
    Does `file.split(System.getProperty("line.separator"))[n]` do it? – tim_yates May 02 '17 at 16:12
  • Grep is not sufficient as there are many lines with identical boilerplate. What I want to get out of the file is a percentage number. I'll test the above tomorrow. – vkopio May 03 '17 at 10:44
  • fyi this is a jenkins bug: https://issues.jenkins-ci.org/browse/JENKINS-46988 – GottZ Jul 03 '19 at 14:40

2 Answers2

12

Just going to leave documented here, but you can also use readLines().

def file = readFile location
def lines = file.readLines()

From this other question

Thiago
  • 864
  • 1
  • 9
  • 16
7

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]
vkopio
  • 914
  • 1
  • 11
  • 28