in reply to Replacing duplicate string

Nevermind that in the end it's going to end up in a 600 line script. Show us the code you wrote for this small, self-contained task.

Replies are listed 'Best First'.
Re: Replacing duplicate string
by ddrew78 (Beadle) on Apr 15, 2009 at 17:54 UTC
    Therein lies my problem, I've got nothing. sed will replace every occurance, grep will find me every occurance. What I need is something to get me started, that will find/replace all occurrances of the word 'priority' except the first one in each paragraph. I had even considered condensing each paragraph into one line and potentially doing it from there, but even then I would still have the same issue. I'm completely out of ideas, don't know how to even start.

      How about remembering whether you've seen the word 'priority' already, and if so, replacing it, and not replacing it if you haven't seen it at least once already? You have to forget that you've seen 'priority' already if you see an empty line.

Re Replacing duplicate string
by ddrew78 (Beadle) on Apr 15, 2009 at 18:10 UTC
    Ok, so I've found this online, but i keep getting compilation errors. At any rate, do you think this is something I can build on to do what I'm trying to accomplish?
    open(MYINPUTFILE, "cos2"); $/ = ''; while (<MYINPUTFILE>) { while ( /\b([\w'-]+) (\s+\1)+\b/gi ) { sed 's/$1/bandwidth/'; } }
    I really appreciate the help on this, it's been driving me crazy.

      Maybe you can tell me where in the Perl documentation you found the sed command?

      As a next step, I would recommend that you add comments indicating where you wrote the code that you think implements the steps I outlined for you to undertake.

      Also, it often helps us to help you better if you also tell us what the error message is, possibly together with the input data.

      A minor note regarding modifying the $/ variable. While it is useful for slurping a file, it is best done with local inside a code block to minimize any possible problems later on in the script. e.g.,
      { local undef $/; while (<MYINPUTFILE>) { ... } }
      That way the changes are limited to that block only. Very useful when messing with the special variables.
      -----
      "Ask not what you can do for your country. Ask what's for lunch."
      -- Orson Welles
      Here's some pseudocode to help you get started:
      while (readline into $line) if $line starts with 'priority' print $line if 'priority not already seen' remember that priority was seen end else if $line is blank forget that priority was seen end else print $line end end

      while (readline into $line) if $line starts with 'priority' replace 'priority' with 'bandwidth' if 'priority already seen' remember that priority was seen end else if $line is blank forget that priority was seen end print $line end

      It's basically what Corion already said, but it has the basic structure of what your code might look like. Now all you have to do is translate it.

      Update: Changed pseudocode to do substitution rather than skip printing.