For Programmers: Free Programming Magazines  


Home > Archive > AWK > July 2004 > Re: print all fields after $14









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: print all fields after $14
Ed Morton

2004-07-16, 3:55 am



Ed Morton wrote:
> Eric Pement wrote:
>
>
>
>
> You could make your SOLUTION 1 more robust be changing it to:
>
> $1=$2="";print substr($0,(2*length(OFS))+1)
>
> to account for any length of output field separator.
>
> In any case, if FS is set to it's normal value of white-space, then I prefer
> this solution:
>
> gawk 'BEGIN{s="[[:space:]][[:space:]]*"}{ sub($1s$2s,"");print}'


It was pointed out elsewhere that the above doesn't work for some input
lines (e.g. those that start with white-space). This should work instead:

gawk --posix 'sub(/ ^[[:space:]]*([^[:space:]]*[[:space:]]*)
{1}/,"")'

The number within the "{...}" is the number of initial fields to delete,
so to print all fields after 14 as originally requested, you'd do:

gawk --posix 'sub(/ ^[[:space:]]*([^[:space:]]*[[:space:]]*)
{14}/,"")'

Regards,

Ed.

Laurent Schneider

2004-07-16, 8:55 am

$ echo 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | awk
'{$1=$2=$3=$4=$5=$6=$7=$8=$9=$10=$11=$12
=$13=null;
sub("^"FS"*","");print}'
14 15 16 17 18 19 20
Ed Morton

2004-07-16, 3:55 pm



Laurent Schneider wrote:
> $ echo 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | awk
> '{$1=$2=$3=$4=$5=$6=$7=$8=$9=$10=$11=$12
=$13=null;
> sub("^"FS"*","");print}'
> 14 15 16 17 18 19 20


No need for the "^" or the "*" in the sub since $0 is being re-evaluated
by the assignment so there'd just be a singe FS at the start of the
line, but the problem is that that doesn't preserve the formatting on
the output line:

$ echo "1 2 3 4" | gawk '{$1=$2=null;sub("^"FS"*","");print}'
3 4
$ echo "1 2 3 4" | gawk --posix
'sub(/ ^[[:space:]]*([^[:space:]]*[[:space:]]*)
{2}/,"")'
3 4

Regards,

Ed.

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com