Welcome to Stack Overflow.
I need change all $(\w+) on ENVs with same name. How i can do that with grep/sed/awk/etc?
First, grep
, sed
, and awk
are not "pure shell". Those are commands installed on the system -- just like perl
is. If you can install perl
, I recommend it; it's terribly useful. If not, your next best approach might be copying the source files to a machine where you can install perl
, and running the process as-is, since the end result is to POST the results back to Confluence (i.e., you can POST from off-system).
But, if you really, absolutely cannot use perl
, of course there is a way to do this with other text-processing tools like grep
, sed
, and awk
.
Second, what have you tried so far? Stack Overflow works best when you show us what you've done so far, what works, and in what ways it doesn't work. That helps people focus their responses according to your exact problem and tailored to your current understanding.
Third, here is a script that might guide you. Again, I don't know how much bash
your know, so this might be a bit obscure. It's also certainly not the only way to do this, and quite probably not the best. I highly recommend testing this by running it on a few sample pages and inspecting the output before POST'ing it; tools like diff
or vimdiff
will help a lot here. Then, even when you are ready to actually POST the results, start slowly with a subset, and validate the results before opening up the firehose.
WARNING: the below script breaks under common circumstances
As noted by @jhnc in the comments, the sed
command will fail when the replacement text contains characters that are replacement-metacharacters for sed
(such as '/' in a URL). There is a way to compensate with further script logic, but IMHO down that path lies madness.
My recommendation if perl
cannot be installed on the target machine is my "next best approach" that I mentioned above: copy the input data to a machine where you can run perl
, and run the transformation and POST back to Confluence from there.
But also take a look at the answer from @jhnc, which offers a solution that avoids this weakness in mine.
(I'm placing this warning here instead of deleting my answer, because of the simpler solutions above, and in case this approach serves as a basis for someone who wishes to improve upon it.)
USE THE BELOW APPROACH WITH A GREAT DEAL OF CAUTION
replace-env-params.sh
#!/bin/bash
while IFS= read -r LINE; do
MATCH=$(echo "$LINE" | grep -E '\$[a-zA-Z0-9_]+')
if [[ ! -z "$MATCH" ]]; then
ENVPARAM=$(echo "$LINE" | sed 's/^.*\$\([a-zA-Z0-9_]*\).*$/\1/')
ENVVAL="$ENVPARAM"
REPLACE="${!ENVVAL}"
LINE=$(echo "$LINE" | sed "s/\$[a-zA-Z0-9_]*/$REPLACE/")
fi
echo "$LINE"
done < $1
cat somehtml
<th class='xtr-0-0'>Version name</th>
<td class='xtr-0-1'>$RELEASE_TAG</td>
</tr>
<tr class='xtr-1'>
<th class='xtr-1-0'>Link</th>
<td class='xtr-1-1'>$RELEASE_URL</td>
testing ...
export RELEASE_TAG=11111111
export RELEASE_URL=22222222
./replace-env-params.sh somehtml
<th class='xtr-0-0'>Version name</th>
<td class='xtr-0-1'>11111111</td>
</tr>
<tr class='xtr-1'>
<th class='xtr-1-0'>Link</th>
<td class='xtr-1-1'>22222222</td>
Thus, you can replace:
newPageTemplate=$(perl -p -e 's/\$(\w+)/("$ENV{$1}")/eg' < $CONFLUENCE_PAGE_TEMPLATE)
with
newPageTemplate=$(./replace-env-params.sh $CONFLUENCE_PAGE_TEMPLATE)