ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
In the script I got for checking sms from online someone had written a regex but it is not right for my new modem as it only searched 0-9 and for some reason when trying last night the modem number was into the 20s. It back down at 0 today. I think it was because I was pugging and unplugging quite a bit. So I would like to regex double digits.
One I cobbled together however it produces 3 outputs:
Code:
MODEMNO=$(mmcli -L|grep -Eo '[[:digit:]]+')
The above produces several outputs, which would, if using the example output from above, be:
Code:
1
0
0
I only want the middle number. 0 in this case but factor for any any number into the double digits. It was late last night and ran out of steam. I realized I could probably simplify with using a regex which uses other parts of the string but only picking the number part. This would have been rudimentary a while ago but I have forgotten it!
I gather I could also use grouping with the existing one and choose the middle but, again, forgotten how to do groupings.
Also I read that grep is not required at all and the regex could be done right in a bash variable. I would be interested in knowing that too and just generally the simplest way but I my attention was waning last night and so stopped and posting where I got up to.
I haven't coded in about a year and it was embarrassing to notice how rusty I was. Then again bash and regex were only periphery to my main language of python.
Last edited by linuxuser371038; 05-19-2024 at 12:33 PM.
@pan64, you didn't see the text after the modem/0 ?
And what if it would contain another / character?
Also the unquoted $(mmcli -L) tries filename generation (disable with set -f unless you know that nmcli -L never produces a wildcard).
You are right, I would not accept that too. mmcli -L will list modems, () will split the output, and actually the script will just cut everything before the last / (of the first word). Using the sample by OP it works, in general it is not really safe.
If I need to vote probably I would prefer sed. Bash is not really good at regex.
Lately seeing that the following text is separated by whitespace, I have edited my post.
Only my point with the filename generation is valid.
I think bash uses the Posix ERE engine, while sed -E uses the ERE engine with some GNU extensions (like \s).
Bash stores the BASH_REMATCH[1] after a match-only, while sed can only use the \1 immediately, within the s command.
Last edited by MadeInGermany; 05-21-2024 at 05:29 AM.
mmcli -L # this is your command
$( ) # will execute the command and returns the output of it
( content*) # will split the content into an array (which is actually the output of your command)
answer=(..) # assign the array to the variable "answer" (or b)
answer="${b[0]}" # assign the first element of array b to the variable answer (which is now not an array)
${answer##*/} # remove everything from the string until the last / in the variable answer
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.