LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   awk help printing only if element is new in array (http://www.linuxquestions.org/questions/programming-9/awk-help-printing-only-if-element-is-new-in-array-4175447921/)

babbab 01-30-2013 09:29 PM

awk help printing only if element is new in array
 
so far

I could build array using

Code:

name_list[$3] = 1
adding only 3rd column to name_list
how do I print only if element is new in array?

danielbmartin 01-30-2013 09:53 PM

Quote:

Originally Posted by babbab (Post 4881048)
how do I print only if element is new in array?

What characteristic distinguishes New from Old?

Daniel B. Martin

babbab 01-30-2013 10:02 PM

say data is..

Code:

aaa 123
bbb 456
bbb 789
ccc 101112
ccc 131415
....

so

Code:

name_list[$1] = 1

now array contains
namelist["aaa"] = 1
namelist["bbb"] = 1
namelist["ccc"] = 1

my intention is to print out only non-duplicated at certain column records.
it should only print out first 1st column encountered data

so desired output is..

Code:

aaa 123
bbb 456
ccc 101112


babbab 01-30-2013 10:17 PM

hmmm

this code actually worked for that matter

Code:

if (name_list[$3] == 0){
    #print code
}
name_list[$3] = 1

thanks for reading my post!!!

grail 01-31-2013 06:44 AM

Using your example:
Code:

awk '!_[$1]++' file

David the H. 01-31-2013 04:17 PM

And in case you're wondering how grail's line works, it's basically entry #43 here:

http://www.catonmat.net/blog/awk-one...ined-part-two/


All times are GMT -5. The time now is 02:07 AM.