Search code examples
sedgrepcygwinlatexmiktex

read number from .txt file and replace string in .tex file


I have a text file with following content aaaa = 1000

I want to replace bbbb in LaTeX file with 1000 that I got from text file

I have tried following codes but did not work.

set motor_loc = grep -i 'aaaa' motor.txt | awk '{print $2}'

sed -i 's/bbbb/$motor_loc/' for_pdf.tex> for_pdf.tex.tmp

At this point, bbbb is changed to $motor_loc

Then, when I tried to create pdf with following line, it gives me an error.

pdflatex for_pdf.tex

How can I change bbbb to 1000 (value read from text file) instead of $motor_loc or string.

FYI, I am using Cygwin and Miktex and will try to run this code in Raspberry Pi 2.

Thank you in advance.


Solution

  • The single quotes in the arguments to sed keep your variable from being expanded, so go from single quotes to double quotes

    user@thoth:~$ FOO=ABC
    user@thoth:~$ echo $FOO
    ABC
    user@thoth:~$ echo "$FOO"
    ABC
    user@thoth:~$ echo '$FOO'
    $FOO
    

    Another sort of quotes that can also be useful are back ticks, that change the output of a command into strings.

    user@thoth:~$ FOO=`ls *.txt`
    user@thoth:~$ echo $FOO
    soap.txt
    

    EDIT: a different form of command substitution seems to be more commonly used in bash scripts these days

     FOO=$(ls *.txt)
    

    There are apparent differences on how backslashes are handled in the command for instance