Replacing a string from an input file within awk script 
Author Message
 Replacing a string from an input file within awk script

Hi

I have an awk script to read a CSV-file (those files that can be
exported from Excel) and want to wrap that raw data in an HTML-file,
one HTML-file for each name (from the CSV) starting with the same
letter. In the end there should be files, such as name-a.html,
name-b.html etc.

In order to be flexible, I would like to keep the data source (my
CSV-file), the script and the dummy layout source (my HTML-file)
apart. So, what I have is

A) the data source (CSV-file with header: test.csv)
Name,Alias,"Classification (1=Work, 2=Private, 3=Uni, 4=Other)",Title
of Site,Address,Phone,Web address
"Smith, Peter",Pete,3,Only Smith,"4/56 Airport Drive,
Mascot",95812345, http://www.*-*-*.com/
"Smith, Peter",Mr. Peter Smith,1,XYZ Company,"255 George Street,
Sydney",99991111, http://www.*-*-*.com/
"Doe, John",John,1,John's Realm,"23 Clovelly Rd,
Cogee",97458888, http://www.*-*-*.com/
"Frog, Kermit",,4,Muppet,"somewhere out
there",12345678, http://www.*-*-*.com/

B) the script
cat test.csv | sed '/^Name/d' | sort -d | gawk '
{
# this part represents the code which puts each record in an array,
# such as
# field[1]: Smith, Pete
# field[2]: Pete
# field[3]: 3
# field[4]: Only Smith
# field[5]: 4/56 Airport Drive, Mascot
# field[6]: 95812345
# field[7]: http://www.*-*-*.com/
letter = tolower (substr (field[1], 1, 1))
file = "name-"letter".html"
# Here I would now like to read from the HTML layout file (below, C))
# and replace the HTML comments with the information
# from field[i], i=1..7

# Then the next input line is read, field is filled etc.

Quote:
}'

C) the HTML layout file (trimmed to a bare minimum)
<html>
<head><!-- Name Letter --></head>
<body>
<!-- alphabetic list, where letters are links to other HTML pages
except the for the current letter -->
<h1>Names starting with <!-- current letter --></h1>
<div id="<!-- sequential super number -->">
  <a href="javascript:%2F%2F" class="listentry"
onMouseDown="javascript:showMenu('<!-- sequential sub number
-->')"><img src="../images/closed.gif" alt="" name="<!-- sequential
image tag number -->">?<!-- Name goes here --></a>
</div>
<div id="<!-- sequential sub number -->">
  <a href="<!-- Web address goes here -->"><!-- Classification goes
here --> - <!-- other data such as title of site, address etc goes
here --></a>
</div>
</body>
</html>

OK, I guess by now you might have guesse my question (and thank you
for your patience!):
How do I replace the HTML comments
 <!-- Name Letter -->
 <!-- alphabetic list, where letters are links to other HTML pages
except the for the current letter -->
 <!-- current letter -->
 <!-- sequential super number -->
 <!-- sequential sub number -->
 <!-- sequential image tag number -->
 <!-- Name goes here -->
 <!-- sequential sub number --> (I know this one appears twice)
 <!-- Web address goes here -->
 <!-- Classification goes here -->
 <!-- other data such as title of site, address etc goes here -->
from within the awk script, which reads from the data source?

Any help is highly appreciated! And again, thanks everybody!
Yowie.



Mon, 14 Nov 2005 15:00:49 GMT  
 Replacing a string from an input file within awk script

Quote:

> OK, I guess by now you might have guesse my question (and thank you
> for your patience!):
> How do I replace the HTML comments
>  <!-- Name Letter -->
>  <!-- alphabetic list, where letters are links to other HTML pages
> except the for the current letter -->
>  <!-- current letter -->
> from within the awk script, which reads from the data source?

you have to read the HTML template line by line in a variable, and then
perform a sub on that variable. I would probably be easier and safer
to use <!--Name_Letter--> as marks in your HTML btw, would protect you
against adding two spaces instead of one, or the HTML editor breaking
lines inside your comments.

Laurent



Mon, 14 Nov 2005 17:39:12 GMT  
 Replacing a string from an input file within awk script

Quote:

> you have to read the HTML template line by line in a variable, and then
> perform a sub on that variable. I would probably be easier and safer
> to use <!--Name_Letter--> as marks in your HTML btw, would protect you
> against adding two spaces instead of one, or the HTML editor breaking
> lines inside your comments.

> Laurent

Thanks for that.

Would I have to read the HTML template in after I process the
(existing) script?

If so, how would that look like? What is the concept?

cat test.csv | sed '/^Name/d' | sort -d | gawk '{existing script}'
cat HTMLtemplate.html | gawk '{reading HTML template line by line into
a variable}' (AFAIK this wouldn't work, would it?) OR
everything IN the existing script?

Can you, or anyone else, give me some hint on how to perform it this
way?

Thanks,
Yowie.



Mon, 14 Nov 2005 21:35:37 GMT  
 Replacing a string from an input file within awk script

Quote:

> > you have to read the HTML template line by line in a variable, and then
> > perform a sub on that variable. I would probably be easier and safer
> > to use <!--Name_Letter--> as marks in your HTML btw, would protect you
> > against adding two spaces instead of one, or the HTML editor breaking
> > lines inside your comments.

> Thanks for that.

> Would I have to read the HTML template in after I process the
> (existing) script?

> If so, how would that look like? What is the concept?

something along the lines of:

cat test.csv | sed '/^Name/d' | sort -d | gawk '
{
  # read the fields in field[1], ...
  ...
  # now read the HTML
  html_template = "the_html_template.html"
  out_file = "the name of the output file"
  for (;;) {
    getline < html_template
    gsub(/<!--Name_Letter-->/, Name_Letter)
    gsub(/<!--Field1-->/, field[1])
    ...
    print $0 > out_file
    if (match($0, /<\/[Hh][Tt][Ll][Mm]>/) {
      close html_template
      close out_file
      break
    }
  }

Quote:
}

'

Laurent



Wed, 16 Nov 2005 14:06:53 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. How to specify input file within awk script?

2. searching and replacing a string within a file with rexx

3. piping input files into awk script

4. Access to Script Name Within Awk Script

5. trying to call awk from within a shell script

6. Run awk from within sed script?

7. shell commands within awk script

8. using Oracle within an awk script

9. Simple awk command within a perl script

10. awk within ksh script

11. Running Awk scripts from within VB

12. Shell variable within an awk script

 

 
Powered by phpBB® Forum Software