Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding html tags 1

Status
Not open for further replies.

Paulova

Technical User
Apr 7, 2005
9
ES
I´m loosing my mind trying to make working an awk script for

1.- Search lines with more than x all-caps words and convert it to all-caps lines adding <h2> at the beginnig and </h2> at the end of that lines
2.- Add <h3> at the beginnig and </h3> at the end to the rest of lines (not the blank ones)

EXAMPLE line ONE

Example line two
Example line three

to:

<h2>EXAMPLE LINE ONE</h2>

<h3>Example line two</h3>
<h3>Example line three</h3>

does anyone know how to do it using awk?
 
Something like this ?
awk '
{ n=0;for(i=1;i<=NF;++i)n+=($i==toupper($i))}
n>1{print "<h2>"$0"</h2>";next}
NF{print "<h3>"$0"</h3>";next}
{print "<br>"}
' /path/to/input > output

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Adding toupper to line two exactly what i was looking for.
Thanks a lot for your quick response.
 
n+=($i==toupper($i)) Counts numbers as caps too. Is there another way to do this?

Thanks again
 
Perhaps would be better to convert all lines like
EXAMPLE LíNE ONE
to all caps, i dont mind, and add <h2> at the beginnig and </h2> at the end to all caps lines.
Pattern is always like:
/[A-Z]á[A-Z]/{gsub(/á/, "A")};
/[A-Z]é[A-Z]/{gsub(/é/, "E")};
...
Can you give me a hand again PH?
Thanks
 
n+=($i==toupper($i)) Counts numbers as caps too
Typed, not tested:
awk '
{ n=0;for(i=1;i<=NF;++i){
x=$i;gsub(/[^A-Za-z]/,"",x)
n+=(x>"" && x==toupper(x))
}
}
n>1{print "<h2>"toupper($0)"</h2>";next}
NF{print "<h3>"$0"</h3>";next}
{print "<br>"}
' /path/to/input > output

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top