Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sdd.hp.com!zaphod.mps.ohio-state.edu!pacific.mps.ohio-state.edu!linac!mp.cs.niu.edu!ux1.cso.uiuc.edu!midway!ellis.uchicago.edu!goer From: goer@ellis.uchicago.edu (Richard L. Goerwitz) Newsgroups: comp.unix.questions Subject: Re: need AWK help: lowercase, trim trailing spaces Keywords: awk Message-ID: <1991Apr20.075507.17530@midway.uchicago.edu> Date: 20 Apr 91 07:55:07 GMT References: <1817@wjvax.UUCP> Sender: news@midway.uchicago.edu (NewsMistress) Organization: University of Chicago Lines: 69 In article <1817@wjvax.UUCP> mario@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@uchicago.bitnet goer@sophist.uchicago.edu rutgers!oddjob!gide!sophist!goer