1

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(

Dudi Boy
  • 4,551
  • 1
  • 15
  • 30
  • @9769953 probably they are, there are supposed to be so many lines, I just want to parse that one line with file url, and I got no idea how to do it – Данил Кокин Apr 29 '22 at 13:22
  • @9769953 it just prints empty line – Данил Кокин Apr 29 '22 at 13:35
  • 2
    The line `variable=RatioUITest.sh | grep 'Test ...` performs the assignment `variable=RatioUItest.sh` (setting the variable to the literal string "RatioUITest.sh") in a subshell which produce no output and writes that (empty output) to `grep`, which promptly exits since its input stream is empty. All of the output you see is from the initial `xcodebuild` command. It looks like you need a basic shell tutorial. – William Pursell Apr 29 '22 at 13:53

2 Answers2

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
  • Can u please provide me complete bash script to achieve my goal, i'm new to bash scripts. Thank u in advance! – Данил Кокин Apr 29 '22 at 14:16
  • You probably want `[.]xcresult` so the `.` can't match other characters. – Charles Duffy Apr 29 '22 at 17:09
  • @ДанилКокин, generally speaking, Stack Overflow is not a code-writing service. Building a knowledge base that describes how to fix narrow, specific problems is what we're here for; not delivering complete working scripts. – Charles Duffy Apr 29 '22 at 17:09
  • @ДанилКокин Check the edited answer, in your script replace that `cat your_text` part with piping output onto grep. Pipe is this | – Adrián Bíro Apr 29 '22 at 17:10
  • BTW, I would suggest changing `echo $var` to `echo "$var"` (likewise for the other) to avoid providing an example subject to the bugs described in [I just assigned a variable. Why does `echo $variable` show something else?](https://stackoverflow.com/questions/29378566/i-just-assigned-a-variable-but-echo-variable-shows-something-else) – Charles Duffy Apr 29 '22 at 17:27
  • @CharlesDuffy This is good point I forgot that. Thanks. – Adrián Bíro Apr 29 '22 at 17:52
0

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}')
Dudi Boy
  • 4,551
  • 1
  • 15
  • 30