2

Somehow the following code isnt working. The echo shows the right command, but doesnt do the replacement in the file.

file abcd.txt:

\overviewfalse
\part1false
\part2false
\part3false
\part4false
\part5false
\part6false
\part7false

Code:

function convert_to_true()
{
        sed -i 's/overviewfalse/overviewtrue/' abcd.txt
        for iterator in `seq 1 10`; do
                match=part${iterator}false
                replace=part${iterator}true
                command="sed -i 's/${match}/${replace}/' abcd.txt"
                echo $command
                $(command)
                done
}
4
  • corrected. was a typo. Commented Jan 15, 2018 at 16:25
  • 1
    TeX is a programming language; factor out the false into a single parameter that is passed to the other commands so that you have only one place to change false to true. Commented Jan 15, 2018 at 16:41
  • Here's an example of how to pass an argument from the command line, so that you don't need to edit your files in-place: tex.stackexchange.com/questions/243854/… My TeX skills are too rusty to suggest how to parameterize \overview and \part1 et al, but tex.stackexchange.com can help you out. Commented Jan 15, 2018 at 16:44
  • @chepner i am editing tex to txt ! The reason is that in .tex the above program will anyways wont work, because numericals are not allowed in variable names. I found out later, that the program wont run. Commented Jan 17, 2018 at 19:09

1 Answer 1

5

Multiple anti-patterns used, 1) Get rid of using shell commands in a variable, use a function or an array. But you don't need either of those for your requirement. 2) Single-quotes don't expand variables in any shell.

Just do the brace expansion logic instead of using non standard seq usage,

for iterator in {1..10}; do
    match="part${iterator}false"
    replace="part${iterator}true"
    sed -i "s/${match}/${replace}/" abcd.tex
done

Or use a function all for sed if at all you need a separate function for it

sed_replace_match() {
    (( "$#" >= 2 )) || { printf 'insufficient arguments\n' >&2; }
    sed -i  "s/${1}/${2}/" abcd.tex
}

and call the function with search and replace patterns i.e.

sed_replace_match "$match" "$replace"

Or if you just want to do it all in one shot, just use GNU sed and don't worry about the numbers, as they are retained across replacements as part of the captured group \1 in the below example

sed -r 's/part([0-9]*)false/part\1true/g' abcd.tex

If the contents look fine, use the -i option for the in-place edit of the file. Or for any POSIX compliant sed just use

sed 's/part\([0-9]*\)false/part\1true/' file
Sign up to request clarification or add additional context in comments.

2 Comments

thank you. your solution works. will get back after reading more.
I did not know that single quotes dont expand variables and this sentence helped me. After changing single quotes to double as your first solution states, the program works now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.