Hello,
Thank you for providing the requested information. The sed
command you provide isn't a full command, as it stands. The s
command is used to substitute one particular given string, very precisely, with another string. By default, the strings are separated by forward-slashes. So at the moment, you're telling sed
you want to perform a substitution, but only providing a string to search for, and nothing to replace it with. Hence, the unterminated 's' command
error.
So - you need to provide the exact string to search for, and what you want to replace it with. You can embed newlines in a sed
substitution by representing newlines as \n
in your script.
So, let's look at an example that expands on your solution to use a fully-qualified and completed sed
substitution command.
Firstly, our input:
$ cat sample1.txt
The start
Some stuff
Some more stuff
Blah
Blah
module load ibmdb2/client/10.1.3
More stuff
Some more stuff
And now the end
$
This file includes the exact string - "module load ibmdb2/client/10.1.3" - that you want to replace.
And now, here's the script we'll use to do the substitution:
$ cat fix.sh
#!/bin/bash
for file in *txt
do
/bin/sed -i 's%module load ibmdb2/client/10.1.3%INFA_CONFIG="$HOME/config/research_dw_rt_$ENV_VAR.cfg"\nDB_Ver=`grep -w 'DB_Version' $INFA_CONFIG | cut -d '=' -f 2`\nmodule load ibmdb2/client/$DB_Ver%g' "$file"
done
$
Now, there's one extra trick here that we have to use. The string we want to search for has a forward slash as part of it, as indeed do the strings we want to replace it with. Now, you'll recall my mentioning earlier that sed
uses forward slashes as its field separator for the substitution command. So by default, sed
would trip up over your strings, since it would think the slashes in them were intended as field separators, and you would get all kinds of errors as a result of sed
thinking the number of fields wasn't correct.
To work around this, sed
will in fact take the first character immediately after the substitution command as the field separator. You'll notice I went with a percent symbol here, since there is no occurrence of these in your strings. So the string we want to search for (one single line), and the one we want to replace it with (three separate lines, with newlines between them) can be fully and correctly specified.
And here's what we get when we run the script:
$ ./fix.sh
$ cat sample1.txt
The start
Some stuff
Some more stuff
Blah
Blah
INFA_CONFIG="$HOME/config/research_dw_rt_$ENV_VAR.cfg"
DB_Ver=`grep -w DB_Version $INFA_CONFIG | cut -d = -f 2`
module load ibmdb2/client/$DB_Ver
More stuff
Some more stuff
And now the end
$
Hope this helps ! If this doesn't quite get you there, or if any part of the above explanation doesn't quite make sense, please let us know and we can take things from there.