Selective translation
Larry Wall
lwall at jpl-devvax.JPL.NASA.GOV
Wed Jul 11 11:00:46 AEST 1990
In article <1553 at dfsun1.electro.swri.edu> jackson at 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 at jpl-devvax.jpl.nasa.gov
More information about the Comp.unix.questions
mailing list