Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Escape HTML special characters in awk

Tags:

html

linux

awk

From an awk script I want to generate a HTML file. My string could include characters like "<" and "&". Is there a short and proven function for awk which does the escaping?

like image 642
cruftex Avatar asked Oct 20 '25 01:10

cruftex


1 Answers

To escape the bare minimum you can do:

function escapeHtml(t)
{
  # Must do this one first
  gsub(/&/, "\\&amp;", t);
  gsub(/"/, "\\&quot;", t)
  gsub(/</, "\\&lt;", t);
  gsub(/>/, "\\&gt;", t);
  return t;
}
like image 174
Matthew Buckett Avatar answered Oct 21 '25 14:10

Matthew Buckett