LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 01-30-2013, 10:29 PM   #1
babbab
Member
 
Registered: Mar 2010
Posts: 60

Rep: Reputation: 1
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?
 
Old 01-30-2013, 10:53 PM   #2
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Ubuntu
Posts: 1,165

Rep: Reputation: 305Reputation: 305Reputation: 305Reputation: 305
Quote:
Originally Posted by babbab View Post
how do I print only if element is new in array?
What characteristic distinguishes New from Old?

Daniel B. Martin
 
Old 01-30-2013, 11:02 PM   #3
babbab
Member
 
Registered: Mar 2010
Posts: 60

Original Poster
Rep: Reputation: 1
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

Last edited by babbab; 01-30-2013 at 11:03 PM.
 
Old 01-30-2013, 11:17 PM   #4
babbab
Member
 
Registered: Mar 2010
Posts: 60

Original Poster
Rep: Reputation: 1
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!!!
 
Old 01-31-2013, 07:44 AM   #5
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,692

Rep: Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987
Using your example:
Code:
awk '!_[$1]++' file
 
1 members found this post helpful.
Old 01-31-2013, 05:17 PM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950
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/
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Does calling an element of an array calls the whole array unkn(0)wn Programming 3 07-06-2012 08:50 PM
[SOLVED] printing array in awk ghantauke Linux - Newbie 3 11-24-2010 10:16 AM
[perl] copying an array element into another array s0l1dsnak3123 Programming 2 05-17-2008 02:47 AM
C++ Six Element Array petercool Programming 2 08-20-2003 12:08 PM


All times are GMT -5. The time now is 04:49 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration