BASH - how to substitute many lines in a text file at once
Hi all! I'm very new in bash and right now I can't find a way to solve my problem. I hope somebody can help.
Right now I have an array:
array_of_channels=( channel1 channel2 channel3 )
and a "new_main.cpp" file where I want to append new lines after the ones containig the elements of the array_of_channels. For example the original "main.cpp" contains:
(text...)
sc_fifo<double> channel1("channel");
sc_fifo<double> channel2("channel");
sc_fifo<double> channel3("channel");
(text...)
And I want to have in the "new_main.cpp" file:
(text...)
sc_fifo<double> channel1("channel");
#ifdef COVG
p[1] = "f channel1";
#endif
sc_fifo<double> channel2("channel");
#ifdef COVG
p[2] = "f channel2";
#endif
sc_fifo<double> channel3("channel");
#ifdef COVG
p[3] = "f channel3";
#endif
(text...)
So I wrote:
channel_index=1
for s in ${array_of_channels[*]}; do
sed '/[^ ]*sc_fifo[^ ]*<[a-z]*> '$s'[\(].*;/ a\#ifdef COVG\np[i] = "f '"$s"'";\n#endif\n' <main.cpp >new_main.cpp
let "channel_index++"
done
The result is that, for every loop, the sed command is writing over the whole file, so everything I get in the "new_main.cpp" is:
(text...)
sc_fifo<double> channel1("channel");
sc_fifo<double> channel2("channel");
sc_fifo<double> channel3("channel");
#ifdef COVG
p[3] = "f channel3";
#endif
(text...)
Would somebody know how to solve it?
|