2

This will be an easy one for most of you I think, but what is the most effective way to parse the modem number and modemmanager folder number from this:

root@5109910:~# mmcli -L
    /org/freedesktop/ModemManager1/Modem/1 [Sierra Wireless Inc.] Sierra Wireless EM7345 4G LTE

The ModemManager1, 1 and [Sierra Wireless Inc.] Sierra Wireless EM7345 4G LTE are dynamic and I want to be able to manipulate the modem in a shell script.

To be more specific, I'd like to generate the following:

MODEM_NUMBER=$(mmcli -L | grep ***PATERN TO PARSE 1 HERE***)

MODEM_DIR=$(mmcli -L | grep ***PATERN TO PARSE ModemManager1 HERE***

MODEM=$(mmcli -L | grep ***PATTERN TO PARSE [Sierra Wireles etc etc HERE***)
jkmartindale
  • 523
  • 2
  • 9
  • 22
Don T Spamme
  • 199
  • 7
  • Which tool is your favorite? Can you solve any part of this problem? – Beta Jul 31 '21 at 02:23
  • I don't have a preference on grep, sed, or awk. Whichever gets the job done. I think my main issue is not understanding regex patterns whatsoever. – Don T Spamme Jul 31 '21 at 02:25
  • 1
    This is easy in sed, but you must learn the rudiments of regex and sed, or the solution will be magical gibberish like `sed 's/ .*//;s|.*/||'` – Beta Jul 31 '21 at 03:14
  • It sure looks like gibberish to me, but magical nonetheless.. Got the "1" thank you! – Don T Spamme Jul 31 '21 at 03:34
  • 1
    Step 1 - do not use the word "pattern" in the context of matching text. See [how-do-i-find-the-text-that-matches-a-pattern](https://stackoverflow.com/questions/65621325/how-do-i-find-the-text-that-matches-a-pattern) to understand the issue then replace the word "pattern" with "string-or-regexp" and "partial-or-full" and "line-or-word" everywhere it occurs in your question so we can help you get the right answer. – Ed Morton Jul 31 '21 at 12:49
  • 1
    @DonTSpamme When you don't have a question about a specific UNIX tool, but just want to know how to do a job _using UNIX tools_, tag `unix` instead of guessing a bunch of specific tools you think someone _might possibly_ use. (An awk question should be _about awk_, a sed question should be _about sed_, etc). – Charles Duffy Jul 31 '21 at 16:48

1 Answers1

2

Using pure bash you can do this in single step i.e. single invocation of mmcli -L command:

IFS='/ ' read -r _ _ _ mm _ mn mt < <(mmcli -L)

# chek variable's content
declare -p mm mn mt

Output:

declare -- mm="ModemManager1"
declare -- mn="1"
declare -- mt="[Sierra Wireless Inc.] Sierra Wireless EM7345 4G LTE"

Details:

  • IFS='/ ': Sets / or space as input field separators
  • read -r _ _ _ mm _ mn mt: Read 4th, 6th and 7th onwards test in variable mm, mn and mt while ignoring rest
  • < <(mmcli -L): Command substitution to invoke mmcli -L and feed it's output to read
anubhava
  • 761,203
  • 64
  • 569
  • 643