1

I have elaborated an awk command which gathers the output of a shell:

val=$(./cli.sh --file=scripts/monitor.sh | awk '/result/{print $3}')
echo $val

Returns:

"75"

As you can see the output is correctly returned, however I'd need to cast it to integer. Is there an awk function to do it?

1

2 Answers 2

2

You can use gsub() to remove the quotes:

$ echo '"3"' | awk '{gsub("\"", "", $1); print}'
3

In your case, this should make:

val=$(./cli.sh ... | awk '/result/{gsub("\"", "", $3); print $3}')

Test

Sample file:

$ cat a
returns a b "75"
returns b c "33"
returns d e 7
hello z z 7

And let's run it:

$ awk '/return/ {gsub("\"", "", $4); print $4}' a
75
33
7
Sign up to request clarification or add additional context in comments.

Comments

0

Try:

val=$(./cli.sh --file=scripts/monitor.sh | awk '/result/{print substr($3, 2, length($2)-1)}')
echo $val

2 Comments

Thanks. However it returns 7 instead of 75.
This is the case with example from OP? or OP can just remove "" from the output very simple.

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.