LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-20-2009, 09:05 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:35 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Code:
#!/bin/bash

array_of_channels=( . channel1 channel2 channel3 )

idx=1
while read line
do
    echo "$line"
    case "$line" in
        'sc_fifo<double> '"${array_of_channels[ $idx ]}"* )
            echo '##ifdef COVG'
            echo "p[$idx] = \"f channel$idx\";"
            echo '#endif'
            let idx=idx+1
    esac
done < main.cpp
 
Old 10-20-2009, 09:38 AM   #3
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, 09:45 AM   #4
vonbiber
Member
 
Registered: Apr 2009
Distribution: slackware 14.1 64-bit, slackware 14.2 64-bit, SystemRescueCD
Posts: 443

Rep: Reputation: 96
I pasted this to a file input.txt
Code:
sc_fifo<double> channel1("channel"); 
sc_fifo<double> channel2("channel"); 
sc_fifo<double> channel3("channel");
Then I ran a script (bogus.sh) with the following content
Code:
#!/bin/sh

sed 's/^\(sc_fifo[^0-9]*\)\([0-9]\{1,\}\)\(.*\)$/\1\2\3\
#ifdef COVG\np[\2] = "f channel\2";\n#endif\n/' input.txt
Code:
$ ./bogus.sh
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
 
Old 10-20-2009, 10:24 AM   #5
berbae
Member
 
Registered: Jul 2005
Location: France
Distribution: Arch Linux
Posts: 540

Rep: Reputation: Disabled
I tried that script which seems to execute what you want:
Code:
#!/bin/bash
array_of_channels=( channel1 channel2 channel3 )
channel_max_index=${#array_of_channels[*]}
touch channel.script
for (( channel_index=0;channel_index<channel_max_index;channel_index+=1 )); do
   i=$(($channel_index + 1))
   echo "/${array_of_channels[$channel_index]}/a \#ifdef COVG\np[$i] = \"f ${array_of_channels[$channel_index]}\";\n#endif\n" >>channel.script
done
sed -f channel.script main.cpp >new_main.cpp
rm channel.script
I presumed that channel1 channel2 ... can be any string, and that p[i] is the rank in the array.
The idea is to generate a sed script with the values in the array_of_channels used to build a command line different for each value.

Last edited by berbae; 10-20-2009 at 10:26 AM.
 
Old 10-20-2009, 10:27 AM   #6
carolflb
Member
 
Registered: Oct 2009
Posts: 40

Original Poster
Rep: Reputation: 15
Thanks!!!

Thanks all for the quick answer! And sorry for posting twice...

@vonbiber:

sed 's/^\(sc_fifo[^0-9]*\)\([0-9]\{1,\}\)\(.*\)$/\1\2\3\
#ifdef COVG\np[\2] = "f channel\2";\n#endif\n/' input.txt

is a great adjustment Thanks!
 
Old 10-20-2009, 01:28 PM   #7
MusicGuy
LQ Newbie
 
Registered: Oct 2009
Posts: 3

Rep: Reputation: 0
Thanks

I'll have to try this. I have a similar difficulty.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
bash- how to compare only certain lines of text files daberkow Linux - Newbie 2 06-01-2009 04:48 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


All times are GMT -5. The time now is 12:08 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration