Mel
Mel

Reputation: 21

Find & replace with increment across multiple files

I have hundreds of html files and each of these files has the term "Issue 1" given in it, at two different places in the same file. My goal here is to increment the issue number across the files.

For example: The files are A1, A2, A3... etc. The file A1 has "Issue 1" given at two places in the file. This file does not need to be changed.

The file A2 again has "Issue 1" given at two places in the file. This file needs to be changed so that "Issue 1" is automatically incremented to "Issue 2" at both places, and so on for all the files.

How to do this in Gawk script? I love Notepad++, but I am not a technical (programmer) user. I need a relatively simple way to perform this task that is easily repeatable and timely.

Upvotes: 1

Views: 333

Answers (2)

Ed Morton
Ed Morton

Reputation: 203674

All you need in the awk script is:

@load "inplace"
{ sub(/\<Issue 1\>/,"Issue " ARGIND); print}

Then just call it as awk -f script A* or whatever the syntax is on Windows. Get a current version of gawk first, though - your version is more than 5 years out of date and missing a TON of useful functionality. Also seriously consider getting cygwin and running gawk and all other UNIX tools from there - it'll make your life vastly easier!

Upvotes: 1

Lars Fischer
Lars Fischer

Reputation: 10149

Here is a GNU awk script that might help you.

script.awk

FNR==1    { fileCount++
            outFile = FILENAME ".mod"
          }
          { $0 = gensub(/Issue 1([^0-9]|$)/, "Issue " fileCount "\\1", "g")
            print > outFile
          }
  1. make a backup copy of your original files
  2. run the script like this: awk -f script.awk pa*.html
  3. make sure that the generated pa*.html.mod files are the correct result
    1. only then: rename the mod files back to the orignal filenames

Upvotes: 0

Related Questions