0

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.

1 Answers1

0

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

infixed
  • 1,155
  • 7
  • 15
  • Thank you! Actually it worked except that I found another problem... When I manually type `motor_loc=1000` Then it works, but I realize that following line does not read value from motor.txt `set motor_loc = grep -i 'aaaa' motor.txt | awk '{print $2}'` – VitChan Jeong Mar 23 '16 at 00:00
  • I don't know if you tried to put backticks in there or not. markup in comments is awkward. since code is marked up by enclosure in backticks in a comment, how does one put backticks inside code in comments on stackoverflow – infixed Mar 23 '16 at 00:12
  • I added backticks into the answer – infixed Mar 23 '16 at 00:17
  • I think I can solve this problem with back ticks. Thank you again for your help. :) – VitChan Jeong Mar 23 '16 at 00:37
  • I'm beginning to wonder if backticks are too old school. All the cool kids seem to be using the form `FOO=$(ls *.txt)` , and perhaps there is some subtle distinction or arvantage I've missed. I probably think of using backticks first because of similar usage in perl. – infixed Mar 23 '16 at 17:28