468,321 Members | 1,764 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,321 developers. It's quick & easy.

Replacing a string

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
5 1337
eWish
971 Expert 512MB
Is the data you provided exactly as it is in the file?

--Kevin
Sep 17 '08 #2
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
410 Expert 256MB
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
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
410 Expert 256MB
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.

Similar topics

4 posts views Thread by C# Learner | last post: by
7 posts views Thread by VMI | last post: by
12 posts views Thread by Adam J. Schaff | last post: by
2 posts views Thread by Alain | last post: by
32 posts views Thread by FireHead | last post: by
reply views Thread by Lakhi | last post: by
7 posts views Thread by DarthBob88 | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.