For Programmers: Free Programming Magazines  


Home > Archive > AWK > December 2004 > Awk arrays and specific character matching









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 Awk arrays and specific character matching
GREG_D

2004-12-03, 3:56 am

am relativity new to awk but what I'm trying to do is take a list of
serial numbers and parse out the duplicates, but the problem is I only
want to remove duplicate entrys if they are at certain characters of
the serial number.
(or i could just use uniq)

It's a 17 digit serial number but i only want to remove duplicates if
they appear in digit 1,2,3,4,5,6,7,8, 10,11,12 disregarding any
duplication in digit 9 or 13-17

is this posible with a awk array?

or do i need to use somthing else to acomplish this?



thanks
William James

2004-12-03, 3:56 pm

# Compare 17-digit serial numbers based on digits 1--8 and
# 10--12. Discard duplicates.
{ fixed = fix($0)
if ( !(fixed in a) )
{ print
a[fixed]++
}
}

function fix(s)
{ return substr(s,1,8) substr(s,10,3)
}


Does this do what you want?
Ed Morton

2004-12-03, 3:56 pm



GREG_D wrote:
> am relativity new to awk but what I'm trying to do is take a list of
> serial numbers and parse out the duplicates, but the problem is I only
> want to remove duplicate entrys if they are at certain characters of
> the serial number.
> (or i could just use uniq)
>
> It's a 17 digit serial number but i only want to remove duplicates if
> they appear in digit 1,2,3,4,5,6,7,8, 10,11,12 disregarding any
> duplication in digit 9 or 13-17
>
> is this posible with a awk array?
>
> or do i need to use somthing else to acomplish this?


Something like this should do it if you want to keep the last occurrence
of the serial number:

awk 'BEGIN{FS=""}
{a[$1$2$3$4$5$6$7$8$10$11$12]=$0}
END{for (i in a) print a[i]}'

If you want to keep the first occurrence then it's:

awk 'BEGIN{FS=""}
{i=$1$2$3$4$5$6$7$8$10$11$12}
!(i in a){a[i]=$0}
END{for (i in a) print a[i]}'

Regards,

Ed.
GREG_D

2004-12-03, 8:55 pm

gregdodds@canada.com (GREG_D) wrote in message news:<f530cee6.0412022331.341444a3@posting.google.com>...
> am relativity new to awk but what I'm trying to do is take a list of
> serial numbers and parse out the duplicates, but the problem is I only
> want to remove duplicate entrys if they are at certain characters of
> the serial number.
> (or i could just use uniq)
>
> It's a 17 digit serial number but i only want to remove duplicates if
> they appear in digit 1,2,3,4,5,6,7,8, 10,11,12 disregarding any
> duplication in digit 9 or 13-17
>
> is this posible with a awk array?
>
> or do i need to use somthing else to acomplish this?
>
>
>
> thanks


hey thanks to both of you for all your help

that worked great, just what i needed
William James

2004-12-07, 3:59 am

# Compare 17-digit serial numbers based on digits 1--8 and
# 10--12. Discard duplicates.
{ fixed = fix($0)
if ( !(fixed in a) )
{ print
a[fixed]++
}
}

function fix(s)
{ return substr(s,1,8) substr(s,10,3)
}


Does this do what you want?
Sponsored Links







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

Copyright 2008 codecomments.com