我需要更新近100個包含15-20個表單字段的HTML頁面。使用bash,grep和sed批量更新表單字段名稱和ID
要通過Section 508合規性,他們都需要唯一命名。
每個表單組有三個像這樣相同的屬性值的:
<label for="input-title" class="control-label">Title*</label>
<input class="form-control" id="input-title" name="input-title" value="SA Analyst" required>
通知之爲,名稱和id屬性都是一樣的。
我只需要它是這樣的,並在最後一個增量數字:
<label for="input-title21" class="control-label">Title*</label>
<input class="form-control" id="input-title21" name="input-title21" value="SA Analyst" required>
面臨的挑戰是: - 遍歷一個HTML文件中的所有表單域(見下面的正則表達式) - 用每個「form-group」更新後面的三個屬性值「for,name and id」來更新每個「form-group」 - 確保每個form-group具有相同的附加增量數字(即每三個屬性會在當前循環中得到相同的數字)
這裏是開始bash的代碼我一起工作:
#!/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
此代碼成功更新與計數器變量數輸入,並將其保存爲.txt文件,但是這還不是因爲它更新所有輸入字段解決方案具有相同遞增數字的HTML文件。
這裏是我想出了正則表達式與認定,需要改變的形式組:
(.*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])
那麼,如何在各種形式的整合這個表達式與上面的bash的代碼和更新三個屬性-組?
我建議使用XML/HTML解析器(xmlstarlet,xmllint ...)。 – Cyrus
你有這樣的例子嗎?謝謝 –