-1

I want to capture the first occurence value of a given text field by reading the whole file. How to achieve this in shell:

Input line:

<coverage lines-valid="3966"  lines-covered="2094"  line-rate="0.528"  branches-valid="5864"  branches-covered="1400"  branch-rate="0.23870000000000002"  timestamp="1532416636656" complexity="0" version="0.1">

I need to get value of first occurence of line-rate field.

Nic3500
  • 8,144
  • 10
  • 29
  • 40

1 Answers1

1

Since you mention you read a whole file, first grep the lines with "line-rate" in them:

grep "line-rate" input-file

Then you want only the first occurence:

grep "line-rate" input-file | head -1

And finally you want the "line-rate" value:

grep "line-rate" input-file | head -1 | sed 's/.*line-rate="\([0-9.]\+\)" .*/\1/'

Explanation of the sed:

  • .* anything
  • followed by line-rate="
  • [0-9.]: characters 0 to 9 and the dot
  • \+: at least one of the preceding, no maximum
  • \( \): delimit what I want to keep, here the numbers
  • .*: everything that follows
  • \1: replaced by what was enclosed by \( \)

So here it will take any number (with dot or not) that follows line-rate=", and ends with ".

Obviously, replace input_file by your file name.

This was done using bash, using GNU sed.

Nic3500
  • 8,144
  • 10
  • 29
  • 40