[ILUG] awk and regular expression back referencing
Braun Brelin
bbrelin at gmail.com
Fri Feb 5 14:04:39 GMT 2010
Thanks.
The correct answer to my problem should probably be "Use Perl"
:-)
Braun
On Fri, Feb 5, 2010 at 2:02 PM, <paul at clubi.ie> wrote:
> On Fri, 5 Feb 2010, Braun Brelin wrote:
>
> /^([a-zA-Z]{5}).*/ {print (\1);}
>>
>
> It's worth noting that AWK splits lines into fields using the FS as input
> to a regular expression. You can dynamically change the FS as the programme
> goes along, e.g:
>
> echo wootbabblahbaaabblob | awk '
>
> BEGIN {FS="ba*b"}
> {
> for (i = 1; i <= NF; i++)
> print $i
> }
>
> when given "wootbabblahbaaabblob" will print:
>
> woot
> blah
> blob
>
> You can change it as you go along too:
>
> $1 == "SetFS" { FS = $2 }
> $1 != "SetFS" && <other conditions and/or REs> { ... }
>
> though, setting the FS only affects splitting of the line prior the
> patterns being matched.
>
> You can also use 'split' to explicitly split a line.
>
> Not as powerful as you want, but hey.
>
>
> regards,
> --
> Paul Jakma paul at jakma.org Key ID: 64A2FF6A
> Fortune:
> The only possible interpretation of any research whatever in the `social
> sciences' is: some do, some don't.
> -- Ernest Rutherford
>
More information about the ILUG
mailing list