LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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, 09:29 PM   #1
babbab
Member
 
Registered: Mar 2010
Distribution: slackware64 current
Posts: 104

Rep: Reputation: 4
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, 09:53 PM   #2
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
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, 10:02 PM   #3
babbab
Member
 
Registered: Mar 2010
Distribution: slackware64 current
Posts: 104

Original Poster
Rep: Reputation: 4
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 10:03 PM.
 
Old 01-30-2013, 10:17 PM   #4
babbab
Member
 
Registered: Mar 2010
Distribution: slackware64 current
Posts: 104

Original Poster
Rep: Reputation: 4
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, 06:44 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Using your example:
Code:
awk '!_[$1]++' file
 
1 members found this post helpful.
Old 01-31-2013, 04:17 PM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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



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 07:50 PM
[SOLVED] printing array in awk ghantauke Linux - Newbie 3 11-24-2010 09:16 AM
[perl] copying an array element into another array s0l1dsnak3123 Programming 2 05-17-2008 01:47 AM
C++ Six Element Array petercool Programming 2 08-20-2003 11:08 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:57 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
Open Source Consulting | Domain Registration