'How to parse output of bash script in CLI
I'm running following bash script
#!/bin/bash
echo Running RatiosUITests
xcodebuild \
test \
-project Ratios.xcodeproj \
-scheme RatiosUITests \
-destination 'platform=iOS Simulator,name=iPhone 13 mini'
variable=RatioUITest.sh | grep 'Test session results, code coverage, and logs:'|cut -f2 -d 'T'
echo $variable
it gives me tons of output:
...
2022-04-29 15:34:17.765 xcodebuild[4331:5066596] [MT] IDETestOperationsObserverDebug: 58.666 elapsed -- Testing started completed.
2022-04-29 15:34:17.765 xcodebuild[4331:5066596] [MT] IDETestOperationsObserverDebug: 0.000 sec, +0.000 sec -- start
2022-04-29 15:34:17.765 xcodebuild[4331:5066596] [MT] IDETestOperationsObserverDebug: 58.666 sec, +58.666 sec -- end
Test session results, code coverage, and logs:
/Users/dmitrijsokolov/Library/Developer/Xcode/DerivedData/Ratios-caehxfsclkooshchojmcmrcadomd/Logs/Test/Test-RatiosUITests-2022.04.29_15-33-18-+0300.xcresult
Failing tests:
RatiosUITests:
...
And I want to store url of the file in a variable
url line I want is on the next line after "Test session results, code coverage, and logs:"
it's that one /Users/dmitrijsokolov/Library/Developer/Xcode/DerivedData/Ratios-caehxfsclkooshchojmcmrcadomd/Logs/Test/Test-RatiosUITests-2022.04.29_15-33-18-+0300.xcresult it's changing all the time so I want to parse that file address.
Code with cut not working properly, please help(
Solution 1:[1]
Just grep it with this:
grep -oP '(?<=start_patern).*(?=end_patern)'
-o, --only-matching
This will select your text, but without this border patterns
adrian@pc:/tmp> cat your_text | grep -oP '(?<=/Users).*(?=.xcresult)'
/dmitrijsokolov/Library/Developer/Xcode/DerivedData/Ratios-caehxfsclkooshchojmcmrcadomd/Logs/Test/Test-RatiosUITests-2022.04.29_15-33-18-+0300
To put it onto variable do
adrian@pc:/tmp> var=$(cat your_text | grep -oP '(?<=/Users).*(?=.xcresult)')
adrian@pc:/tmp> echo $var
/dmitrijsokolov/Library/Developer/Xcode/DerivedData/Ratios-caehxfsclkooshchojmcmrcadomd/Logs/Test/Test-RatiosUITests-2022.04.29_15-33-18-+0300
Then to concatenate parts back
adrian@pc:/tmp> complete_var="/Users${var}.xcresult"
adrian@pc:/tmp> echo $complete_var
/Users/dmitrijsokolov/Library/Developer/Xcode/DerivedData/Ratios-caehxfsclkooshchojmcmrcadomd/Logs/Test/Test-RatiosUITests-2022.04.29_15-33-18-+0300.xcresult
Solution 2:[2]
Suggestion 1
Assuming file name is in same line with Test session results, code coverage, and logs:
variable=$(RatioUITest.sh | awk -F":" '/Test session results, code coverage, and logs:/{print $2}')
Suggestion 2
Assuming file name is in next line after Test session results, code coverage, and logs:
variable=$(RatioUITest.sh | awk '/Test session results, code coverage, and logs:/{line=NR}NR==(line+1){print}')
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Dudi Boy |
