in reply to Searching and replacing text

Since you asked for a push, here's some pseudo-code, which you should be able to translate into perl:
read first line from file_a split the line into scalar variables ("machine" and "site") open file_b while reading a line at a time from file_b { if the line contains "machine", set $apply_edit to true elsif the line contains "Parent Submap: " and $apply_edit is true +{ replace the rest of this line with "site" reset $apply_edit to false } print the line }
I expect others will propose more elegant and compact methods, but this is, I think, easy to comprehend. Note that watching for the "machine" name involves a regex match with the "i" modifier at the end, to ignore case. When expressed in perl, this approach can involve less typing than the pseudo-code I gave you.

Replies are listed 'Best First'.
Re^2: Searching and replacing text
by ysth (Canon) on Jul 29, 2004 at 06:06 UTC
    But you can't just substitute arbitrary text in a text file, unless it happens to have exactly the same length. As file_b is being read and searched, each line should be copied out to a new file, and updated line printed in it's proper order, and the remainder of file_b copied. Then rename the new file to file_b (making sure file_b is closed).
      Yes, I glossed over the output management part, thinking the OP probably had a handle on that already (unfortunate pun duly noted).

      As often as not for things like this, I just make the script print to STDOUT and put the file handling stuff on the command line -- why right write more lines of perl code to do stuff that the shell can do for me with an angle bracket?

      text-altering-script.perl input.file > output.file # optional step: mv output.file input.file