0

There are many ways to express arithmetic things: new and old ways. However, I would like to write this line efficiently and well

sed -n '$START,$ENDp;$ENDq' < data.tex

which output

sed: 1: "$START,$ENDp;$ENDq": invalid command code S

Total Code

#!/bin/bash
function getStart {
        local START="$(awk '/begin\{document\}/{ print NR; exit }' data.tex)"
        echo $START
}

function getEnd {
        local END="$(awk '/end\{document\}/{ print NR; exit }' data.tex)"
        echo $END
}

START=$(getStart)
(( START-- ))
echo $START

END=$(getEnd)
(( END++ ))
echo $END

sed -n '$START,$ENDp;$ENDq' < data.tex

where the SED is getting the linenumbers from local variables of functions. How can you express the first line by expressing the variables efficiently for the SED?

1 Answers1

3

Variables aren't expanded in single quotes. Use double quotes:

sed -n "$START,${END}p;${END}q" < data.tex

Also, since you want the value of END followed by the character p, and not the value of ENDp, you need to indicate where the name of the variable ends. You can use the ${END} syntax, which makes it explicit where the variable name ends.

Note that if $START or $END are not numbers but regular expressions, you might need to escape some characters.

choroba
  • 47,233