0

I'm looking forward to get a string from an cmd output (ID of tmutil destinationinfo).

I have tested this regex with https://regex101.com/r/aI3yH2/1:

/^.*?Name.+?backup02\nKind.+?\nID?\s+:\s([A-Z0-9\-]+)/s

Looks like it's working, I'm searching for backup01, backup02, ...

But how can I grab the ID to a variable within a shell script?

Output is like this:

====================================================
Name          : backup02
Kind          : Local
Mount Point   : /Volumes/ext_backup01_02
ID            : BC51C9FA-DE8A-4BA3-B23E-AEC10E9E9F69
> ==================================================
Name          : backup01
Kind          : Network
URL           : afp://[email protected]/backup01
Mount Point   : /Volumes/backup01
ID            : 009B3736-61C5-4996-B9BC-A5230BED7961
====================================================
Name          : backup03
Kind          : Network
URL           : afp://[email protected]/backup03
Mount Point   : /Volumes/backup01
ID            : 1E9A3734-33D6-1316-C9B4-B143DA35D9F2

I really don't know how to handle this. Sample code 100% not working:

#!/bin/bash
ID=$(tmutil destinationinfo | sed -E '/^.*?Name.+?backup02\nKind.+?\nID?\s+:\s([A-Z0-9\-]+)/s')
echo $ID

Thanks!

1 Answer 1

1

sed (and grep, which would be the other popular regex matcher) are line-oriented; they process one line at a time. So multiline patterns won't work.

You can get sed to accumulate lines, but it's not very pretty. If you did that, you would find that the regex dialect recognized by sed is quite different from the one you are using in the online tool. In particular, sed doesn't implement non-greedy match.

You can probably do better with awk:

awk -F '[[:space:]]*:[[:space:]]*' \
    '$1 == "Name" && $2 == "backup02" { this_one = 1 }
     this_one && $1 == "ID" { print $2; exit }'
Sign up to request clarification or add additional context in comments.

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.