2

The title doesn't give the question justice. I'm writing a shell script, for use in conky, to get the status of my mouse and keyboard's battery. Previously, I used upower to do this:

dev=`upower -e | grep "mouse_"`
upower -i $dev | grep 'state' | cut '-c26-36'

As you can see, I used the cut command to trim the result as it's position never changed.

However, today I found a bug in upower. It cannot see my mouse being charged. So, I have installed solaar from github which does display the correct status. But I'm not sure how to trim the result:

solaar show mouse | grep 'Battery'
# outputs "Battery: N/A, recharging."

I cannot use cut anymore because the segment 'N/A' has an arbitrary length.

Ideally, I'd like the script to output any word which follows the first comma.

How can I do this?

Twifty
  • 245

2 Answers2

3

With a single grep:

solaar show mouse | grep -oP 'Battery.*, \K.*'
2

This should do:

solaar show mouse | grep 'Battery' | sed -r 's/^.*, //'

^: delimiter, matches the start of the string; .: matches any character; *: matches any number of occurencies (including 0) of the previous character; ,: matches ,; : matches

kos
  • 41,268