[NTLUG:Discuss] awk question I think

Chris Cox cjcox at acm.org
Thu Aug 8 09:59:17 CDT 2002


Bobby Wrenn wrote:

>OK I'm getting to the point of being dangerous with sed. Now I have a comma 
>delimited file where I need to trim some characters out of a field.
>
>What I would like to do is take the last 4 characters of the fifth field and 
>place them in a new field. The new field can be at the beggining or end of 
>the record.
>
>Is this a problem for awk? If so, any pointers?
>
>TIA
>Bobby
>
An awk solution (I think... I typed this pretty fast)...


 awk -F, '
{
    le5=length($5) - 4;
    newfield=substr($5,(le5+1));
    $5=substr($5,1,le5);
    for (i=1;i<=NF;i++)
        printf("%s,", $i);
    print newfield;
}'







More information about the Discuss mailing list