The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
how to delete duplicate rows in a file vamshikrishnab Shell Programming and Scripting 5 06-18-2008 10:00 AM
Splitting file based on number of rows wahi80 Shell Programming and Scripting 2 06-03-2008 09:38 PM
duplicate rows in a file infyanurag Shell Programming and Scripting 3 05-22-2008 12:39 AM
Remove Duplicate lines from File Nysif Steve UNIX for Dummies Questions & Answers 18 09-09-2007 08:57 AM
Remove Duplicate Lines in File Teh Tiack Ein Shell Programming and Scripting 5 01-12-2006 08:30 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-26-2008
risk_sly risk_sly is offline
Registered User
  
 

Join Date: Sep 2008
Posts: 17
Remove duplicate rows of a file based on a value of a column

Hi,

I am processing a file and would like to delete duplicate records as indicated by one of its column. e.g.

COL1 COL2 COL3
A 1234 1234
B 3k32 2322
C Xk32 TTT
A NEW XX22
B 3k32 2322


I want the file not to contain duplicate COL1. i.e. the file should only contain the ff:

COL1 COL2 COL3
A 1234 1234
B 3k32 2322
C Xk32 TTT


The records with duplicate COL1 were deleted.

Anybody who has suggestions on how to do this?

Thank you.
  #2 (permalink)  
Old 09-26-2008
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,643
Code:
awk -F, '!arr[$1]++' oldfile > newfile

Last edited by jim mcnamara; 09-26-2008 at 05:51 AM.. Reason: new FS setting
  #3 (permalink)  
Old 09-26-2008
risk_sly risk_sly is offline
Registered User
  
 

Join Date: Sep 2008
Posts: 17
Thanks for the reply Jim. But when I tried the script, it returned "event not found error". any idea what's causing this error? also, i forgot to include in my sample, that the file i want to process is comma delimited. thank you.

COL1, COL2, COL3
A, 1234, 1234
B, 3k32, 2322
C, Xk32, TTT
A, NEW, XX22
B, 3k32, 2322
  #4 (permalink)  
Old 09-26-2008
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,643
Look at the change above - also try gawk or nawk especailly if you are on a solaris box.
The statement is okay for a modern awk.
  #5 (permalink)  
Old 09-26-2008
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,794
Quote:
Originally Posted by risk_sly View Post
Thanks for the reply Jim. But when I tried the script, it returned "event not found error". any idea what's causing this error?
[...]
It's your shell ((t)csh I suppose).
Try using a script:

Code:
$ cat uniq.awk 
!arr[$1]++
$ awk -f uniq.awk file
COL1, COL2, COL3
A, 1234, 1234
B, 3k32, 2322
C, Xk32, TTT
  #6 (permalink)  
Old 09-26-2008
risk_sly risk_sly is offline
Registered User
  
 

Join Date: Sep 2008
Posts: 17
Thanks again Jim, but I still get the "arr[: event not found error". I also noticed that when I recall the command (by pressing up arrow key), the part "![arr", is removed from the script. ie. the script becomes

awk -F, '$1]++' oldfile > newfile

im running on a solaris, and have also tried gawk and nawk, but the same error is being returned.

thank you.
  #7 (permalink)  
Old 09-26-2008
risk_sly risk_sly is offline
Registered User
  
 

Join Date: Sep 2008
Posts: 17
Thanks radolouv. But how do I use this?
$ cat uniq.awk
!arr[$1]++
$ awk -f uniq.awk file


what is uniq.awk file?
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 02:09 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0