Replacing a string

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ramesh54
    New Member
    • Jul 2008
    • 37

    #1

    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,

    Code:
    while (<IN>){
    my $file = $_;
    if ($file =~ /\start of program/){
    print OUT $file;
    $file=<IN>;
    }
    $file =~ s/             /changed/g;
    }
    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
  • eWish
    Recognized Expert Contributor
    • Jul 2007
    • 973

    #2
    Is the data you provided exactly as it is in the file?

    --Kevin

    Comment

    • ramesh54
      New Member
      • Jul 2008
      • 37

      #3
      Hi Kevin,

      The exact data in my file is

      <main>x02_a08_s 091<main>
      <sub>Iteratio n 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.

      Comment

      • nithinpes
        Recognized Expert Contributor
        • Dec 2007
        • 410

        #4
        Originally posted by ramesh54
        Hi Kevin,

        The exact data in my file is

        <main>x02_a08_s 091<main>
        <sub>Iteratio n 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.
        Code:
        while (<IN>){
        my $file = $_;
        if ($file =~ /x02_a08_s091/) { ## make this generic if pattern is same across
        print OUT $file;
        $file=<IN>;
        $file =~ s/(<sub>\s*)\S+/$1Actual/;
        print OUT $file;
        }
        }

        Comment

        • ramesh54
          New Member
          • Jul 2008
          • 37

          #5
          Thanks for the hint. I just modified a little bit and it is working well

          Code:
          $file =~ s/(<sub>)\s+(\w+)/$1Actual/;
          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?

          Comment

          • nithinpes
            Recognized Expert Contributor
            • Dec 2007
            • 410

            #6
            Originally posted by ramesh54
            Thanks for the hint. I just modified a little bit and it is working well

            Code:
            $file =~ s/(<sub>)\s+(\w+)/$1Actual/;
            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.

            Comment

            Working...