Google Answers Logo
View Question
 
Q: Awk script to export database to html pages ( Answered 5 out of 5 stars,   0 Comments )
Question  
Subject: Awk script to export database to html pages
Category: Computers > Algorithms
Asked by: questioner1234-ga
List Price: $10.00
Posted: 27 Oct 2003 22:14 PST
Expires: 26 Nov 2003 22:14 PST
Question ID: 270339
I have a tab-delimited text file. I would like to take each line and
output the data in that line to a new html file. So essentially, I am
exporting a flatfile text database to html pages with each page being
one formatted database record.

There are ten fields. Field 3 is the filename that I would like record
saved as. For the other fields, I would like to put text around them.
For instance, the output would be something like:

<title>$1</title>

<a href="$2">$5</a>

Where the $NUMBER is the field data.
Answer  
Subject: Re: Awk script to export database to html pages
Answered By: endo-ga on 28 Oct 2003 10:37 PST
Rated:5 out of 5 stars
 
Hi,

Thank you for your question. AWK is pretty nifty for doing stuff like
that very quickly and easily.

Here is an example of the command you'll want to use, looking at your
example:

awk '{print "<title>",$1,"</title>","\n","<a href=\""$2"\">"$5"</a>" >
$3}' inputfile.txt

Let me try and explain what is going on.
The 'print' command is what you're using to generate the output, it is
contained between the curly brackets. Something between double quotes
is printed as is, e.g. "<title>" will output <title>. Anything not
between double quotes will be treated as an expression.

"\n" creates a newline in your file.

When there's a backslash before a double quote, that means 'ignore the
normal function of this double quote and output it instead'. That's
why you see \"", because you actually want a double quote on the
output, before closing the string of text.

The last bit '> $3', tells the print command to redirect its output to
a file with the filename the actual value of $3.

It's then just a matter of supplying the input file and you're done.

I have tested it and I believe this does what you want, I hope this
solves your problem. If you have any more questions, please do not
hesitate to ask.

Thanks
endo


Search strategy:

awk field output filename
://www.google.com/search?hl=en&lr=&ie=ISO-8859-1&q=awk+field+output+filename

The AWK manual Redirecting Output to Files and Pipes
http://www.cs.uu.nl/docs/vakken/st/nawk/nawk_38.html#SEC41

Item Level Databases: database-specific processing of output
http://www.dur.ac.uk/a.j.gray/2hht5.htm
questioner1234-ga rated this answer:5 out of 5 stars
excellent answer on-topic

Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy