I am trying to assign the result of a sed command to a variable in bash, but I am unable to escape everything corrrectly (probably just due to my lack of knowledge in bash), I have tried:
hash_in_podfile=$( sed -rn 's/^ *pod [\'\"]XXX["\'],.*:commit *=> *["\']([^\'"]*)["\'].*$/\1/p' ${PODS_PODFILE_DIR_PATH}/Podfile )
but I am getting
bash_playground.sh: line 9: unexpected EOF while looking for matching `''
UPDATED SCRIPT
This is the script I am using updated with the code from the answer. Only the path and the comment have changed:
#!\bin\sh
PODS_PODFILE_DIR_PATH='/Users/path/to/file'
# just a comment
hash_in_podfile=$(sed -rnf - <<\! -- "${PODS_PODFILE_DIR_PATH}/Podfile"
s/^ *pod ['"]XXX["'],.*:commit *=> *["']([^'"]*)["'].*$/\1/p
!
)
echo $hash_in_podfile
executed with sh script_name.sh
sh --version yields:
GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin20) Copyright (C) 2007 Free Software Foundation, Inc.
On execution I get:
script_name.sh: line 6: unexpected EOF while looking for matching `"'
script_name.sh: line 10: syntax error: unexpected end of file
$()directly in the console I get a hexadecimal hash. The input is a file that contains a line that looks like this :pod 'XXX', :git => '[email protected]:ZZZ/AAA.git', :commit => '23401689216a1905bc137df'. The output in this case would be23401689216a1905bc137dfsed -E 's/^pod.*>..(.*)./\1/'for the match.