1

I have a variable which has content like

----- File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s ----------------------------------|---------|----------|---------|---------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- All files | 36.95 | 20.87 | 24.

PS: The output I mentioned is actually pretty long and I trimmed it for the sake of posting here. I found out by trial and error that All files <> | was between position 970-990

I am trying to extract the value associated with All files i.e 36.95. I am able to use cut to get the substring like this

TOTAL="$(cut -c 975-990 <<< $RES)"

But I am looking for better approach for example using a regex pattern, I saw patterns like grep and awk but I am not able to formulate it.

7
  • 1
    The cut code you show cannot be producing an output of "36.95" with the content you show (it would be cut -c 499-504). What does it mean for a value to be "associated with All files"? Do you mean the |-delimited field following it? Commented Jun 20, 2022 at 0:44
  • It is actually a pretty long output, I happened to trim it out. Commented Jun 20, 2022 at 0:51
  • 2
    "36.95" has 5 characters, not 930, so it still couldn't be correct. You have left out important information for creating a robust solution. Commented Jun 20, 2022 at 0:55
  • I am trying to gather the test coverage from an npm test command Commented Jun 20, 2022 at 1:06
  • 1
    Given I now notice you use $RES unquoted in your question I suspect you're using it unquoted to show it's contents in your question, echo $RES, and it in fact DOES contain newlines but by not quoting the variable you're stripping them, Please edit your question to show the output of echo "$RES" (i.e. with quotes to retain the original content). Commented Jun 20, 2022 at 10:10

4 Answers 4

2

And since you mentioned it - here an awk alternative:

total=$(awk -F'|' '{print $12}'<<<"${res}")

Note: all uppercase variable names are (more by convention than anything) reserved for the system.

Sign up to request clarification or add additional context in comments.

1 Comment

It's more than convention. For example: The name space of environment variable names containing lowercase letters is reserved for applications. Applications can define any environment variables with names from this name space without modifying the behavior of the standard utilities. - implying use of ones not containing lowercase may affect operation of "utilities in the Shell and Utilities volume of POSIX.1-2017"
1

If you're not limiting yourself to cut, this is a possible answer using sed that looks for the All files | ... | pattern and grabs the text from there.

TOTAL="$(sed -E 's/^.* All files \| ([^ ]+) \| .*$/\1/' <<< $RES)"

Here's a tiny script with that idea put to use:

#!/bin/bash

RES="----- File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s ----------------------------------|---------|----------|---------|---------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- All files | 36.95 | 20.87 | 24."
TOTAL="$(sed -E 's/^.* All files \| ([^ ]+) \| .*$/\1/' <<< $RES)"
echo "$TOTAL"

Running that:

$ ./test.sh
36.95

4 Comments

I like this idea, the only problem is that it does not echo anything, I beleive that it is undefined
I'm not sure I follow. The sed command will output the relevant output, and that will be captured in the TOTAL variable. If you want to echo that, just echo "$TOTAL"?
I think I am wrong, just plain echoing it returns the value 36.95, however if I assign it to a variable and echo it does not print anything
I have been trying to run the above command in my gitlab shell which seems to use gnu runner which is why although this command works like a charm on my local(macbook) it just wont execute it and instead returns me the value of $RES from the above answer instead of TOTAL. What can I do ?
1

Since RES is a shell variable, you could do something like this:

TOTAL="${RES#*All Files}"
TOTAL="${TOTAL#*|}"
TOTAL="${TOTAL%|*}"
TOTAL="${TOTAL// /}"
  • The first line removes everything up to and including the words All Files
  • Since it is unsure how many spaces are after the words All Files, we remove again everything upto the first <pipe>-character
  • Finally, we remove everything after the first <pipe>-character.
  • As a final step, we delete all spaces

Comments

1

I don't believe your RES variable contents are all on 1 line, I think you're making it look that way by using echo $RES instead of echo "$RES" and what it really contains looks more like:

$ echo "$RES"
----------
  File    | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|------------------
All files |  36.95  |   20.87  | 24.

in which case all you need is:

$ echo "$RES" | awk -F' *[|] *' '/All files/{print $2}'
36.95

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.