need AWK help: lowercase, trim trailing spaces

Richard L. Goerwitz goer at ellis.uchicago.edu
Sat Apr 20 17:55:07 AEST 1991


In article <1817 at wjvax.UUCP> mario at wjvax.UUCP (Mario Dona) writes:
>
>HELP! I have a situation that just cries out for an awk solution, however
>I'm at a loss over some minor, but important details.  I have a list of
>companies that need to be preprocessed before sending them to our typing
>department.  A simplified portion of the input file as follows:
>
>COMPANY1  2800 FULLING       P O BOX 3608     HARRISBURG PA           17105
>^         ^                  ^                ^                       ^
>|         |                  |                |                       |
>1         11                 30               47                      71
>
>My mission, which I chose to accept, was to reformat the list so that it looks
>like this:
>
>Company1
>28 Fulling
>P O Box 3608
>Harrisburg PA 17105
>...

Here is one Icon solution.  Note that it omits blank lines, capitalizes
multi-word city, street, and company names, and removes the annoying space
between the P and O in "P O Box."  It also inserts three spaces between
the state abbreviation and the zipcode (2 or 3 spaces is standard these
days).  A slight alteration (one line) would be all that you'd need to
add in to force all-uppercase company names.  Note that I split the line
based on the column positions you gave, although I can't imagine how
the gatherers of these statistics managed to fit everything into such
tight spaces!

procedure main()

    every line := trim(!&input,'\t ') do {
	line ? {
	    every i := 11|30|47
	    do write("" ~== capitalize_words(tab(i) \ 1))
	    writes(capitalize_words(tab(71), 1), "   ")
	    write(tab(0), "\n")
	}
    }

end    

procedure capitalize(s)
    s ? (return (move(1) || map(tab(upto('\t ') | 0)) || tab(0)) | "")
end

procedure capitalize_words(s, sw)

    s2 := ""
    trim(s,'\t ') ? {
	while chunk := capitalize(tab(upto('\t '))) do {
	    s2 ||:= chunk || { if chunk == "P" & =" O " then "O " else " " }
	    tab(many('\t '))
	}
	if \sw & s2 ~== ""
	then s2 ||:= tab(0)
	else s2 ||:= capitalize(tab(0))
    }
    return s2

end


-- 

   -Richard L. Goerwitz              goer%sophist at uchicago.bitnet
   goer at sophist.uchicago.edu         rutgers!oddjob!gide!sophist!goer



More information about the Comp.unix.questions mailing list