Path: utzoo!utgpu!watserv1!watmath!att!occrsh!uokmax!munnari.oz.au!samsung!uakari.primate.wisc.edu!sdd.hp.com!elroy.jpl.nasa.gov!jpl-devvax!lwall From: lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) Newsgroups: comp.unix.questions Subject: Re: Selective translation Message-ID: <8677@jpl-devvax.JPL.NASA.GOV> Date: 11 Jul 90 01:00:46 GMT References: <1553@dfsun1.electro.swri.edu> Reply-To: lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) Organization: Jet Propulsion Laboratory, Pasadena, CA Lines: 51 In article <1553@dfsun1.electro.swri.edu> jackson@dfsun1.electro.swri.edu (Keith Jackson) writes: : I was trying to filter a file by making the first word lowercase and : leaving the rest as is. My solution: : : % awk -f filt1.awk foo | tr A-Z a-z > stage1.b : % pr -m -t -s -l1 stage1.a stage1.b > final : : where filt1.awk contains: : { : print $1; : for (i = 2; i < NF; i++) : printf("%s ", $i) >> "stage1.a"; : if (NF > 1) : print $NF >> "stage1.a"; : } : : One flaw to this solution is that pr(1) ends up adding an extra : line with the separation character (tab) on it. The question is, how : does one do this more easily? Well, there's perl -pe 's/\S+/($tmp = $&) =~ y:A-Z:a-z:, $tmp/e' foo >final or perl -pe '/\S+/ && substr($_,length($`),length($&)) =~ y/A-Z/a-z/' foo >final or perl -pe '($x)=/(\S+)/ && $x =~ y/A-Z/a-z/ && s//$x/' foo >final or perl -ne '@x = split(/(\S+)/); $x[1] =~ y/A-Z/a-z/; print @x' foo >final or even sed -e 'h' \ -e 's/^\([ ]*[^ ][^ ]*\).*/\1/' \ -e 'y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/' \ -e 'G' \ -e 's/\n[ ]*[^ ][^ ]*//' foo >final where [ ] and [^ ] can be expanded to have a tab as well. Take your pick. All of these will preserve the whitespace around the first word on each line, which you can't do very easily with awk. Larry Wall lwall@jpl-devvax.jpl.nasa.gov