LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-20-2009, 09:19 AM   #1
carolflb
Member
 
Registered: Oct 2009
Posts: 40

Rep: Reputation: 15
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?
 
Old 10-20-2009, 09:40 AM   #2
rn_
Member
 
Registered: Jun 2009
Location: Orlando, FL, USA
Distribution: Suse, Redhat
Posts: 127
Blog Entries: 1

Rep: Reputation: 25
The issue is you are reading from main.cpp and writing to new_main.cpp. main.cpp never gets updated with the change you made in any of the previous iterations of the loop, and only the last iteration makes it through. There are a couple of ways to solve this:

1. after the sed command do a : mv new_main.cpp main.cpp -- to overwrite the main.cpp
2. use sed -i ;
a. remove the less-than sign from '< main.cpp' and just leave 'main.cpp' -- sed can read the file directly without redirection.
b. remove the > new_main.cpp part completely.

Remember to make a backup of main.cpp before you run the script.

Also, please only post your question in one forum at a time. Thanks.
 
Old 10-20-2009, 10:21 AM   #3
carolflb
Member
 
Registered: Oct 2009
Posts: 40

Original Poster
Rep: Reputation: 15
Thanks!!!

Hi rn. Thanks a lot for your answer! Both options work perfectly.

Ah.. sorry for posting twice.. from now own I'll add the post to just one topic.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to loop over text file lines within bash script for loop? johnpaulodonnell Linux - Newbie 9 07-28-2015 03:49 PM
[SOLVED] BASH - how to substitute many lines in a text file at once carolflb Linux - Newbie 6 10-20-2009 01:28 PM
Remove lines in a text file based on another text file asiandude Programming 10 01-29-2009 10:59 AM
Adding lines of text to beginning of a text file BillKat Programming 2 01-19-2009 10:40 AM
Substitute specific lines with lines from another file rahmathullakm Programming 4 01-10-2009 05:47 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:37 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration