[ILUG] [OT] awk FS question
Niall O Broin
niall at magicgoeshere.com
Mon Sep 11 19:16:21 IST 2000
On Mon, Sep 11, 2000 at 06:16:30PM +0100, Mark Webb wrote:
> > From: Paul Jakma [mailto:paulj at itg.ie]
> >
> > doc's for awk say the FS can be set to a regexp. I want FS to split
> > fields on spaces, tabs, etc and '/'.
> >
> > I've tried
> >
> > FS = "[:space]\/"
> > = "[[:space:]\/]"
> > = "[\ /]"
> > = "/[[:space:]/]"
> > = "/[[\/[:space:]]"
> >
> > and loads of combinations thereof.. just can not get it to fscking do
> > what i want.. arrgggg..
>
> [mwebb at linux ~]$ cat m
> linewithnospaces
> a space tab/slash:colon
>
> ^___ tab
>
> [mwebb at linux ~]$ cat p.awk
> #
> #
> BEGIN { FS="[ \t/]" }
>
> { print "[", $1, "] [", $2, "] [", $3, "] [", $4, "]" }
>
> END { }
>
> [mwebb at linux ~]$ awk -f p.awk m
> [ linewithnospaces ] [ ] [ ] [ ]
> [ a ] [ space ] [ tab ] [ slash:colon ]
>
> Easier than perl...
Well, I couldn't let that pass :-) . For the same file m I submit that
perl -F"[\s/]" -lane 'print "[ $F[0] ] [ $F[1] ] [ $F[2] ] [ $F[3] ]"' m
is "easier" although neither solution is easy if you don't know the
respective language :-) In either case, all the brackets that Mark threw in
make the solution a little less readable for those who don't know the
languages .
Regards,
Niall
More information about the ILUG
mailing list