help me with this sed expression

Gautam Gopalakrishnan ggop at madras.dyndns.org
Mon Jan 5 18:22:27 PST 2004


On Tue, Jan 06, 2004 at 12:30:42PM +1030, Malcolm Kay wrote:
> On Mon, 5 Jan 2004 22:19, Zhang Weiwu wrote:
> > Hello. I've worked an hour to figure out a serial of sed command to process
> > some text (without any luck, you kown I'm kinda newbie). I really
> > appreciate your help.
> >
> > The original text file is in this form -- for each line:
> > one Chinese word then one or two English word seperated by space.
> >
> > I tried to do things like s/\(.*\)\([a-z]*\)/\2 \1/ but the first \(.*\) is
> > too greedy and included the rest [a-z].
> 
> Well the greedy part is easily fixed with:
>   s/\([^a-z]*\)\([a-z]*\)/\2 \1/
> 
> But this will not work for those lines with 2 english words. The following should:
> % sed -n -e 's/\([^a-z]*\)\([a-z]*\) .*/\2 \1/p' -e 's/\([^a-z]*\)[a-z]* \([a-z]*\)/\2 \1/p' original > target


I think awk is easier:

awk '{print $2 " " $3 " " $1}' original | tr -s > target

Gautam



More information about the freebsd-questions mailing list