-1

I need to update almost 100 HTML pages that contain 15-20 form fields each.

To pass Section 508 compliance, they all need to be uniquely named.

Each form-group has three of the same values for attributes like this:

<label for="input-title" class="control-label">Title*</label>
<input class="form-control" id="input-title" name="input-title" value="SA Analyst" required>

Notice the for, name and id attributes are all the same.

I just need it to be something like this with an incrementing number at the end:

<label for="input-title21" class="control-label">Title*</label>
<input class="form-control" id="input-title21" name="input-title21" value="SA Analyst" required>

The challenge is to: - loop through all form fields in an HTML file (see regex below) - update each "form-group" with an incrementing number appended to the end of each of the three attribute values "for, name and id" - make sure each form-group has the same appended, incremented number (i.e. every three attributes would get the same number in the current loop)

Here is the starting bash code I am working with:

#!/bin/bash
FILES=/Users/Administrator/files/*.html
counter=1
for f in $FILES
do
  echo "Processing $f file..."
  # take action on each file. $f store current file name
  # cat $f
  # sed 's/<input/<input2/g' $f > $f.txt  

  sed "s/<input/<input$counter/g" $f > $f.txt 

  echo $counter
    ((counter++))


done
echo All done

This code successfully updates the input with the counter variable number and saves it to a .txt file but this is not yet the solution since it updates ALL input fields in the HTML file with the same incremented number.

Here is the regex I came up with that finds the form groups that need to be changed:

(.*for\=")([0-9A-Za-z-]+)(".*\n\s*[0-9A-Za-z\<\>\-\=\"\s]*[id=|name=]")([0-9A-Za-z-]+)(".*[id=|name=]")([0-9A-Za-z-]+)("\s[type|req])

So how do I integrate this regex with the bash code above and update the three attributes in every form-group?

Mitch Moccia
  • 519
  • 2
  • 8
  • 23

1 Answers1

0

With mawk:

scriptfile1:

/label for=\"input-title\"/ {
        num++
        }
{
        gsub("label for=\"input-title\"","label for=\"input-
title"num"\"")
        gsub("id=\"input-title\"","id=\"input-title"num"\"")
        gsub("name=\"input-title\"","name=\"input-title"num"\"")
        print
}

Here we increment a counter (num) every time we encounter the text label for="input-text", we then check for three instances of input-text in each segment (for=,id= and name=) using gensub and change these to add the num variable. We finally print the rebuilt line.

Run with:

awk -f scriptfile1 sourcedatafilename
Raman Sailopal
  • 12,320
  • 2
  • 11
  • 18