Using sed or awk to turn data into html


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using sed or awk to turn data into html
# 1  
Old 01-25-2011
Using sed or awk to turn data into html

Hi there,

I'm wondering the best way to go about this. I have a file which is fairly specific in its format, but it has some options in it that mess up what I need. I'll give you an example of a couple of lines:
Code:
Bob D
Thomas D/F
Alice A/F
Michael A/D/F
John Michael B
Bachman Turner A/D

And what I would like for it to do is spit out something like:
Code:
<option>Bob Deluxe</option>
<option>Thomas Deluxe</option>
<option>Thomas Failure</option>
<option>Alice Awesome</option>
<option>Alice Failure</option>
<option>Michael Awesome</option>
<option>Michael Deluxe</option>
<option>Michael Failure</option>
<option>John Michael Balloon</option>
<option>Bachman Turner Awesome</option>
<option>Bachman Turner Deluxe</option>

so that the last section of each line, with the / separations, denotes a need for a different line and a qualitative descriptor spelled out based on that letter. The <option> html is just an example, I have something specific that needs to go in there, but I should be able to handle that if someone can help out with the sed or awk that I need. I think the toughest part is dealing with the lines with extra spaces in them. *sigh* i've been looking all over and trying all sorts of things, hopefully someone will have an idea.

Thanks,
Steve
# 2  
Old 01-25-2011
Code:
sed 's#.*#<option>&</option>#' myFile

ooops, not so fast....
This User Gave Thanks to vgersh99 For This Post:
# 3  
Old 01-25-2011
I don't think that takes into account the processing of the quality variables at the end of each line, does it? And inserting lines with the same name but different qualities if the end of the line has more than one quality variable?
# 4  
Old 01-25-2011
Quote:
Originally Posted by melancthon
I don't think that takes into account the processing of the quality variables at the end of each line, does it? And inserting lines with the same name but different qualities if the end of the line has more than one quality variable?
as I said 'ooooops' Smilie

nawk -f mel.awk myFile

mel.awk:
Code:
BEGIN {
  tN=split("A B D F", tA, FS)
  for(i=1;i<=tN;i++)
     abbvA[tA[i]]=i
  fullN=split("Awesome Balloon Deluxe Failure", fullA, FS)

  SEP_abbv="/"
}
function rindex(str,c)
{
  return match(str,"\\" c "[^\\" c "]*$")? RSTART : 0
}
{
  valN=split($NF,valA,SEP_abbv)
  str=substr($0,1, rindex($0,FS)-1)
  for(i=1;i<=valN;i++)
    print "<option>" str, ((valA[i] in abbvA)?fullA[abbvA[valA[i]]]:"unknown") "</option>"
}

This User Gave Thanks to vgersh99 For This Post:
# 5  
Old 01-25-2011
That's so awesome. Now to hope I have nawk Smilie

One little question - what if my <option> tag has "" in it (like <option value="blah"> Can I use ' with print on the outside as in echo or sed?

And my turn for ooops... a little \ does wonders!

You're the best! Thanks!
# 6  
Old 01-25-2011
Code:
BEGIN {
  tN=split("A B D F", tA, FS)
  for(i=1;i<=tN;i++)
     abbvA[tA[i]]=i
  fullN=split("Awesome Balloon Deluxe Failure", fullA, FS)

  SEP_abbv="/"
  qq=sprintf("%c", 034)
}
function rindex(str,c)
{
  return match(str,"\\" c "[^\\" c "]*$")? RSTART : 0
}
{
  valN=split($NF,valA,SEP_abbv)
  str=substr($0,1, rindex($0,FS)-1)
  for(i=1;i<=valN;i++)
    print "<option value=" qq "blah" qq ">" str, ((valA[i] in abbvA)?fullA[abbvA[valA[i]]]:"unknown") "</option>"
}

This User Gave Thanks to vgersh99 For This Post:
# 7  
Old 01-25-2011
Gah!

I forgot some of the lines have nothing at the end...

Code:
Alice A/F
George
Boggy D

Anyway to have it just put George again instead of unknown?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Awk/sed HTML extract

I'm extracting text between table tags in HTML <th><a href="/wiki/Buick_LeSabre" title="Buick LeSabre">Buick LeSabre</a></th> using this: awk -F "</*th>" '/<\/*th>/ {print $2}' auto2 > auto3 then this (text between a href): sed -e 's/\(<*>\)//g' auto3 > auto4 How to shorten this into one... (8 Replies)
Discussion started by: p1ne
8 Replies

2. Shell Programming and Scripting

Merge files and copy some data using sed or awk

Copy data from other file to paste cat file1: Name: server1.data.com data1 server1 running Name: server3.data.com data3 server3 running cat file2: server1 good server2 bad network not ok server3 good Output: (10 Replies)
Discussion started by: kenshinhimura
10 Replies

3. Shell Programming and Scripting

Retrieve information Text/Word from HTML code using awk/sed

awk/sed newbie here. I have a HTML file and from that file and I would like to retrieve a text word. <font face=arial size=-1><li><a href=/value_for_clients/Tokyo/abc_process.txt>abc</a> NDK Version: 4.0 </li> <font face=arial size=-1><li><a... (6 Replies)
Discussion started by: sk2code
6 Replies

4. Shell Programming and Scripting

awk or sed or any commands to stripe data

Say i have: g_gateway domain="abc.com" to="123.123.123.123" relay="false" how do i use awk or sed or any method to get the result below: abc.com 123.123.123.123 (3 Replies)
Discussion started by: timmywong
3 Replies

5. Shell Programming and Scripting

awk -- Extract data from html within multiple tags as reference

Hi, I'm trying to get some data from an html file, but the problem is before it can extract the information I have multiple patterns that need to be passed through. https://www.unix.com/shell-programming-scripting/150711-extract-data-awk-html-files.html Is a similar problem. The only... (5 Replies)
Discussion started by: counfhou
5 Replies

6. Shell Programming and Scripting

awk - sed :Help Getting next lines data .

Experts, Can you please help how to get the output that are written just below "bad" calls badcalls nullrecv 439486 54 0 badlen xdrcall dupchecks ... (6 Replies)
Discussion started by: rveri
6 Replies

7. Shell Programming and Scripting

extract data with awk from html files

Hello everyone, I'm new to this forum and i am new as a shell scripter. my problem is to have html files in a directory and I would like to extract from these some data that lies between two different lines Here's my situation <td align="default"> oxidizability (mg / l): data_to_extract... (6 Replies)
Discussion started by: sbobotex
6 Replies

8. Shell Programming and Scripting

formatting data file with awk or sed

Hi, I have a (quite large) data file which looks like: _____________ header part.. more header part.. x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 x11 x12 x13 ... ... x59 x60 y1 y2 y3 y4... ... y100 ______________ where x1, x2,...,x60 and y1, y2,...y100 are numbers of 10 digits (so each line... (5 Replies)
Discussion started by: lego
5 Replies

9. Shell Programming and Scripting

SED to extract HTML text data, not quite right!

I am attempting to extract weather data from the following website, but for the Victoria area only: Text Forecasts - Environment Canada I use this: sed -n "/Greater Victoria./,/Fraser Valley./p" But that phrasing does not sometimes get it all and think perhaps the website has more... (2 Replies)
Discussion started by: lagagnon
2 Replies

10. Shell Programming and Scripting

Turn HTML data into delimited text

I have a file I've already partially pruned with grep that has data like: <a href="MasterDetailResults.asp?textfield=a&Application=3D Home Architect 4">3D Home Architect 4</a> </td> Approved </td> -- <a href="MasterDetailResults.asp?textfield=a&Application=3d Home... (6 Replies)
Discussion started by: macxcool
6 Replies
Login or Register to Ask a Question