Text Formatting with Awk 
Author Message
 Text Formatting with Awk

% Now I have a file with customer information that I want to put into a
% format which can be read by a database.  I am using 'getline' to read
% the file one line at a time.  But, I think I need the information in a
% block of 14 to 15 lines.  That is, the information is in a file and looks
% like this:
% "
% Name: Sherman L. Willden
% Title: An Administrator
% Company: My Company
% Address: 3349 Academy Blvd.
% Address: Colorado Springs, CO
% Address: 80918
% Telephone: 719-592-7102
% Fax: 719-592-7102

% Comments:
%
% Windows NT on a Intel machine.
% Works good, I guess
%
% "
[...]
% I have tried delimiting the entries with 'awk -F\" ', but that returned
% the whole file and caused a core dump.

It ought not to have done so. It ought not to have helped, either, since
-F doesn't change the record separator, which is what you wanted to do.

You could do this:
 BEGIN { RS = "\"" }
 { # sort out what each field is -- ommitted for brevity :)
   # printf ...
 }

or you could do this:
 BEGIN { FS = ":"; comments = "" }
 $1 == "Name" { name = $2; next }
 $1 == "Title" { title = $2; next }
 # ...
 $1 == "Comments" { comments = $2 }
 $1 == "\"" { printf "\"%s\",\"%s\",...,\"%s\"\n", name, title, ..., comments;
              name = ""; title = ""; ...
              next }
 # this line should only be reached in the comments section
 { comments = comments " " $0 }
for the repeated lines of address, you can do the same thing I do for comments,
just keep appending to the address variable. Before printing, you should
strip out leading blanks and make sure embedded quotes are handled
correctly.

Hope that helps.
--

Patrick TJ McPhee
East York  Canada



Sun, 26 Mar 2000 03:00:00 GMT  
 Text Formatting with Awk

Quote:

> "

whenever you see a `"' line, this is a new record.  so you can print
out the old record (if it's not empty) and empty a new record
(with NAME="" etc.)  that gets rid of the `9 prints per person' problem.

Quote:
> Address: 3349 Academy Blvd.
> Address: Colorado Springs, CO
> Address: 80918

those are going to cause you problems with your if statements!  you'll
need to keep a counter of `NumberOfAddressesSeen' as well.

Quote:
> Comments:

> Windows NT on a Intel machine.
> Works good, I guess

> "
> "Windows NT on a Intel machine. Works good, I guess"

that's another issue altogether -- when you're reading the Comments
block, you want to mesh all the lines together.  not a problem
(have a variable ThisIsInComments, maintain it carefully, and when
it's true just add $0 onto CommentLineSoFar).

Quote:
> This is the function I am presently using:
> BEGIN {FS=":"

here I always use `q="\"";' also, (and for you, qcq=q "," q) to change

Quote:
>    print "\""NAME",""\""TITLE",""\""COMPANY",""\""........

to

Quote:
>    print q NAME qcq TITLE qcq COMPANY qcq ........

which is easier to read.

if you set `OFS' to `","', then use

  print q NAME,TITLE,COMPANY,...

you may get an even clearer way to do this.
--



Sun, 26 Mar 2000 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. formatting a text file with awk

2. RTF-text format in text fields ?

3. Rendering HTML-formatted text as normal text

4. Tkinter code for formatted text in Text widget?

5. Outputting formatted text from text widget

6. Text format of text widget

7. Formatting text (TOC) using text widget

8. Importing other text formats into the tk text widget

9. dates, formatting in AWK

10. formatting with awk

11. using awk to format ifconfig -a output

12. Format functions in AWK

 

 
Powered by phpBB® Forum Software