By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,834 Members | 2,234 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,834 IT Pros & Developers. It's quick & easy.

Replacing a string

P: 37
Hello All,

I have two lines as follows in my file.
<start>start of program<end>
<start>string to be replaced<end>

These lines are in the middle. There are blank spaces before the promt <. The sentence 'string to be replaced' need to be replaced by the sentence 'changed'.
The sentences 'string to be replaced is not always the same.

So i would like to delete the line 'string to be replaced and add my new sentence 'changed'.

I have tried the code,

Expand|Select|Wrap|Line Numbers
  1. while (<IN>){
  2. my $file = $_;
  3. if ($file =~ /\start of program/){
  4. print OUT $file;
  5. $file=<IN>;
  6. }
  7. $file =~ s/             /changed/g;
  8. }
As i told earlier the two sentences are in the middle with blank spaces before it, How could i incorporate it and print the first line as it and then change my second line to 'changed'.

Ramesh
Sep 17 '08 #1
Share this Question
Share on Google+
5 Replies


eWish
Expert 100+
P: 971
Is the data you provided exactly as it is in the file?

--Kevin
Sep 17 '08 #2

P: 37
Hi Kevin,

The exact data in my file is

<main>x02_a08_s091<main>
<sub>Iteration problem<sub>

In my file, only the numbers change in my first line and the name of the problem in this case is Iteration, but the title changes every time. I need to change the heading to 'Actual problem' from 'Iteration problem'. My intention is to change the heading to 'Actual problem' irrespective of the heading mentioned initially. There are blank spaces before main and sub.
Sep 17 '08 #3

nithinpes
Expert 100+
P: 410
Hi Kevin,

The exact data in my file is

<main>x02_a08_s091<main>
<sub>Iteration problem<sub>

In my file, only the numbers change in my first line and the name of the problem in this case is Iteration, but the title changes every time. I need to change the heading to 'Actual problem' from 'Iteration problem'. My intention is to change the heading to 'Actual problem' irrespective of the heading mentioned initially. There are blank spaces before main and sub.

I am assuming you may want to retain those spaces.
Expand|Select|Wrap|Line Numbers
  1. while (<IN>){
  2. my $file = $_;
  3. if ($file =~ /x02_a08_s091/) { ## make this generic if pattern is same across
  4. print OUT $file;
  5. $file=<IN>;
  6. $file =~ s/(<sub>\s*)\S+/$1Actual/;
  7. print OUT $file;
  8. }
  9. }
  10.  
  11.  
Sep 18 '08 #4

P: 37
Thanks for the hint. I just modified a little bit and it is working well

Expand|Select|Wrap|Line Numbers
  1. $file =~ s/(<sub>)\s+(\w+)/$1Actual/;
  2.  
I have one more query,In the above problem,I print the first line and modify the second line.If in a file, if I have to modify the file at 4 places, Is it possible to do it under one while loop or do I need to open the file 4 times and use the while loop each time?
Sep 21 '08 #5

nithinpes
Expert 100+
P: 410
Thanks for the hint. I just modified a little bit and it is working well

Expand|Select|Wrap|Line Numbers
  1. $file =~ s/(<sub>)\s+(\w+)/$1Actual/;
  2.  
I have one more query,In the above problem,I print the first line and modify the second line.If in a file, if I have to modify the file at 4 places, Is it possible to do it under one while loop or do I need to open the file 4 times and use the while loop each time?
The single while loop will iterate through the entire file. You can do the modification any number of times in the file, as long as the pattern is matched.
Sep 22 '08 #6

Post your reply

Sign in to post your reply or Sign up for a free account.