UNIX Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsOperating SystemsUNIX Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old August 24th, 2006, 07:58 AM
smerk smerk is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2006
Posts: 1 smerk User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 19 m 16 sec
Reputation Power: 0
Awk script, print lines with one equivalent field

Hi,
I have files in which some lines have identical fields.
4696 1 741.30345 M1-D6 5
4793 1 741.30345 M1-D6 5
5472 1 740.37771 M1-D6 5
I'd like awk to compare only $3 and printout all and
only full lines ($0) with the same $3.
awk '$3 = prev { print; prev = $3 }' <file>
gives removes duplicates fine but I can't get
duplicates only, '$3 != prev ...' fails to give
anything. Thanks for any tips.

Reply With Quote
  #2  
Old August 24th, 2006, 08:19 AM
mu's Avatar
mu mu is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2006
Location: Dublin, Ireland
Posts: 174 mu User rank is Sergeant Major (2000 - 5000 Reputation Level)mu User rank is Sergeant Major (2000 - 5000 Reputation Level)mu User rank is Sergeant Major (2000 - 5000 Reputation Level)mu User rank is Sergeant Major (2000 - 5000 Reputation Level)mu User rank is Sergeant Major (2000 - 5000 Reputation Level)mu User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 3 Days 23 h 39 m 56 sec
Reputation Power: 39
I think this should do the trick

Code:
sort file.txt -nk 3 | awk '
BEGIN { prev = $3 }
($3 == prev) {print $0}
{prev = $3}
'


If the file's already sorted, I guess you can leave out the first part of that

Code:
sort file.txt -nk 3 |

Reply With Quote
  #3  
Old August 24th, 2006, 01:31 PM
jim mcnamara jim mcnamara is offline
......@.........
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jun 2004
Posts: 1,308 jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 3 Days 5 h 20 m 49 sec
Reputation Power: 48
You can get awk to print duplicated lines
Code:
awk '{ arr[$3]++; (arr[$3]>0) {print $0} }' file.txt > dupfile.txt

Reply With Quote
Reply

Viewing: Dev Shed ForumsOperating SystemsUNIX Help > Awk script, print lines with one equivalent field


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
Stay green...Green IT