Reputation: 13
I am using a MacOSX to create this a bash script, the script checks other files and compares the contents to one another, which creates an incrementing counter. The functions that use the counters, must be executed first in order for the variables to contain the information, therefore it seems that the best option would be inserting the line through the command sed. Is there any way to make it so that I can ignore the literal portion that the single quotes create? This is BSD sed too, not GNU sed. Or if there is an alternative option other than using sed, I'd be interested in knowing how to do it.
CODE:
sed -i '' '3i\
There are a total of ${ALOLANCOUNT} Pokemon in Alola, and you have ${HAVECOUNTER}, and need ${ALOLANNEEDCOUNTER}' $LOGFILE
sed -i '' '3i\
There are a total of ${NATIONALCOUNT} Pokemon in the world, and you need ${NATNEEDCOUNTER}' $LOGFILE
OUTPUT:
There are a total of ${NATIONALCOUNT} Pokemon in the world, and you need ${NATNEEDCOUNTER} There are a total of ${ALOLANCOUNT} Pokemon in Alola, and you have ${HAVECOUNTER}, and need ${ALOLANNEEDCOUNTER}
Upvotes: 1
Views: 831
Reputation: 124648
Variables are not expanded inside '...'
.
Change the single-quotes to double-quotes.
On the other hand,
the \
inside double-quotes will be interpreted to escape the newline that follows it,
but in this case you need it to be a literal \
for the i
command of sed
.
Inside double-quotes you need to write that as \\
.
Putting it together, write like this:
sed -i '' "3i\\
There are a total of ${ALOLANCOUNT} Pokemon in Alola, and you have ${HAVECOUNTER}, and need ${ALOLANNEEDCOUNTER}" $LOGFILE
As @EdMorton pointed out, it will be more robust to use single-quotes, and break out of it as needed to double-quote only the specific parts where variables are used, like this:
sed -i '' '3i\
There are a total of '"${ALOLANCOUNT}"' Pokemon in Alola, and you have '"${HAVECOUNTER}"', and need '"${ALOLANNEEDCOUNTER}" $LOGFILE
This is more robust,
because this way you don't need to scan the entire string if there is something that might need to be escaped, like the \
in my first proposal.
Upvotes: 1