Dear Sir,
Thank you for the quick response. There is one more thing I would like to check. Suppose I want to modify a particular line in a text file called ‘input.dat’. I tried to do the following:
shell sed -i “1s/.*/$n1 $n2/” input.dat
where the index style variables n1 and n2 have been previously defined in the lammps input script. This gives the following error:
sed: -e expression #1, char 7: unterminated `s’ command
for the text in quotation marks, no immediate variable expansion happens. when it is passed as an argument to sed, the double quotation marks are stripped off, hence the error about the unterminated ‘s’ command. the command line passed to the subshell would have been:
sed -i 1s/.*/$n1 $n2/ input.dat
to have the command you expect to happen multiple things have to be done. for immediate expansion of variables names with more than 1 letter you have to use curly braces, i.e. {n1} or {n2}, or else you get an error of the kind “Illegal substitution for variable n” (unless you by accident also have a variable n defined).
also, you must not use quotation marks. so what should work is this:
shell sed -i 1/.*/{n1}\ {n2}/ input.dat
However, if I only attempt to write a single variable using the command ‘shell sed -i “1s/.*/$n1/” input.dat’, everything works fine. May I know why this is the case?
this latter command cannot have worked correctly. see my explanation above.
What can I do if I want to replace a particular line in the middle of a text file with the values of multiple variables? I am aware of the print and fix print commands but those seem to either overwrite files or append data to their end.
you haven’t looked at the rules for variable expansion and input file processing careful enough. it is a bit complex, but all the information is there. since this is custom written code, you must not assume, that it is the same as in, say, bourne shell or similar. some features are, but not all.
axel.