Intro
I have the output of the command xcrun simctl list devices
on a file called simulators.txt
:
-- iOS 13.1 --
iPhone 11 (50E46560-28EE-4689-B015-01E86D9A2C8A) (Shutdown)
-- iOS 14.5 --
iPhone 12 Pro (8F7AF263-396D-4256-803A-937FBC944795) (Booted)
-- iOS 16.2 --
iPhone 12 Pro (21CB340E-A08A-416A-BC28-78E26E42FC04) (Booted)
iPhone 14 Pro (62BA4ADD-7441-4D62-B137-C02A50867817) (Booted)
-- Unavailable: com.apple.CoreSimulator.SimRuntime.iOS-16-0 --
iPhone SE (3rd generation) (809AF9A3-5F67-4083-AF66-4F3B4C7B2138) (Shutdown) (unavailable, runtime profile not found)
My goal is to output this by filtering devices that are Booted
:
* iPhone 12 Pro (iOS 14.5, 8F7AF263-396D-4256-803A-937FBC944795)
* iPhone 12 Pro (iOS 16.2, 21CB340E-A08A-416A-BC28-78E26E42FC04)
* iPhone 14 Pro (iOS 16.2, 62BA4ADD-7441-4D62-B137-C02A50867817)
Note that I need to preserve the model, version and UUId.
Things that I've tried so far
Using sed:
sed -ne 's/^-- iOS \(.*\) --/iOS \1:/p' -ne 's/^[ \t]*\(.*\) (Booted)/ * \1/p' < simulators.txt
I get:
iOS 13.1:
iOS 14.5:
* iPhone 12 Pro (8F7AF263-396D-4256-803A-937FBC944795)
iOS 16.2:
* iPhone 12 Pro (21CB340E-A08A-416A-BC28-78E26E42FC04)
* iPhone 14 Pro (62BA4ADD-7441-4D62-B137-C02A50867817)
I would need to save the capture group from the first match (^-- iOS \(.*\) --
) to a variable and apply it on the second match, but I'm not sure how to do that.
Using bash function
function displaySimulators {
while IFS="" read -r p; do
if [[ "$p" =~ --\ iOS.*\ -- ]]; then
IOS_VERSION=$(echo $p | sed -ne 's/^-- \(.*\) --/\1/p')
else
echo $p | sed -ne 's/^[ \t]*\(.*\) (\(.*\)) (Booted)/* \1 ('"$IOS_VERSION"', \2)/p'
fi
done < simulators.txt
}
Here you can see that I applied the idea of saving the first match to a variable and reusing it when necessary.
The output is:
* iPhone 12 Pro (iOS 14.5, 8F7AF263-396D-4256-803A-937FBC944795)
* iPhone 12 Pro (iOS 16.2, 21CB340E-A08A-416A-BC28-78E26E42FC04)
* iPhone 14 Pro (iOS 16.2, 62BA4ADD-7441-4D62-B137-C02A50867817)
Which is what I need, but then some questions come to mind.
Questions
- How can I make the bash solution better? Are there any good practices or another approach that is faster and cleaner than this?
- How to achieve the same result using just
sed
orawk
?