1

My current value of $OUTPUT is:

Updating (122 files)

In this case, I want to capture the 122 and assign it to a variable.

I was thinking of using a regex like \d* to capture the number, but I'm unclear on the syntax for working with regular expressions in bash.`

3 Answers 3

2

I would use sed:

OUTPUT=$(echo $OUTPUT| sed 's/[^0-9]//g')

to delete all non-digit characters for instance.

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

Comments

2

Just use parameter expansion:

$ output='Updating (122 files)'
$ output="${output##*(}"
$ output="${output%% *}"
$ echo "$output"
122

You can tweak the pattern if you need more flexible matching.

Comments

2

Using regular expressions,

$ regex="Updating \(([[:digit:]]+) files\)"
$ [[ $OUTPUT =~ $regex ]]
$ echo ${BASH_REMATCH[1]}
122

In this case, there is enough context that you could simplify the regular expression to

regex="Updating \((.*) files\)"

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.