I'm using a shell script to insert code with a variable after a previous code pattern in script.tex, however sed is not adding anything after the expected pattern.
cat script.tex
\multicolumn{1}{c}{st_var}
Expected result (script.tex) after script.sh is run:
\multicolumn{1}{c}{A} & \multicolumn{1}{c}{B} & \multicolumn{1}{c}{C} & \multicolumn{1}{c}{D} & \multicolumn{1}{c}{E} & \multicolumn{1}{c}{F} \\
Current result (script.tex):
\multicolumn{1}{c}{A}
The first part of the conditional is working as expected. The remaining is not being found by sed.
cat script.sh:
#!/bin/bash
var=("NA" "A" "B" "C" "D" "E" "F")
clen=$(( ${#var[@]} - 1 ))
cind=1
for (( i=1; i<${#var[@]}; i++ )) ; do
if [[ "$cind" -eq 1 ]]; then
sed -i 's/st_var/'${var[$i]//\"/}'/g' script.tex
elif [[ "$cind" -gt 1 ]] && [[ "$cind" -lt "$clen" ]]; then
sstr="\multicolumn{1}{c}{${var[$i-1]//\"/}}"
estr=" & \multicolumn{1}{c}{${var[$i]//\"/}}"
festr=" & \multicolumn{1}{c}{${var[$i]//\"/}} \\\\"
sed -i '/^${sstr}/ s/$/${estr}/' script.tex
else
sed -i '/^${sstr}/ s/$/${festr}/' script.tex
fi
cind=$((cind + 1))
done
The var array here must have all elements double quoted for other purposes outside of this question. Also, the var array is shown here for simplicity - the letters A-F could be any random string. The first element in the array here is skipped (NA).
The best attempt so far:
script.sh:
#!/bin/bash -x
var=("NA" "A" "B" "C" "D" "E" "F")
clen=$(( ${#var[@]} - 1 ))
cind=1
for (( i=1; i<${#var[@]}; i++ )) ; do
if [[ "$cind" -eq 1 ]]; then
sed -i 's/st_var/'${var[$i]//\"/}'/g' script.tex
elif [[ "$cind" -gt 1 ]] && [[ "$cind" -lt "$clen" ]]; then
sstr='\multicolumn{1}{c}{'${var[$i-1]//\"/}'}'
estr=' \& \multicolumn{1}{c}{'${var[$i]//\"/}'}'
festr=' \& \multicolumn{1}{c}{'${var[$i+1]//\"/}'} \\'
# sed -i '/$sstr/r $estr/' script.tex
# sed -i '/^'"${sstr}"'/'"${estr}"'/' script.tex
sed -i "s/$sstr/&$estr/" script.tex
else
sed -i "s/$sstr/&$festr/" script.tex
# sed -i '/^'"${sstr}"'/'"${festr}"'/' script.tex
fi
cind=$((cind + 1))
done
Result:
\multicolumn{1}{c}{A} & multicolumn{1}{c}{B} & multicolumn{1}{c}{C} & multicolumn{1}{c}{D} & multicolumn{1}{c}{F} \ & multicolumn{1}{c}{E}
The ampersands are coming through, however the backslashes before multicolumn aren't coming through, and neither are the two backslashes at the end of the line. E and F are also flipped - F should be last.